Task

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n.

Show output for n = 1000.

This is is the same as Project Euler problem 1.

Extra credit: do this efficiently for n = 1e20 or higher.




#include <stdio.h>
#include <stdlib.h>
 
unsigned long long sum35(unsigned long long limit)
{
    unsigned long long sum = 0;
    for (unsigned long long i = 0; i < limit; i++)
        if (!(i % 3) || !(i % 5))
            sum += i;
    return sum;
}
 
int main(int argc, char **argv)
{
    unsigned long long limit;
 
    if (argc == 2)
        limit = strtoull(argv[1], NULL, 10);
    else
        limit = 1000;
 
    printf("%lld\n", sum35(limit));
    return 0;
}