2020-06-06 00:20:25 +08:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Problem 1](https://projecteuler.net/problem=1) solution
|
2020-07-01 01:39:31 +08:00
|
|
|
* \details
|
2020-06-06 00:20:25 +08:00
|
|
|
* An Efficient code to print all the sum of all numbers that are multiples of 3
|
|
|
|
* & 5 below N.
|
|
|
|
*/
|
2018-10-28 01:02:02 +08:00
|
|
|
|
2017-11-27 16:00:21 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-06-06 00:20:25 +08:00
|
|
|
/** Main function */
|
2020-05-30 04:23:24 +08:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int t;
|
2019-06-11 01:18:42 +08:00
|
|
|
printf("Enter number of times you want to try");
|
2020-05-30 04:23:24 +08:00
|
|
|
scanf("%d", &t);
|
2020-06-28 23:25:37 +08:00
|
|
|
while (t--) // while t > 0, decrement 't' before every iteration
|
2018-10-28 01:02:02 +08:00
|
|
|
{
|
2020-05-30 04:23:24 +08:00
|
|
|
unsigned long long N, p = 0, sum = 0;
|
2019-06-11 01:18:42 +08:00
|
|
|
printf("Enter the value of N ");
|
2018-10-28 01:02:02 +08:00
|
|
|
|
2020-06-28 23:25:37 +08:00
|
|
|
scanf("%lld", &N); // Take input of N from user
|
2020-05-30 04:23:24 +08:00
|
|
|
p = (N - 1) / 3;
|
|
|
|
sum = ((3 * p * (p + 1)) / 2);
|
2017-11-27 16:00:21 +08:00
|
|
|
|
2020-05-30 04:23:24 +08:00
|
|
|
p = (N - 1) / 5;
|
|
|
|
sum = sum + ((5 * p * (p + 1)) / 2);
|
|
|
|
|
|
|
|
p = (N - 1) / 15;
|
|
|
|
sum = sum - ((15 * p * (p + 1)) / 2);
|
2020-06-28 23:25:37 +08:00
|
|
|
printf("%lld\n", sum); // print the sum of all numbers that are
|
|
|
|
// multiples of 3 & 5 below N
|
2018-10-28 01:02:02 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|