Project Euler # 5 "Minimum Multiples" in Python

Problem 5 "Minimum multiple"

2520 is a number divisible by all integers from 1 to 10 and is the smallest of such numbers. Then, what is the smallest positive number among the numbers divisible by all integers from 1 to 20?

Python


MAX = 20
seq = range(1, MAX+1)

def compute_prime_factors(x):
  prime_factors = {}
  i = 2
  while x > 1:
    if x % i == 0:
      if(not prime_factors.has_key(i)):
        prime_factors[i] = 0
      prime_factors[i] += 1
      x = x / i
    else:
      i += 1
  return prime_factors

prime_factors = [compute_prime_factors(i) for i in seq]
result_dic = {}
for dic in prime_factors:
  for key in dic.keys():
    if(result_dic.has_key(key)):
      result_dic[key] = max(result_dic[key], dic[key])
    else:
      result_dic[key] = dic[key]

factors = [key ** result_dic[key] for key in result_dic.keys()]
result = reduce(lambda x,y: x * y, factors)

print result
print result == 232792560
print prime_factors
print result_dic
print factors

result


232792560
True
[{}, {2: 1}, {3: 1}, {2: 2}, {5: 1}, {2: 1, 3: 1}, {7: 1}, {2: 3}, {3: 2}, {2: 1, 5: 1}, {11: 1}, {2: 2, 3: 1}, {13: 1}, {2: 1, 7: 1}, {3: 1, 5: 1}, {2: 4}, {17: 1}, {2: 1, 3: 2}, {19: 1}, {2: 2, 5: 1}]
{2: 4, 3: 2, 5: 1, 7: 1, 11: 1, 13: 1, 17: 1, 19: 1}
[16, 9, 5, 7, 11, 13, 17, 19]

Recommended Posts

Project Euler # 5 "Minimum Multiples" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Functional programming in Python Project Euler 1
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Functional programming in Python Project Euler 2
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler # 12 "High Divisibility Triangular Number" in Python
Project Euler # 13 "Sum of Large Numbers" in Python
Project Euler # 6 "Difference in sum of squares" in Python
Project Euler 37
Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 17
Project Euler 26
Project Euler 8
Project Euler 23
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Create Python project documentation in Sphinx
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 11 "Maximum product in grid"
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25