banovski

Project Euler, Problem #5, Python

Dec 9th, 2021 (edited)
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # The problem: 2520 is the smallest number that can be divided by each
  4. # of the numbers from 1 to 10 without any remainder. What is the
  5. # smallest positive number that is evenly divisible by all of the
  6. # numbers from 1 to 20?
  7.  
  8. # Since non-prime numbers in the range of 11 through 20 are all
  9. # divisible by a number in the range of 2 through 10, only numbers in
  10. # the former range are checked for being factors. Thus, the product of
  11. # these numbers should be the maximum checked value.
  12.  
  13. product = 1
  14. for i in range(11,21):
  15.     product *= i
  16.  
  17. # Since the biggest factor is 20 and the check starts with 20, the
  18. # step should also be 20.
  19.  
  20. checked = 20
  21. while checked <= product:
  22.     for i in range(20,10,-1):
  23.         if checked % i != 0:
  24.             break
  25.     else:
  26.         print(checked)
  27.         break
  28.     checked += 20
  29.  
  30. # 232792560
  31.  
Add Comment
Please, Sign In to add comment