Advertisement
Guest User

PrimeNumbers

a guest
Nov 22nd, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. prime1=[]
  2. #prime1 is a list in which I will store prime numbers, X.
  3.  
  4. prime2=[]
  5. #prime2 is a temporary list that stores numbers that cleanly divide the number in question, X.
  6.  
  7. x=100
  8. #using 100 to find all prime numbers under 100.
  9. y=99
  10. #starting with 99 because 100/100 will cleanly divide, so -1 is 99.
  11.  
  12. while x > 0:
  13. #when x = 0 is where I want to stop trying to find primes.
  14.     while y >0:
  15.     #in this loop, after each attempt to divide into x, y will reduce itself by 1 and attempt to divide again.
  16.         if x%y == 0:
  17.         #in the event of a successful divide.....
  18.             prime2.append(y)
  19.             #the value of y is stored into the prime2 temp list.
  20.         y=y-1
  21.         #y is incremented, eventually down to 0 where the while loop will stop.
  22.     if sum(prime2[:]) == 1:
  23.     #a prime number will only successfully divide by 1. y naturally starts at x-1, so it will never include x.
  24.     #if the sum of prime2 is 1, that means that x is a prime number because it is only divisible by 1, other than itself.
  25.         prime1.append(x)
  26.         #the prime number x is appended into the list prime1.
  27.         prime2.clear()
  28.         #prime2 is cleared for another loop.
  29.     x=x-1
  30.     #x increments down
  31.     y=x-1
  32.     #y follows and increments to -1 of x. While Loop then continues again.
  33.  
  34. print prime1
  35. #after the while loop determines if numbers 100 through
  36. #100 are primes, it stops. At this point, all primes
  37. #should have been appended to the list prime1.
  38. #This list is then printed.
  39.  
  40. raw_input('Press <enter> to close.')
  41. #this closes the program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement