# primes.py - #calculate the primes from 2 to N...

• Test Prep
• 1

This preview shows page 1. Sign up to view the full content.

#calculate the primes from 2 to N inclusive #7:30 - 7:37 def get_max_search(): """ get a valid upper bound from the user to search for. A valid upper bound is >= 2 @returns: a valid upper bound to search for """ max_prime_search = '' #while the input isn't valid while(not(max_prime_search.isdigit() and int(max_prime_search) >= 2)): #ask for valid input max_prime_search = input('Please enter an integer >= 2: ') #input is valid, do the conversion return int(max_prime_search) def list_primes(max_prime_search): """ find all the primes between [2, max_prime_search] @max_prime_search: the upper bound to look for primes
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: @returns: a list of the primes """ primes = [2] #the only prime we know is 2 for num in range(3, max_prime_search + 1, 2): #only odd numbers can be prime for prime in primes: if(num % prime == 0): #the number was evenly divisible by a prime isPrime = False #so it must not be prime break #and there is no reason checking against the rest of the numbers else: #the number is prime primes.append(num) #add it to the list of primes return primes def main(): max_prime_search = get_max_search() primes = list_primes(max_prime_search) for prime in primes: print(prime) main()...
View Full Document

• Fall '07
• Amenta
• Prime number, valid upper bound

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern