-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy patheuler-totient_better_complexity.cpp
84 lines (74 loc) · 1.15 KB
/
euler-totient_better_complexity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
//to calculate prime numbers which are stored in prime vector
int sieve[1000000];
vector<int> prime;
void init()
{
for(int i=0;i<1000000;i++)
{
sieve[i]=0;
}
}
void getprime()
{
int i,j;
for(i=2;i*i<1000000;i++)
{
if(!sieve[i])
{
for(j=i*i;j<1000000;j=j+i)
{
sieve[j]=1;
}
}
}
for(i=2;i<1000000;i++)
{
if(!sieve[i])
prime.push_back(i);
}
//cout<<prime.size();
}
// totient function
int totientfunc(int n)
{
int i;
int ans=n;
for(i=0;prime[i]*prime[i]<=n && i<prime.size();i++)
{
if(n%prime[i]==0)
{
ans=ans*(1-1.0/prime[i]);
while(n%prime[i]==0)
{
n=n/prime[i];
}
}
}
if(n>1)
{
ans=ans*(1-1.0/n);
}
return ans;
}
// driver code to run the euler totient function and get the prime numbers
int main() {
fastIO;
init();
getprime();
int n,i,j;
int t,q;
for(i=1;i<=10;i++)
{
cout<<totientfunc(i)<<endl;
}
return 0;
}