2017-06-07

Project Euler Problem 5

The problem:

"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"

Should I go for a for-loop or a while-loop? Atleast I need to use the modulus %. After messing around, trying to mix for-loops, while-loops and if-statements, I started to think that I needed to take a break. Then suddenly I hover "y == 1" with the cursor and see "Statement seems to have no effect". What's this?! Then I realized! Double equal sign is when doing logical things like checking if x is equal to y. When assigning a value to a variable, only one equal sign is to be used!

To solve this problem I'm using a brute force way, checking every single modulus for every denominator up to 20. This takes quite some time (a minute) and there must be a more efficient (faster) way of doing this, probably there is some smart math way to do it.

The code I used:

x = 1y = 1while y != 20:
    if x % y == 0:
        y += 1    else:
        y = 1        x += 1print(x, y)

/Ludvig

No comments:

Post a Comment