Docs


Problem 1: Multiples of 3 and 5

Problem

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

/*
 * Multiples of 3 or 5
 * Problem 1 <https://projecteuler.net/problem=1>
 *
 * Project Euler
 *
 * By Ankit R Gadiya
 */

#include <stdio.h>

int sum_of_multiple (int num, int up);

int main (void)
{
	int sum = 0;

	sum = sum_of_multiple (3, 1000);
	sum += sum_of_multiple (5, 1000);
	sum -= sum_of_multiple (15, 1000);
	printf("Result: %i\n", sum);

	return 0;
}

int sum_of_multiple (int num, int up)
{
	int counter = 1, sum = 0;
	while (counter * num < up) {
		sum += counter * num;
		counter++;
	}

	return sum;
}

prob1.c

Result

233168