Project Euler in C
All Files Functions Macros Pages
Functions
0001.c File Reference

Sum of the positive integers less than 1000 that are divisible by 3 or 5. More...

#include <stdio.h>

Go to the source code of this file.

Functions

int main (void)
 

Detailed Description

Sum of the positive integers less than 1000 that are divisible by 3 or 5.

https://projecteuler.net/problem=1

Definition in file 0001.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 8 of file 0001.c.

8 {
9 /*
10 * `int` is only guaranteed to go up to 2^15 - 1 = 32767, which is not
11 * enough. `long` is guaranteed to go up to 2^31 - 1 = 2147483647, which is
12 * sufficient since it is even greater than
13 * 1 + 2 + ... + 999 = 999*1000/2 = 499500.
14 */
15 long sum = 0;
16
17 int i;
18 for (i = 1; i < 1000; i++) {
19 if (i % 3 == 0 || i % 5 == 0) {
20 sum += i;
21 }
22 }
23
24 printf("%ld\n", sum);
25
26 return 0;
27}