Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
36 views29 pages

Euler - S Phi Algorithm.2025

Euler_s phi Algorithm.2025

Uploaded by

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

Euler - S Phi Algorithm.2025

Euler_s phi Algorithm.2025

Uploaded by

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

TEST TIME ON SEGMENTED AND INCREMENTAL SIEVE

URL:https://forms.gle/geq3XgXnt7WCk3qMA

QR CODE:
EULER’S PHI
ALGORITHM
TOPICS

⮚ Introduction

⮚ Examples

⮚ Properties

⮚ Coding

⮚ Interview Questions
INTRODUCTION
Euler's totient function (also called phi-function or totient function) takes a single positive
integer n as input and outputs the number of integers present between 1 and n that are
co-prime to n.
Note:
• 2 positive integers a and b are said to be co-prime if their greatest common
factor/divisor is equal to 1, that is,
gcd(a,b)=1
• 1 is considered co-prime to all numbers.
Example:1
Find Փ(5)
Solution
Here n=5
Numbers less than 5 are 1,2,3 and 4
Euler’s Phi Algorithm

GCD Relatively Prime?

GCD(1,5)=1

GCD(2,5)=1

GCD(3,5)=1

GCD(4,5)=1

there are 4 numbers less than 5 that are co-prime to it


Therefore Փ(5)=4
Example:2
Find Փ(11)
Solution
Here n=11
Numbers less than 11 are 1,2,3,4,5,6,7,8,9 and 10
Euler’s Phi Algorithm

GCD Relatively GCD Relatively


Prime? Prime?

GCD(1,11)=1 GCD(6,11)=1
GCD(2,11)=1 GCD(7,11)=1
GCD(3,11)=1 GCD(8,11)=1
GCD(4,11)=1 GCD(9,11)=1
GCD(5,11)=1 GCD(10,11)=
1
there are 10 numbers less than 11 that are co-prime to it
Therefore Փ(11)=10
Example:3
Find Փ(8)
Solution
Here n=8
Numbers less than 8 are 1,2,3,4,5,6 and 7
Euler’s Phi Algorithm

GCD Relatively GCD Relatively


Prime? Prime?

GCD(1,8)=1 GCD(5,8)=1

GCD(2,8)=2 GCD(6,8)=2

GCD(3,8)=1 GCD(7,8)=1

GCD(4,8)=4

there are 4 numbers less than 8 that are co-prime to it


Therefore Փ(8)=4
Properties

Criteria of 'n' Formula

'n' is prime. Φ(n) = (n-1)

Φ(n) n=pxq Φ(n) = (p-1) × (q-1)


'p' and 'q' are primes.

n = a x b.
Either 'a' or 'b’ is composite.
Both 'a' and 'b' are composite.
Property 1

Criteria of 'n' Formula


Φ(n)
'n' is prime. Φ(n) = (n-1)

Proof
Find Φ(7)
Here n=7
‘n’ is a prime number
Φ(n) = (n-1)
Φ(7) = (7-1)
Φ(7) = 6
there are 6 numbers less than 7 that are co-prime to it
Therefore Փ(7)=6
Property 2

Criteria of 'n' Formula

Φ(n) n=pxq Φ(n) = (p-1) × (q-1)


'p' and 'q' are primes.

Proof
Find Φ(35)
Here n=35
‘n’ is a product of two prime numbers 5 and 7
Let us assign p=5 and q=7.
Φ(n)=(p-1) × (q-1)
Φ(35)=(5-1) × (7-1)
=4x6
= 24
So, there are 24 numbers that are lesser than 35 and relatively prime to 35.
Therefore Փ(35)=24
Property 3

Criteria of 'n' Formula


Φ(n) n = a x b.
Either 'a' or 'b’ is composite.
Both 'a' and 'b' are composite.
Basic Algorithm

A simple solution is to iterate through all numbers from 1 to n-1 and count numbers with gcd with n

import java.io.*; if (gcd(i, n) == 1)


class Main { result++;
// Function to return GCD of a and b return result;
static int gcd(int a, int b) }
{ public static void main(String[] args)
if (a == 0) {
return b; int n=5;
return gcd(b % a, a); //Finding Phi for input n
}
static int phi(int n) System.out.println(phi(n));
{ }
int result = 1; }
for (int i = 2; i < n; i++)

Time Complexity: O(N log N)


Better Solution

Algorithm
1) Initialize: result = n
2) Run a loop from 'p' = 2 to sqrt(n), do following for every 'p'.
a) If p divides n, then
Set: result = result * (1.0 - (1.0 / (float) p));
Divide all occurrences of p in n.
3) Return the result
import java.io.*;
class Main {
static int phi(int n) {
float result = n;
for (int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result *= (1.0 - (1.0 / (float) p));
}
}
if (n > 1)
result -= result / n;
return (int) result;
}
public static void main(String args[]) {
int n = 35;
System.out.println("phi(" + n + ") = " + phi(n));
}
}
Better Solution - 2

We can avoid floating-point calculations in the above method. The idea is to count all
prime factors and their multiples and subtract this count from n to get the totient
function value (Prime factors and multiples of prime factors won’t have GCD as 1)

Algorithm
1) Initialize result as n
2) Consider every number 'p' (where 'p' varies from 2 to Φn).
If p divides n, then do following
a) Subtract all multiples of p from 1 to n [all multiples of p will have gcd more than 1
(at least p) with n]
b) Update n by repeatedly dividing it by p.
3) If the reduced n is more than 1, then remove all multiples of n from result.
Program
import java.io.*;
class Main
{
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;
}
public static void main (String[] args)
{
int n=1000;
System.out.println(phi(n));
}
}
INTERVIEW
QUESTIONS
Interview questions

What is Euler's Phi algorithm?

Answer: Euler's Phi algorithm is a mathematical algorithm used to calculate the totient

function of a given number. The totient function (φ(n)) is the number of positive integers

less than or equal to n that are relatively prime to n.


Interview questions

What is the time complexity of Euler's Phi algorithm?

Answer: The time complexity of Euler's Phi algorithm is O(sqrt(n) log n), where n is the
input number.
Interview questions

What are some applications of Euler's Phi algorithm?

Answer: Euler's Phi algorithm has several applications in number theory and cryptography.
It is used in RSA encryption and decryption, as well as in the generation of keys for public
key cryptography. It is also used in determining the order of an element in a group, and in
solving the discrete logarithm problem.
Interview questions

Can Euler's Phi algorithm be used for large numbers?

Answer: Euler's Phi algorithm can be used for large numbers, but it may become

computationally expensive due to the prime factorization step. In some cases, it may be

necessary to use more advanced algorithms for calculating the totient function of very

large numbers.
Interview questions

What is the significance of Euler's Phi function?

Answer: Euler's Phi function is significant in number theory and cryptography as it is used

in many mathematical proofs and algorithms. It has applications in determining the relative

primality of two numbers, generating RSA keys, and solving the discrete logarithm

problem, among other things.


Interview questions
How does Euler's Phi algorithm differ from the Sieve of Eratosthenes?

Answer: Euler's Phi algorithm and the Sieve of Eratosthenes are two different algorithms
used in number theory. The Sieve of Eratosthenes is used to find all the prime numbers up
to a given limit, while Euler's Phi algorithm is used to calculate the totient function of a
given number. The two algorithms have different purposes and use different methods to
accomplish their respective tasks.
Practice questions

Question 1: Understanding Euler's Totient Function

Explain the concept of Euler's Totient Function (φ) and its significance in number theory.

How does the provided Java code calculate Euler's Totient Function for a given integer `n`?

Provide a step-by-step explanation of the algorithm used in the code.


Practice questions

Question 2: Analyzing Time Complexity

Analyze the time complexity of the `phi` method in the provided Java code. Consider the

worst-case scenario and discuss how the time complexity is determined based on the input

value `n`. Suggest any possible optimizations to improve the time complexity of the

algorithm.
Practice questions

Question 3: Handling Large Inputs

Discuss the limitations of the provided code when dealing with large input values of `n`.

Identify potential issues that may arise due to the data type used for the `result` variable

in the `phi` method. Propose alternative approaches to handle large inputs efficiently while

ensuring accuracy in calculating Euler's Totient Function.


Practice questions

Question 4: Write a Java program to calculate Euler's Totient Function (φ) for a given integer

input n. Euler's Totient Function φ(n) for an input n is defined as the count of positive

integers less than or equal to n that are coprime to n.


THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like