0
Use a loop to calculate the sum of all numbers that are a multiple of 3 in the range of 1 to 1000. A number is a multiple of 3
Its R practice 12.2 I can't figure out how to make the loop statement to make it work has anyone encountered this
3 Antworten
+ 2
Loops are explained in lesson 12.1. Review the lesson.
Use an if-statement the modulo operator to test if a number is a multiple of 3
+ 1
Finally figured it out
sum <- 0
i <- 1
for (i in 1:1000){
if(i%%3==0){
sum=sum+i
}
i=i+1
}
print(sum)
thank you everyone
0
G'day Lisa, would you try a few successive values from your starting seed. Test if its a multiple of 3 and then multiply from that?
Eg: input x, x is 4, test x, then test x+1, then test x+2. When (x+2)%3==0, then y=x+2, y+=3 until y>=1000
Edit: I think I misread the instructions. It is all numbers from 1 to 1000, so always starts at 3 and ends at 999.