Friday, June 19, 2009

Project Euler Problem 5

Problem 5

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 number that is evenly divisible by all of the numbers from 1 to 20?


public class ProblemFive {
public static void main(String[] args) {
// Any number will be always divisible by 1
// To be divisible by 2 it has to be an even number
// Number will be greater than or equal to at least 20
int start = 20;

while (true) {
int i = 3;

for (; i <= 20; ++i) {
if (0 != (start % i)) {
break;
}
}

if (i >= 20) {
break;
}

start += 2;
}

System.out.println(start);
}
}

1 comment:

  1. I'm pretty sure this is a non-ideal method of solving this problem. The original solution shows that what you need to do is find the prime factorization of each number in the list, then multiply by the highest exponent for each prime among all of the results.

    ReplyDelete