Question

In: Computer Science

Write a script that creates and calls a stored procedure named test. This procedure should identify...

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!

Solutions

Expert Solution

Short Summary:

  • Created the stored the procedure and explaines in detail using inline comments

**************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!!!

**************************************************************************************


Related Solutions

SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: 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...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named mgs --CONNECT cs270226mgs/mgs; -- Use an anonymous PL/SQL script to -- drop all tables and sequences in the current schema and -- suppress any error messages that may displayed -- if these objects don't exist BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE category_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE product_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE customer_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE address_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE order_id_seq'; EXECUTE IMMEDIATE 'DROP...
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
USING MATLAB write a script that creates eight subplots(4x2). In each one, graph the values of...
USING MATLAB write a script that creates eight subplots(4x2). In each one, graph the values of sin(n(pi)x), where -1 <=x<=1, with an interval of 0.05 for n = 1 to 8. there is no parenthesis on pi. I just didn't want to make it look like (npix) so it wouldn't be confusing to read.
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT