Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol2.c File Reference

Problem 1 solution More...

#include <stdio.h>
Include dependency graph for sol2.c:

Functions

int main ()
 Main function.
 

Detailed Description

Problem 1 solution

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 N.

This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.

Function Documentation

◆ main()

int main ( void  )

Main function.

16{
17 int n = 0;
18 int sum = 0;
19 scanf("%d", &n);
20
21 int terms = (n - 1) / 3;
22 sum += ((terms) * (6 + (terms - 1) * 3)) / 2; // sum of an A.P.
23 terms = (n - 1) / 5;
24 sum += ((terms) * (10 + (terms - 1) * 5)) / 2;
25 terms = (n - 1) / 15;
26 sum -= ((terms) * (30 + (terms - 1) * 15)) / 2;
27
28 printf("%d\n", sum);
29}