Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Problem 3
- # https://projecteuler.net/problem=3
- # The prime factors of 13195 are 5, 7, 13 and 29.
- # What is the largest prime factor of the number 600851475143 ?
- # Test solution of 13195 before testing 600851475143
- target = 13195
- all_factors = []
- prime_factors = []
- temp_non_prime_factors=[]
- i = 1
- while i <= target:
- #print("Count",i)
- if (target%i == 0):
- all_factors.append(i)
- i = i + 1
- print ("All factors of",target)
- print (all_factors)
- for item in all_factors:
- #if item%item==1 or item%1==item:
- #if item % i == 0 and item not in prime_factors:
- if item >=2:
- for y in range(2,item):
- if not (item % y) and item not in temp_non_prime_factors:
- #print (item)
- temp_non_prime_factors.append(item)
- print ("Non Prime factors of",target)
- print (temp_non_prime_factors)
- print ("Prime factors of",target)
- prime_factors= [e for e in all_factors if e not in temp_non_prime_factors]
- #https://stackoverflow.com/questions/21468194/how-to-remove-all-matches-in-a-python-list
- print(prime_factors)
- print("Largest prime factor is", max(prime_factors))
- # Potential to accelerate prime factorization: Sieve of Eratosthenes
- #https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement