An integer greater than 1 is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not.
a) Write a function that determines whether a number is prime.
b) Use this function in a program that determines and prints all the prime numbers between 2 and 1,000.
c) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need go only as high as the square root of n. Rewrite the program and run it both ways to show that you get the same result.
a) ```
# Determines if a number is prime.
# returns 1 if number is prime
def isPrime( number ):
for x in range( 2, number ):
# return 0 if x is a factor of number
if number % x == 0:
return 0
return 1
```
b) ```
# Determines and prints all prime numbers between 2 and 1,000.
# returns 1 if number is prime
def isPrime( number ):
for x in range( 2, number ):
# return 0 if x is a factor of number
if number % x == 0:
return 0
return 1
# test numbers between 2 and 1,000
for number in range( 2, 1001 ):
# display the number if it is prime
if isPrime( number ):
print number,
```
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191
193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283
293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509
521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751
757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877
881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
c)```
# Determines and prints all prime numbers between 2 and 1,000.
import math
# returns 1 if number is prime
def isPrime( number ):
# test only up to the square root of the number
for x in range( 2, math.sqrt( number ) + 1 ):
# return 0 if x is a factor of number
if number % x == 0:
return 0
return 1
# test numbers between 2 and 1,000
for number in range( 2, 1001 ):
# display number if it is prime
if isPrime( number ):
print number,
```
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191
193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283
293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509
521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751
757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877
881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
You might also like to view...
When Outlook detects a macro attempting to send an e-mail, it launches a warning box. If the user clicks the ________ instead of the ALLOW button inside the warning box, Access will halt the macro process and will not complete any further actions
Fill in the blank(s) with correct word
If you use a card reader to download pictures, you will still need to use the camera's software
Indicate whether the statement is true or false