Skip to content

Commit b650126

Browse files
committed
Euler totient with Bytecode approach
1 parent ae19f0d commit b650126

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

euler_totient.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<map>
4+
using namespace std;
5+
#define MAX 1000005
6+
long long phi[MAX];
7+
void generatePhi()
8+
{
9+
10+
phi[1]=1;
11+
for(int i=2; i<MAX; i++)
12+
{
13+
if(phi[i]==0)
14+
{
15+
phi[i]=i-1;
16+
for(int j=(i<<1);j<MAX;j=j+i)
17+
{
18+
if(!phi[j])
19+
phi[j]=j;
20+
21+
phi[j]=(phi[j]/i)*(i-1);
22+
}
23+
}
24+
}
25+
}
26+
27+
long long solutions[MAX];
28+
int main()
29+
{
30+
generatePhi();
31+
32+
33+
for(int i=1; i<MAX; i++)
34+
{
35+
for(int j=2; i*j<MAX; j++)
36+
solutions[i*j] += i*phi[j];
37+
}
38+
39+
for(int i=2; i<MAX; i++)
40+
{
41+
solutions[i] += solutions[i-1];
42+
}
43+
44+
int t;
45+
cin>>t;
46+
while(t--)
47+
{
48+
int n;
49+
cin>>n;
50+
cout<<solutions[n]<<endl;
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)