Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. ## Project Euler
  2. ## Problem 3
  3. ##
  4. ## The prime factors of 13195 are 5, 7, 13 and 29.
  5. ##
  6. ## What is the largest prime factor of the number 600851475143 ?
  7.  
  8. #the number we want to find
  9. number = 600851475143
  10.  
  11. listprimes = []
  12. result = [] #empty lists we will use later
  13.  
  14. #function to check if number is prime
  15. def isprime(n):
  16.     if n < 2:
  17.         return False
  18.     if n == 2:
  19.         return True    
  20.     if not n & 1:
  21.         return False
  22.     for x in range(3, int(n**0.5)+1, 2):
  23.         if n % x == 0:
  24.             return False
  25.     return True
  26.  
  27. print('Generating list...')
  28.  
  29. #generates a list of the first 10k prime numbers (arbitrary)
  30. for i in range(1, 10001):
  31.     test = isprime(i)
  32.     if test == True:
  33.         listprimes.append(i)
  34.         print('Added '+str(i)+' to the list.')
  35.  
  36. print('Verifying divisors...')
  37. i = 0 #starts up counter
  38.  
  39. try:
  40.     while True:
  41.         if number % listprimes[i] == 0:  ## this test checks if the number is divisible by the prime
  42.             result.append(listprimes[i])              ## if it is, the number is added to a list
  43.             print('Divisible by '+str(listprimes[i])) ## and it tells you so
  44.         else:
  45.             print('Not divisible by '+str(listprimes[i])) #if it isn't, it also tells you.
  46.         i += 1 #moves the counter one position ahead
  47. except IndexError:
  48.     print('End of primes list')
  49.  
  50. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement