A prime number is a number that is only evenly divisible by itself and 1. For ex
ID: 3690247 • Letter: A
Question
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 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Design a Boolean function called isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the function in a program that prompts the user to enter a number and then displays a message indicating whether the number is prime. The following modules should be written: getNumber, that accepts a Ref to an integer, prompts the user to enter a number, and accepts that input isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise showPrime, that accepts an integer as an argument , calls isPrime, and displays a message indicating whether the number is prime The main module, that will call getNumber and showPrime Write the pseudo code and paste it below
Explanation / Answer
. # Prime Number Sieve
2. # http://inventwithpython.com/hacking (BSD Licensed)
3.
4. import math
5.
6.
7. def isPrime(num):
8. # Returns True if num is a prime number, otherwise False.
9.
10. # Note: Generally, isPrime() is slower than primeSieve().
11.
12. # all numbers less than 2 are not prime
13. if num < 2:
14. return False
15.
16. # see if num is divisible by any number up to the square root of num
17. for i in range(2, int(math.sqrt(num)) + 1):
18. if num % i == 0:
19. return False
20. return True
21.
22.
23. def primeSieve(sieveSize):
24. # Returns a list of prime numbers calculated using
25. # the Sieve of Eratosthenes algorithm.
26.
27. sieve = [True] * sieveSize
28. sieve[0] = False # zero and one are not prime numbers
29. sieve[1] = False
30.
31. # create the sieve
32. for i in range(2, int(math.sqrt(sieveSize)) + 1):
33. pointer = i * 2
34. while pointer < sieveSize:
35. sieve[pointer] = False
36. pointer += i
37.
38. # compile the list of primes
39. primes = []
40. for i in range(sieveSize):
41. if sieve[i] == True:
42. primes.append(i)
43.
44. return primes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.