The Problem: Multiples of 3 and 5
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.
Find the sum of all the multiples of 3 or 5 below 1000.
Solution: Complete
float x = 1000/3;
float y = 1000/5 -1;
float z = 1000/15;
int x1 = round(x);
int x2 = round(y);
int x3 = round(z);
float sum3 = 0;
float sum5 = 0;
float sum15 = 0;
int [] muiltiples3 = new int[x1+1];
int [] muiltiples5 = new int[x2+1];
int [] muiltiples15 = new int[x3+1];
void setup(){
size(1000,1000);
if(x1>x){
x1--;
}
if(x3>z){
x3--;
}
for(int n = 0; n<=x1;n++){
muiltiples3[n] = 3*n;
sum3 += muiltiples3[n];
}
for(int f = 0; f <= x2; f++ ){
muiltiples5[f] = 5*f;
sum5 += muiltiples5[f];
}
for(int p = 0; p <= x3; p++){
muiltiples15[p] = 15 * p;
sum15 += muiltiples15[p];
}
}
void draw(){
float sumTotal = sum3 + sum5- sum15;
text(sumTotal,50,50);
}
float y = 1000/5 -1;
float z = 1000/15;
int x1 = round(x);
int x2 = round(y);
int x3 = round(z);
float sum3 = 0;
float sum5 = 0;
float sum15 = 0;
int [] muiltiples3 = new int[x1+1];
int [] muiltiples5 = new int[x2+1];
int [] muiltiples15 = new int[x3+1];
void setup(){
size(1000,1000);
if(x1>x){
x1--;
}
if(x3>z){
x3--;
}
for(int n = 0; n<=x1;n++){
muiltiples3[n] = 3*n;
sum3 += muiltiples3[n];
}
for(int f = 0; f <= x2; f++ ){
muiltiples5[f] = 5*f;
sum5 += muiltiples5[f];
}
for(int p = 0; p <= x3; p++){
muiltiples15[p] = 15 * p;
sum15 += muiltiples15[p];
}
}
void draw(){
float sumTotal = sum3 + sum5- sum15;
text(sumTotal,50,50);
}