Project Euler #1: 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.

 

Solution in Python

total = 0;
for i in range(0,1000):
    if (i%5 == 0):
        total += i;
    else:
        if (i%3 == 0):
            total +=i; 
print total;

Solution in C

#include <stdio.h>

void main(){
    int i, total;
    for(i = 0; i < 1000; i++){
        if(i%3 == 0){
            total += i;
        }
        else if(i%5 == 0){
            total += i;
        }
    }
    printf("Total: %d", total);
}

Solution in C++

#include <iostream>

int main(void)
{
	unsigned long long int total;

	for (int i = 999; i > 0; i--){
		if(i%5 == 0 || i%3 == 0)
		{
			total+=i;
		}
	}
	std::cout << "Total: " << total;
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *