Question

Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...

Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime numbers. To write your program, you will need to use the following programming features: The random library (module) A function A variable that will prompt the user to say how many random numbers the program should generate A for loop with a range function A randint function taken from the random library (module); the randint function takes two arguments – the beginning and the ending of a range An if/else statement print commands, and A calling function. Before you write your program, you might want to outline what needs to be done and write the pseudocode for this program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import random
from math import sqrt


def isPrime(num):
    """
    method to determine whether a number is prime or not
    """
    if num < 2:
        return False
    # iterate till sqrt(num) if it is divisible by any other number than it is not prime
    for i in range(2, int(sqrt(num)) + 1):
        if num % i == 0:
            return False

    return True


if __name__ == '__main__':

    # prompt the user to enter how many random numbers he want to generate
    size = int(input("enter how many random numbers you want to generate"))
    # ask user to enter the start and end range in which he want to generate random values
    starting, ending = map(int, input("Enter the starting and ending numbers to generate random numbers").split())
    print(starting, ending)
    numbers = []
    
    # make a list of all the randomly generated numbers 
    for i in range(size):
        numbers.append(random.randint(starting, ending))

    print("The list of randomly generated numbers is : ")
    print(numbers)
    i=0
    # if even one number is not prime  , then display set of generated numbers is not prime
    for i in range(size):
        if isPrime(numbers[i]):
            continue
        else:
            break

    if i < size:
        print("All the numbers in the set of randomly generated numbers are not prime")
    else:
        print("All the numbers in the set of randomly generated numbers are prime")

Screenshots:

pimport random afrom math import sqrt def isPrime(num): if num < 2: return False for i in range(2, int(sqrt(num)) + 1): if nugif main name size = int(input(enter how many random numbers you want to generate)) starting, ending = map(int, input(EnteOutput:

enter how many random numbers you want to generate10 Enter the starting and ending numbers to generate random numbers1 50 1 5

Add a comment
Know the answer?
Add Answer to:
Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your program  write a program to test whether a number is prime or not. Your program should...

    Your program  write a program to test whether a number is prime or not. Your program should ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers. The program should continue to ask for input, till it sees a 0, when it exits. Please submit printed pseudocodeshould ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers....

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • 2. Use function to write a program that prints all prime numbers in a range of...

    2. Use function to write a program that prints all prime numbers in a range of a user input numbers. a. Draw a flow chart for the function IsaPrimenumber [5 marks] b. Draw a flow chart for the whole scenario [10 marks] c. Convert the flow charts to a c program. [15 marks] In C programming language

  • Write a user-defined MATLAB function that gives a random integer number within a range between two...

    Write a user-defined MATLAB function that gives a random integer number within a range between two numbers. For the function name and arguments, use n = randint(a,b) where the two input arguments a and b are the two numbers and the output argument n is the random number. Use the function in the command window for the following: (a) Generate a random number between 1 and 49. (b) Generate a random number between -35 and -2

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • write program above in C only Write a program to find statistics on some random numbers....

    write program above in C only Write a program to find statistics on some random numbers. Seed the random number generator with time. In a for loop generate 12 random numbers between the values of 2 and 20. Print the numbers. As the numbers are generated, find the following: the number of even numbers (those evenly divisible by 2) e the sum of the even numbers the product of the even numbers the maximum value of all the numbers e

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • Write a program in C to generate random numbers. The program recieves 4 items on activation...

    Write a program in C to generate random numbers. The program recieves 4 items on activation through argv: 1. Name of the data file 2. Number of items for program to generate 3. Range of numbers 4. Seed of the random number generator Example run: ./rand datafile.txt 20 1000 3127 Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT