I'm currently trying to work through the ProjectEuler questions with Python (something I've only picked up today). The question I'm working is question 5, where
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?
I have worked through the problems using Java before, so using the same method as I did before, I just created a loop that iterated, however it seems that my code never ends.
Python:
i = 1
while 1:
if i%2 == 0 and i%3==0 and i%4==0 and i%5==0 and i%6==0 and i%7==0 and i%8==0 and i%9==0 and i%10==0 and i%11==0 and i%12==0 and i%13==0 and i%14==0 and i%15==0 and i%16==0 and i%17==0 and i%18==0 and i%19==0:
print i
break
i+=1
Java:
public class p5
{
public static void main(String[] args)
{
for (int i=1;;i++)
{
if (i%1==0&&i%2==0&&i%3==0&&i%4==0&&i%5==0&&i%6==0&&i%7==0&&i%8==0&&i%9==0&&i%10==0&&i%11==0&&i%12==0&&i%13==0&&i%14==0&&i%15==0&&i%16==0&&i%17==0&&i%18==0&&i%19==0&&i%20==0)
{
System.out.println(i);
break;
}
}
}
}
Java executed that in under 3 seconds on my computer, whereas the Python code never seemed to end. Any tips?
Edit:
Apparently I typed something wrong, which caused it to never end. However, even with the entire thing written correctly (with the same output as my Java), it still took 1 minute and 20 seconds, whereas for Java it took around 1 - 2 seconds. Am I doing anything wrong? Or is Python's performance that bad (which shouldn't be afaik)