0% found this document useful (0 votes)
133 views2 pages

Pbinfo

This document contains code to calculate the Euler totient function (φ) of a number n. It first defines a function prim() that checks if a number is prime. The Phi() function then checks if n is prime, and if so returns n-1. Otherwise, it iteratively calculates (d-1) * d^(p-1) for each prime factor d of n with power p, and multiplies the results to obtain the Euler totient function value.

Uploaded by

Zicu Nicu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views2 pages

Pbinfo

This document contains code to calculate the Euler totient function (φ) of a number n. It first defines a function prim() that checks if a number is prime. The Phi() function then checks if n is prime, and if so returns n-1. Otherwise, it iteratively calculates (d-1) * d^(p-1) for each prime factor d of n with power p, and multiplies the results to obtain the Euler totient function value.

Uploaded by

Zicu Nicu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#36 i_prim pbinfo

bool prim(int n){


int d = 2, cnt = 1;
while(n > 1){
int p = 0;
while(n % d == 0)
n/=d, p++;
cnt *= (p + 1);
d++;
if(d * d > n)
d = n;
}
return cnt == 2;
}
int i_prim(int n){
int p1 = n, p2 = n;
while(!prim(p1))
p1--;
while(!prim(p2))
p2++;
return p2 - p1;
}

-----------------------------------------------------------------------------------
---------------------------
#2642 phi

#include <bits/stdc++.h>
bool prim(int n)
{
int cnt=0;
for(int i = 1 ; i * i <= n ; ++i)
if(n % i == 0 && i * i != n)
cnt+=2;
else if(i * i == n)
cnt++;
if(cnt == 2)
return 1;
return 0;
}
int Phi(int n)
{
if(prim(n))
return n-1;
int d=2;
int rez = 1;
while(n>1)
{
int p = 0;
while(n % d==0)
n/=d , p++;
if(p != 0)
rez = rez * (d-1) * pow(d , p - 1);
d++;
if(d * d > n)
d = n;
}
return rez;
}

You might also like