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.
program language (c++,python)
in c++:
int zbir = 0;
int b = 8456;
int i;
int m;
int n;
void setup() {
// put your setup code here, to run onc
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
long suma=0;
Serial.println (i);
for (i =0;i<b;i=i+3) {
suma+=i;
}
for (m =0;m<b;m=m+5) {
suma+=m;
}
for (n =15;n<b;n+=15) {
suma-=n;
}
Serial.println (suma);
Serial.readString();
}
in python:
suma,tri,pet = 0,0,0
b=1000
for i in range (1,b):
if i % 3 == 0:
suma+=i
for m in range (1,b):
if m % 5 == 0:
suma+=m
for n in range (1,b):
if n % 15 == 0:
suma-=n
print (suma)
first problem is done 🙂 .
Problem 2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.