Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Project Euler Problem 5
- def isPrime(n):
- for i in range(2,int(n**0.5)+1):
- if n%i==0:
- return False
- return True
- def factor(n):
- factors = []
- #Get the first factors
- if isPrime(n) == False:
- for i in range(2,n):
- if n%i == 0:
- factors.append(i)
- factors.append(int(n/i))
- break
- else: return [1,n]
- #Continue until fully factored(all nums are prime)
- while False in [isPrime(i) for i in factors]:
- newFactors = []
- for f in factors:
- if isPrime(f) == False:
- for j in range(2,f):
- if f%j == 0:
- newFactors.append(j)
- newFactors.append(int(f/j))
- break
- else: newFactors.append(f)
- factors = newFactors
- return factors
- numFactors = [factor(i) for i in range(1,21)]
- h = {}
- for i in numFactors:
- for j in set(i):
- if j in h:
- if i.count(j) > h[str(j)]:
- h[str(j)] = i.count(j)
- else:
- h[str(j)] = i.count(j)
- a = 1
- for i in h: a*=int(i)**h[i]
- print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement