21 April 2020 : 1 new video added
L15 : Euler’s Totient Function
bruh I got to know about it last month and completed number theory
and today in April cookoff I am able to reach and think about the solution of decomposing matrix thanks @waqar_ahmad224
That’s really nice to hear.
you’re welcome buddy
working on it.
uploaded a video today
25 April 2020 : 1 new lecture added
L17 : Calculating Euler’s Totient Function in O(sqrt(N)) Time
Brother i tried to calculate Eule totient with the help of modulo multiplicative inverse but it gives me WA , can you please check where i m doing mistake??
Please help!!
#include<bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i–)
#define pb push_back
#define mp make_pair
#define vll vector
#define mod 1000000007LL
ll power(ll a, ll n, ll m) // using Binary Exponential in TC O(logn)
{
ll res=1;
while(n)
{
if(n%2)
res=(resa)%m ,n–;
else
a=(aa)%m ,n/=2;
}
return res;
}
ll Euler_totient(ll n)
{
// firstly calculate the Number of primes
vectorprimes;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
primes.pb(i);
while(n%i==0)
n=n/i;
}
}
if(n>1) // it means it still a prime number
primes.pb(n);
// now calulate the inverse modulo of the each primes and stores in the another array
vector<ll>inverse_of_primes;
for(auto x: primes)
{
inverse_of_primes.pb(power(x,mod-2,mod));
}
ll res=n;
for(int i=0;i<(int)primes.size();i++)
{
res=(res*(primes[i]-1)*inverse_of_primes[i])%mod;
}
return res;
}
int main() {
auto start = chrono::high_resolution_clock::now();
fio;
ll t=1;
//cin>>t;
while(t–)
{
ll n; cin>>n;
cout<<Euler_totient(n)<<"\n";
}
auto finish = chrono::high_resolution_clock::now();
cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
return 0;
}
you need to know you are not calculating phi(n) but phi(n) % mod.
second thing you are initializing res = n ,which is wrong for now because n is not n which was sent to this function because it has changed where you were calculating prime factorization (in first for loop you are dividing n).
so set res = n before first for loop
#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i–)
#define pb push_back
#define mp make_pair
#define vll vector
#define mod 1000000007LL
ll power(ll a, ll n, ll m) // using Binary Exponential in TC O(logn)
{
ll res=1;
while(n)
{
if(n%2)
res=(res * a)%m ,n--;
else
a=(a * a)%m ,n/=2;
}
return res;
}
ll Euler_totient(ll n)
{
// firstly calculate the Number of primes
vector<int> primes;
ll res=n;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
primes.pb(i);
while(n%i==0)
n=n/i;
}
}
if(n>1) // it means it still a prime number
primes.pb(n);
// now calulate the inverse modulo of the each primes and stores in the another array
vector<ll>inverse_of_primes;
for(auto x: primes)
{
inverse_of_primes.pb(power(x,mod-2,mod));
}
for(int i=0;i<(int)primes.size();i++)
{
res=(res*(primes[i]-1))%mod;
res = (res * inverse_of_primes[i]) % mod;
}
return res;
}
int main() {
auto start = chrono::high_resolution_clock::now();
fio;
ll t=1;
//cin>>t;
while(t--)
{
ll n; cin>>n;
cout<<Euler_totient(n)<<"\n";
}
auto finish = chrono::high_resolution_clock::now();
cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
return 0;
}
here is the correct code (it is still calculating phi(n) % mod)
this course is really helpful it helped me with many number theory concepts thank u
you’re welcome man
30 April 2020 : 2 new lecture added.
L18 : Calculating Euler Phi Function from 1 to N in O(Nlog(logN)) Time
E001 : Euler’s Totient Function | SPOJ | Number Theory
I was thinking the same
can you add Möbius inversion to your course plzz?
currently I am teaching Euler’s Totient Function.
Hopefully after that I will teach mobius inversion as well
Hey can you please how to find phi_Inverse(x) as it is solution of question this. I got the solution is phi_inverse(x) but dont know how to calculate it for given constraint.It will of great help.
I will make video lecture on this
for now you can read this : number theory - inversion of the Euler totient function - Mathematics Stack Exchange
this answer is really great
//if we want to calculate all divisior of a number i sqrt(n)
#include<bits/stdc++.h>
using namespace std;
int divisior(int n)
{
int res=n;
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
res=res*(i-1);
res=res/i;
while(n%i==0)
n=n/i;
}
}
if(n>1)
{
res=res*(n-1);
res=res/n;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–>0)
{
int n;
cin>>n;
int l=divisior(n);
cout<<l<<endl;
}
}
why spoj give wrong answer EFT problem
this should be a compilation error
it should be
while(t--)
or
while(t-->0)