Advertisement
Guest User

project euler 3 prime factorization unoptimized

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # Problem 3
  2. # https://projecteuler.net/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.  
  9. # Test solution of 13195 before testing 600851475143
  10. target = 13195
  11. all_factors = []
  12. prime_factors = []
  13. temp_non_prime_factors=[]
  14. i = 1
  15. while i <= target:
  16.     #print("Count",i)
  17.     if (target%i == 0):
  18.         all_factors.append(i)
  19.     i = i + 1
  20.  
  21. print ("All factors of",target)
  22. print (all_factors)
  23.  
  24. for item in all_factors:
  25.     #if item%item==1 or item%1==item:
  26.    
  27.     #if item % i == 0 and item not in prime_factors:
  28.     if item  >=2:
  29.         for y in range(2,item):
  30.             if not (item % y) and item not in temp_non_prime_factors:
  31.                 #print (item)
  32.                 temp_non_prime_factors.append(item)
  33.  
  34. print ("Non Prime factors of",target)
  35. print (temp_non_prime_factors)
  36.  
  37. print ("Prime factors of",target)
  38.  
  39. prime_factors= [e for e in all_factors if e not in temp_non_prime_factors]
  40.     #https://stackoverflow.com/questions/21468194/how-to-remove-all-matches-in-a-python-list
  41. print(prime_factors)
  42. print("Largest prime factor is", max(prime_factors))
  43.  
  44. # Potential to accelerate prime factorization: Sieve of Eratosthenes
  45.     #https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement