Skip to content

Commit 2cc7ba7

Browse files
Add : Project Euler #1 Multiples of 3 and 5 (Hackerrank)
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.
1 parent 19b9245 commit 2cc7ba7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Project Euler 1.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
long long sum_n(long long n,long long d)
7+
{
8+
long long x= (n*(n+1)*d) /2;
9+
return x;
10+
}
11+
12+
int main(){
13+
int t;
14+
long long ans,temp,x,y,z;
15+
cin >> t;
16+
for(int a0 = 0; a0 < t; a0++){
17+
int n;
18+
cin >> n;
19+
n--;
20+
temp= (n-(n%3))/3;
21+
x= sum_n(temp,3);
22+
temp= (n-(n%5))/5;
23+
y= sum_n(temp,5);
24+
temp= (n-(n%15))/15;
25+
z=sum_n(temp,15);
26+
//cout<<x<<" ";cout<<y<<" ";cout<<z<<" ";
27+
cout<<x+y-z<<endl;
28+
}
29+
return 0;
30+
}

0 commit comments

Comments
 (0)