0

Help please: Find the sum of all the multiples of 3 or 5 below 1000. (I'm using a website called Project Euler.net to help me practice code. I'm already stuck on the first problem.)

Can you help me with this problem?

20th Nov 2016, 10:26 PM
Dorre
Dorre - avatar
9 ответов
+ 5
something like this? public int findSum(){ int sum=0; for(int i=0;i<1000;i++){ // if i is a multiple of 3 OR 5 // (divided by either with ZERO remainder) // add i to the total sum if(i%3 == 0 || i%5 == 0) sum+=i; } System.out.println(sum); } or am i misunderstanding the question?
20th Nov 2016, 10:34 PM
Burey
Burey - avatar
+ 5
i'll go through the code line by line sum = 0; this variable will hold the final sum for(int i=0;i<1000;i++) this is the loop which goes through all the numbers from 0 to 999 (1000 is not included) each iteration of the loop the variable 'i' which was initialized to 0 (int i=0) will incremented (i++) as long as its value is LESS than 1000 (i<1000) inside the loop (remember that each loop cycle the value of i is increased by 1) if(i%3 == 0 || i%5 == 0) this condition checks if i can be divided by 3 with no remainder (i%3 == 0) OR (|| in the code) if i can be divided by 5 with no remainder (i%5 == 0) if one of the conditions is met, add the value of 'i' to the sum. a little explanation about the operator % (modulus): this is a very handy operator which can be used in many different ways in programming. in this code it is used to determine whether the value 'i' can be divided fully by 3 or 5 in a given loop cycle (iteration). the way this works is as follows: modulus of variable A on variable B (A%B) will give you the remainder, or in other words, you can say it like this: how many times can i put B inside A and what do i have as leftovers? the leftovers is the remainder few examples with numbers: 7%5 -> i can put 5 inside 7 one time and that is it, but i still have 2 as leftovers, so that is the remainder. 3%6 -> i can put 6 inside 3 ZERO times since it is bigger than 3, so the remainder is 6(as a rule of thumb, if the right side variable is bigger than the left, then the modulu is the right side variable) 28%5 -> i can put 5 inside 28 five times and my remainder is 3 (28-25=3) and finally when a number A is a multiple of B there will be NO remainder left 33%11 = 0 because 11*3 == 33 and that is exactly what was done inside the loop.
20th Nov 2016, 11:07 PM
Burey
Burey - avatar
+ 3
then it was all worth it :)
20th Nov 2016, 11:19 PM
Burey
Burey - avatar
+ 2
public class sumNumber { Public static void main ( String[] args) { Int num; numb; Int sum1; sum2; Int sumTotal; for( num=1; n<=1000;num++) { sum1+=num*3; } for(numb=1; numb<=1000;num++) { sum2+= num*5; } sumTotal = sum1+sum2; System.out.println("the total of the sum is" + sumTotal); } }
22nd Nov 2016, 11:12 PM
Vicky Li
Vicky Li - avatar
+ 2
Hey, do u know what is the answer of the question? Is it 4004000?
22nd Nov 2016, 11:16 PM
Vicky Li
Vicky Li - avatar
+ 1
@Burey thank you! Can you explain how you got the right answer? I still don't completely understand.
20th Nov 2016, 10:48 PM
Dorre
Dorre - avatar
0
@Burey Thanks for taking the time to explain! This was very helpful and now I understand it!
20th Nov 2016, 11:18 PM
Dorre
Dorre - avatar
0
@Vicky Li the answer is 233168 In Eclipse I put in: public final class Problem1{ public static void main(String[ ]args){ System.out.println(new Problem1( ).run( ) ); } public String run( ){ int sum= 0; for (int i= 0; i< 1000; i++){ if (i% 3== 0 || i% 5== 0) sum += i; } return Integer.toString(sum); } } //output: 233168
23rd Nov 2016, 3:00 AM
Dorre
Dorre - avatar
0
sum <- 0 for (i in 1:1000){ if (i%%3 ==0){ sum = sum + i } } print(sum)
15th Apr 2022, 1:34 PM
Seth Eilerts
Seth Eilerts - avatar