Question

In this assignment you are asked to write a Python program to determine all the prime...

In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the first input value, re-prompt them to enter a correct value. Both the start and the end input values should be inclusive in the range.

Hints:

1. Use a for loop to iterate through the range.

2. You can use a while or for loop to check whether a number is prime or not, but you must create a function to test whether an integer is prime or not. To do that, divide every number in the range by 2 up through half the number. If any of those divides evenly into your test number, the number is not prime and the function returns a Boolean ‘False’. Otherwise, it is prime and the function returns ‘True’.

3. If the function returns ‘True’ for a number, append it to the list.

4. After the end of the range is reached, print the list by iterating using a ‘for’ loop.

5. Start by writing code to use a range of 0 to some number (i.e. 0 to 25), then when your tests show that works add the starting range value (i.e. 20 to 100).

Please make it simple to read as possible. I'm a beginner.

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

def isPrime(n):
#deal with some starting cases
if n==1:
return False
for i in range(2,n//2+1):
if n%i==0:#if remainder is 0, that is the number is divisible
return False
return True#if we reach here, it means we did not find any divisor
  
num1=int(input('Enter starting number: '))
while num1<1:#this loop will run as long as num1 is less than 1
print('Please enter a positive number')
num1=int(input('Enter starting number: '))
num2=int(input('Enter ending number: '))
while num2<1 or num2<=num1:#this loop will run as long as num2 is less than 1 or 1
print('Please enter a positive number greater than '+str(num1))
num2=int(input('Enter starting number: '))
  

primes=[]
for i in range(num1,num2+1):
if isPrime(i):
primes.append(i)

#printing the list
for i in primes:
print(i,end=' ')

Add a comment
Answer #2

Program

Primes.py

"""
Python program to determine all the prime numbers in given range
"""


def isPrime(n) :

"""
Function that checks if the given number is prime
"""
  
# Corner cases
if (n <= 1) :
return False
if (n <= 3) :
return True
  
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0) :
return False
  
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
  
return True


# printing Introduction
print("\nThis program prints all the prime number in a given range\n")


# reading start of the range
range_start = int(input("Enter the starting value of the range : "))

# reading end of the range
range_end = int(input("Enter the ending value of the range : "))

if(range_start < 0 | range_end < 0 | range_end < range_start):
  
# reading start of the range
range_start = int(input("Enter the starting value of the range : "))

# reading end of the range
range_end = int(input("Enter the ending value of the range : "))


# empty list to store the prime numbers
primes = []

# both the values are inclusive
# looping over the values within the range
for number in range(range_start, range_end+1):

# check if the number is prime
if(isPrime(number)):

# if the number is prime append to the list
primes.append(number)

# printing the list of prime number in the given range
print("Prime Number in the given range are : ", primes)

output:


This program prints all the prime number in a given range

Enter the starting value of the range : 20
Enter the ending value of the range : 100
Prime Number in the given range are : [23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Hope this helps!

Please let me know if any changes needed.

Thank you!

Add a comment
Know the answer?
Add Answer to:
In this assignment you are asked to write a Python program to determine all the 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
  • 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...

  • a) Write a program that uses a while loop to print all divisors of a number...

    a) Write a program that uses a while loop to print all divisors of a number supplied by the user. The program should also print the sum of all the divisors and whether the number the user entered is a prime number or not. Note: The definition of a divisor is a number that divides another evenly (i.e., without a remainder) and the definition of a prime number is a number whose only divisors are 1 and itself. b) Implement...

  • 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...

  • 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 Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

  • 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...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

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