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
Jainil Patel
1,653 PointsHow to solve project euler 1????????
HELP
Ken Alger
Treehouse TeacherJainil;
Please post the code you are using.
Ken
2 Answers
Ken Alger
Treehouse TeacherJainil;
Euler 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
public static void main(String[] args) {
long sum = 0 ;
for ( long i = 3 ; i < 1000 ; i+=3 ){
sum+=i;
}
for ( long i = 5 ; i < 1000 ; i+=5 ){
sum+=i;
}
for ( long i = 15 ; i < 1000 ; i+=15 ){
sum-=i;
}
System.out.println(sum);
}
or
public static void main(String[] args) {
int sum = 0;
for (int i = 3; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
System.out.println(sum);
}
Should both get you going in the right direction.
Post back with other questions.
Happy coding,
Ken
Jainil Patel
1,653 PointsMainly I'm having trouble converting the algorithm into Java Code I'm just beginning to learn Java
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsHello,
In order to better help you, we need to know where you're having trouble with. Are you having trouble determing an algorithm to use to determine the answer or having trouble converting that algorithm into Java code?