Problem 5(Smallest multiple)
Problem 5(Smallest multiple)
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?
In Python:
import time
startTime = time.time()
multiple = 1
temp = 0
for i in range(2, 21):
for j in range(2, i+1):
temp = multiple
while i % j == 0:
i = i / j
if temp % j == 0:
temp = temp / j
else:
multiple = multiple * j
print(multiple)
print(time.time() - startTime, "seconds")
In Java:
package project_euler;
public class Euler5 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int multiple = 1;
int temp = 0;
int k = 0;
for (int i = 2; i < 21; i++) {
k = i;
for (int j = 2; j < i + 1; j++) {
temp = multiple;
while (k % j == 0) {
k = k / j;
if (temp % j == 0) {
temp = temp / j;
}
else {
multiple = multiple * j;
}
}
}
}
System.out.println(multiple);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 +"seconds");
System.out.println(endTime - startTime +"ms");
}
}
solution: 232792560
[from Project Euler: https://projecteuler.net/problem=5]