Perfect totient number
Last Updated :
16 Jul, 2021
A Perfect totient number is an integer that is equal to the sum of its iterated totients. Perfect totient number is denoted by .
For example:
, Now, , and 9 == 6 + 2 + 1, Therefore, 9 is a perfect totient number.
Check if N is a Perfect totient number
Given an integer N, the task is to check N is a Perfect totient number.
Examples:
Input: N = 9
Output: Yes
Input: N = 10
Output: No
Approach: The idea is to find the Euler Totient Value of the given number, suppose we get the Euler Totient Value of N as V, then we will again find the Euler Totient Value of V until the new Euler Totient Value V becomes 1. We will also keep the sum of all the Euler Totient Value values V we got till now and check if the sum is equal to N or not. If equal then the given number is a perfect Euler Totient number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int phi( int n)
{
int result = n;
for ( int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
int isPerfectTotientNum( int n)
{
int temp = n;
int sum = 0;
while (n > 1){
sum = sum + phi(n);
n = phi(n);
}
if (sum == temp)
return true ;
return false ;
}
int main()
{
int n = 9;
if (isPerfectTotientNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static int phi( int n)
{
int result = n;
for ( int p = 2 ; p * p <= n; ++p)
{
if (n % p == 0 )
{
while (n % p == 0 )
{
n /= p;
}
result -= result / p;
}
}
if (n > 1 )
result -= result / n;
return result;
}
static boolean isPerfectTotientNum( int n)
{
int temp = n;
int sum = 0 ;
while (n > 1 )
{
sum = sum + phi(n);
n = phi(n);
}
if (sum == temp)
return true ;
return false ;
}
public static void main(String[] args)
{
int n = 9 ;
if (isPerfectTotientNum(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def phi(n):
result = n
for p in range ( 2 , n):
if p * p > n:
break
if (n % p = = 0 ):
while (n % p = = 0 ):
n / / = p
result - = result / / p
if (n > 1 ):
result - = result / / n
return result
def isPerfectTotientNum(n):
temp = n
sum = 0
while (n > 1 ):
sum = sum + phi(n)
n = phi(n)
if ( sum = = temp):
return True
return False
if __name__ = = '__main__' :
n = 9
if (isPerfectTotientNum(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int phi( int n)
{
int result = n;
for ( int p = 2; p * p <= n; ++p)
{
if (n % p == 0)
{
while (n % p == 0)
{
n /= p;
}
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
static bool isPerfectTotientNum( int n)
{
int temp = n;
int sum = 0;
while (n > 1)
{
sum = sum + phi(n);
n = phi(n);
}
if (sum == temp)
return true ;
return false ;
}
public static void Main()
{
int n = 9;
if (isPerfectTotientNum(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function phi( n)
{
let result = n;
for ( let p = 2; p * p <= n; ++p)
{
if (n % p == 0)
{
while (n % p == 0)
{
n = parseInt(n/p);
}
result -= parseInt(result / p);
}
}
if (n > 1)
result -= parseInt(result / n);
return result;
}
function isPerfectTotientNum( n) {
let temp = n;
let sum = 0;
while (n > 1) {
sum = sum + phi(n);
n = phi(n);
}
if (sum == temp)
return true ;
return false ;
}
let n = 9;
if (isPerfectTotientNum(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(n1/2)
Auxiliary Space: O(1)
References: https://en.wikipedia.org/wiki/Perfect_totient_number
Share your thoughts in the comments
Please Login to comment...