이경수 선생님의 수학실험실

Problem 5(Smallest multiple) 본문

Project Euler

Problem 5(Smallest multiple)

(이경수) 2019. 2. 8. 19:39

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]



'Project Euler' 카테고리의 다른 글

Problem 7(10001st prime)  (0) 2019.02.08
Problem 6(Sum square difference)  (0) 2019.02.08
Problem 4(Largest palindrome product)  (0) 2019.02.07
Problem 3(Largest prime factor)  (0) 2019.02.06
Problem 2(Even Fibonacci numbers)  (0) 2019.02.06
Comments