Question

Using Python: A Prime number is an integer greater than 1 that cannot be formed by...

Using Python:

A Prime number is an integer greater than 1 that cannot be formed by multiplying two smaller integer other than 1 and itself. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1. In this question you will write a program that takes a sequence of integers from the user and display all the prime numbers contained in that sequence.

We will separate this question in 2 steps Step 1 (5 pts)

Write a script that accepts a sequence of positive integer from the user and display all the prime numbers from that sequence. Your program should accept and save in a list the input from the user until the value -1 is entered. Your program should also make sure that only positive values are entered. You can assume the users only enter integers.

Step 2 (5 pts) We can improve the program usability by adding the following functions: - A function is_prime that receives an integer as parameter, check if this integer is a prime number and returns a boolean value depending on the result. - A function find_primes that receives the user sequence as parameter and use the function is_prime to figure out which are the prime values. The function then returns a list containing all the prime values contained in the user sequence.

Your user input script should be similar as in part 1 and use the result of the find_primes function to display the list of prime numbers.

The following is a sample output. User inputs is displayed in bold

Enter a value: 5 Enter the next value: 8 Enter the next value: -6 Incorrect Value Enter another value: 6 Enter the next value: 3 Enter the next value: 5 Enter the next value: 12 Enter the next value: 45 Enter the next value: 21 Enter the next value: 8 Enter the next value: -1 You entered the following values : [5, 8, 6, 3, 5, 12, 45, 21, 8] From you list, the prime numbers are : {3, 5}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

===========================================================================

# step 2
def is_prime(num):
    for i in range(2, num):
        if (num % i) == 0:return False
    else:
        return True


#A function find_primes that receives the user sequence as parameter
def find_primes(user_numbers):
    primes=[]
    for n in user_numbers:
        if is_prime(n) and n not in primes:primes.append(n)
    primes.sort()
    return primes

    # step 1
user_numbers=[]
while True:
    num = int(input('Enter a value: '))
    if num==-1:break
    elif num<0:print('Incorrect Value')
    else:user_numbers.append(num)

print('You entered the following values:',user_numbers)

prime_numbers=[]
for n in user_numbers:
    if is_prime(n):prime_numbers.append(n)

primes=find_primes(user_numbers)
print('From you list, the prime numbers are:',primes)

Add a comment
Know the answer?
Add Answer to:
Using Python: A Prime number is an integer greater than 1 that cannot be formed by...
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
  • A positive integer greater than 1 is said to be prime if it has no divisors...

    A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that defines two functions is_prime() and list_primes(). is_prime() will determine if a number is prime. list_primes() will list all the prime numbers below itself other than 1. For example, the first five prime numbers are: 2, 3, 5, 7 and 11." THE PROGRAM...

  • For Python: A prime number is defined as an integer greater than 1 that has no...

    For Python: A prime number is defined as an integer greater than 1 that has no positive divisors other than 1 and itself. Write a program that prompts the user for an integer > 1. Validate the value is > 1 (if not, ask for another). Use a loop to determine if the number is prime or not. Issue an appropriate message. [complete this part before proceeding]. Add a loop that continues to ask the user if they would like...

  • A Prime Number is an integer which is greater than one, and whose only factors are...

    A Prime Number is an integer which is greater than one, and whose only factors are 1 and itself. Numbers that have more than two factors are composite numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. The number 1 is not a prime number. Write a well-documented, Python program - the main program, that accepts both a lower and upper integer from user entries. Construct a function, isPrime(n), that takes...

  • (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers...

    (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers (not including 1). For example, 2, 3, 5 are prime numbers but 4 (2*2) and 6 (2*3) are not. Write a C++ program that receives the start point and end point from user and displays all the prime numbers in this range. The program also receives how many numbers to display per row. No need to validate. Print the results in a tabular format....

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    17. Prime Numbers A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or...

  • Q2. To check and print if entered integer is prime or not. Prompt the user to...

    Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return...

  • Problem 1 Write a Python program to do the following: (A) Ask the user to enter...

    Problem 1 Write a Python program to do the following: (A) Ask the user to enter as many integers from 1 to 10 as he/she wants. Store the integers entered by the user in a list. Every time after the user has entered an integer, use a yes/no type question to ask whether he/she wants to enter another one. (B) Display the list. (C) Calculate and display the average of the integers in the list. (D) If the average is...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

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