In: Computer Science
Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this:
2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2 9 1 3 1 1 . . .
Hint: To get this to work, you will need to nest one loop within another loop. In addition, you will need to code an IF statement within the inner loop.
MYSQL
PLEASE PROVIDE EXPLAINATION STEP-BY-STEP PLEASE. I'M TRYING TO
LEARN. THANK YOU!
Short Summary:
**************Please upvote the answer and appreciate our time.************
Stroed Procedure:
DELIMITER $$
CREATE PROCEDURE test(
IN input INT,
OUT primeNumbers VARCHAR(200))
BEGIN
/* Declare loop variables */
DECLARE currentNumber, factor, factorsCount INT;
SET currentNumber := 2;
SET primeNumbers := '';
/* Loop from 2 to input number */
WHILE (currentNumber < input) DO
/* Reset the factor and
factorsCount */
SET factor := 2;
SET factorsCount := 0;
/* Loop from 2 to currentNumber
*/
WHILE(factor <= currentNumber)
DO
/* if it is
divisible, increase the factors count */
if(currentNumber
% factor = 0)then
set factorsCount := factorsCount + 1;
end if;
/* Move to next factor for current number */
set factor =
factor + 1;
end while;
/* prime numbers has only one
factor */
if (factorsCount = 1) THEN
/* Concat the
prime number with ' ' */
SET primeNumbers
:= concat(primeNumbers, currentNumber, ' ');
END IF ;
/* Move to next number */
SET currentNumber := currentNumber
+ 1;
END WHILE;
END
$$
To Execute the stored procedure:
CALL TEST(100, @primeNumbers);
SELECT @primeNumbers AS 'Prime Numbers';
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************