Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. # read n, number user wishes to see if it's prime
  2. # use for loop, loop from 2 to n
  3. # if statement --> if a factor is found, end loop
  4. # write if the number is prime
  5.  
  6.  
  7. n = int(input("What number do you wish to test to see if it's prime?\n"))
  8. k = int(input("Up to which number do you wish to see all prime numbers?\n"))
  9.  
  10. isItPrime = 0
  11.  
  12.  
  13. for n in range(2, k + 1):
  14. if n > 1:
  15. # check for factors
  16. for i in range(2, n):
  17. if (n % i) == 0:
  18. break
  19. else:
  20. print(n, end=", ")
  21. isItPrime = 1
  22.  
  23.  
  24.  
  25.  
  26. # this is in case they enter 1
  27. else:
  28. print(n, "is not a prime number")
  29.  
  30. if(isItPrime == 1):
  31. print(n, "is a prime number")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement