Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Singleton
6,253 PointsEuler 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 positive number that is evenly divisible by all of the numbers from 1 to 20?
1 Answer
Michael Hess
24,512 PointsHi Michael,
public static void main(String[] args) {
// TODO code application logic here
System.out.println(problem5());
}
public static int problem5() {
int i = 2520;
boolean found = false;
while (!found) {
i += 2520;
boolean isDividable = true;
for (int j = 11; j <= 20; j++) {
if (i % j != 0) {
isDividable = false;
break;
}
}
if (isDividable) {
found = true;
}
}
return i;
}
}
The numbers 1 - 10 are not needed in the for loop -- the numbers 11-20 are divisible by the same numbers as 1-10. So we start at 11 and end at 20 in the for loop statement.
The smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is 232792560. Hope this helps! If you have any questions -- just ask!