Skip to content

Commit

Permalink
Added Euler totient function (jainaman224#779)
Browse files Browse the repository at this point in the history
Updated changes
  • Loading branch information
MastersAbh authored and jainaman224 committed Mar 25, 2019
1 parent 169103a commit 79b6177
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Euler_Totient_Function/Euler_Totient_Function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Euler Totient Function.
ETF (Euler Totient Function) of a number n is defined as the number of positive integers
less than n, which are co-prime to n, i.e.
ETF(n) = frequency of x where gcd(x,n) = 1 and 1<=x<=n.
Brute force approach : Iterate over all elements < n and check if it's co-prime to n.
Our Approach : We use formula - ETF(n) = (1- 1/p1)*(1 - 1/p2).. where p1, p2 etc are
prime factors of n.
*/
#include <bits/stdc++.h>
using namespace std;
#define lint long long int

double Etf(lint num){
double ans = num;
/*
In this loop, we check for all prime factors of num and keep removing the
multiples of those prime factors from our scope of search and thus, with each
iteration, num gets reduced by an order of that factor.
*/
for(lint i=2; i <= sqrt(num); i++){
if(!(num%i)){
ans *= (1 - (1.0/i)); // Using the ETF formula.
while(!(num%i)){
num/=i;
}
}
}
ans = (( num > 1 ) ? (ans * (1 - (1.0/num))) : (ans));
// If there's any element left which is > sqrt(n), it can contribute maximum once.
return ans;
}

int main()
{
lint num = 34;
double etf = Etf(num);
cout<<"ETF of "<<num<<" is "<<(int)etf;
}
/*
Input : num = 34.
Output: ETF of 34 is 16
Verification : 1,3,5,7,9,11,13,15,19,21,23,25,27,29,31,33 are the numbers whose gcd with 34 is 1.
*/

54 changes: 54 additions & 0 deletions Euler_Totient_Function/Euler_Totient_Function.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Euler Totient Function.
ETF (Euler Totient Function) of a number n is defined as the number of positive integers
less than n, which are co-prime to n, i.e.
ETF(n) = frequency of x where gcd(x,n) = 1 and 1<=x<=n.
Brute force approach : Iterate over all elements < n and check if it's co-prime to n.
Our Approach : We use formula - ETF(n) = (1- 1/p1)*(1 - 1/p2).. where p1, p2 etc are
prime factors of n.
*/

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.awt.Point;

public class Solution {
public static double Etf(long num){
double ans = num;
/*
In this loop, we check for all prime factors of num and keep removing the
multiples of those prime factors from our scope of search and thus, with each
iteration, num gets reduced by an order of that factor.
*/
for(long i=2; i <= Math.sqrt(num); i++){
if(num%i == 0){
ans *= (1 - (1.0/i)); // Using the ETF formula.
while(num%i == 0){
num/=i;
}
}
}
ans = (( num > 1 ) ? (ans * (1 - (1.0/num))) : (ans));
// If there's any element left which is > sqrt(n), it can contribute maximum once.
return ans;
}

public static void main(String[] args) {
long num = 34;
double etf = Etf(num);
System.out.print("ETF of "+num+" is "+(int)etf);
}
}
/*
Input : num = 34.
Output: ETF of 34 is 16
Verification : 1,3,5,7,9,11,13,15,19,21,23,25,27,29,31,33 are the numbers whose gcd with 34 is 1.
*/
41 changes: 41 additions & 0 deletions Euler_Totient_Function/Euler_Totient_Function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''
Euler Totient Function.
ETF (Euler Totient Function) of a number n is defined as the number of positive integers
less than n, which are co-prime to n, i.e.
ETF(n) = frequency of x where gcd(x,n) = 1 and 1<=x<=n.
Brute force approach : Iterate over all elements < n and check if it's co-prime to n.
Our Approach : We use formula - ETF(n) = (1- 1/p1)*(1 - 1/p2).. where p1, p2 etc are
prime factors of n.
'''
import math
def Etf(num):
ans = num
'''
In this loop, we check for all prime factors of num and keep removing the
multiples of those prime factors from our scope of search and thus, with each
iteration, num gets reduced by an order of that factor.
'''
for i in range(2,int(math.sqrt(num)+1)):
if(not num%i):
ans = ans * (1 - (1.0/i)) # Using the ETF formula.
while not num%i:
num = num/i
ans = ((ans * (1 - (1.0/num))) if ( num > 1 ) else (ans))
# If there's any element left which is > sqrt(n), it can contribute maximum once.
return ans

num = 34
etf = int(Etf(num))
print("ETF of",num,"is",etf)


'''
Input : num = 34.
Output: ETF of 34 is 16
Verification : 1,3,5,7,9,11,13,15,19,21,23,25,27,29,31,33 are the numbers whose gcd with 34 is 1.
'''

0 comments on commit 79b6177

Please sign in to comment.