HACKER RANK Project Euler 01 - Multiples of 3 and 5 - Solution.'C'
Project Euler Challenges-01 - Multiples of 3 and 5 Solution
Problem Statement
This problem is a programming version of Problem 1 from projecteuler.net
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 .
Input Format
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N .
Output Format
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N .
Constraints
1≤T≤105
1≤N≤109
Sample Input
2
10
100
Sample Output
23
2318
Problem Statement
This problem is a programming version of Problem 1 from projecteuler.net
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 .
Input Format
First line containsT that denotes the number of test cases. This is followed by T lines, each containing an integer, N .
First line contains
Output Format
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 belowN .
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below
Constraints
1≤T≤105
1≤N≤109
Sample Input
2
10
100
Sample Output
23
2318
Solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
unsigned long long n,i,sum=0,t;
scanf("%llu",&t);
while(t--)
{
scanf("%lld",&n);
--n;
unsigned long long n1,n2,n3;
n1=n/3;
n2=n/5;
n3=n/15;
sum=(3*n1*(n1+1))/2+(5*n2*(n2+1))/2-(15*n3*(n3+1))/2;
printf("%lld\n",sum);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { unsigned long long n,i,sum=0,t; scanf("%llu",&t); while(t--) { scanf("%lld",&n); --n; unsigned long long n1,n2,n3; n1=n/3; n2=n/5; n3=n/15; sum=(3*n1*(n1+1))/2+(5*n2*(n2+1))/2-(15*n3*(n3+1))/2; printf("%lld\n",sum); } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Comments
Post a Comment