top of page

Project Euler Problem 5 - Smallest positive number that is evenly divisible by the numbers 1-20

3 days ago

2 min read

0

8

0


Finding the Smallest Multiple Using Python

Project Euler Problem 5 asks us to find the smallest positive number that is evenly divisible by all numbers from 1 to 20. Let's solve it with Python.

The Problem

We need to find the smallest number that can be divided by each of the numbers from 1 to 20 without any remainder. For example, 2520 is the smallest number divisible by all numbers from 1 to 10.

The Solution

python

for i in range(0,300000000):
    if i % 2 != 0:
        continue
    if i % 3 != 0:
        continue
    if i % 4 != 0:
        continue
    if i % 5 != 0:
        continue
    if i % 6 != 0:
        continue
    if i % 7 != 0:
        continue
    if i % 8 != 0:
        continue
    if i % 9 != 0:
        continue
    if i % 10 != 0:
        continue
    if i % 11 != 0:
        continue
    if i % 12 != 0:
        continue
    if i % 13 != 0:
        continue
    if i % 14 != 0:
        continue
    if i % 15 != 0:
        continue
    if i % 16 != 0:
        continue
    if i % 17 != 0:
        continue
    if i % 18 != 0:
        continue
    if i % 19 != 0:
        continue
    if i % 20 == 0:
        print(i)

How It Works

  1. Loop through numbers from 0 to 300,000,000

  2. Check divisibility for each number from 2 to 20 using the modulo operator %

  3. Continue statement skips to the next iteration if any divisibility check fails

  4. Print the result when a number passes all divisibility tests

The Result

The smallest positive number evenly divisible by all numbers from 1 to 20 is 232792560.

3 days ago

2 min read

0

8

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page