Skip to content

Commit fea167e

Browse files
committed
Added example of Euler Totient to Math directory
1 parent 72a0e4e commit fea167e

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

Math/EulerTotient.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public int EulerTotient( int number )
2+
{
3+
int k, divisor;
4+
int gcd = 1;
5+
int totiatives = 0;
6+
7+
// Check all numbers, k, inclusively between 1 and number
8+
for( k = 1; k <= number; k++ )
9+
{
10+
11+
// Check all divisors of k
12+
for( divisor = 1; divisor < k; divisor++ )
13+
{
14+
15+
// Find greatest common denominator of (n, k)
16+
if( k % divisor == 0 && number % divisor == 0 )
17+
{
18+
gcd = divisor;
19+
}
20+
}
21+
22+
// If gcd is relatively prime, we have found a totient
23+
if( gcd == 1 )
24+
{
25+
totiatives++;
26+
}
27+
28+
}
29+
30+
return totiatives;
31+
}

Math/EulerTotientExample.class

446 Bytes
Binary file not shown.

Math/EulerTotientExample.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
public int EulerTotient( int number )
3+
{
4+
int k, divisor;
5+
int gcd = 1;
6+
int totiatives = 0;
7+
8+
// Check all numbers, k, inclusively between 1 and number
9+
for( k = 1; k <= number; k++ )
10+
{
11+
12+
// Check all divisors of k
13+
for( divisor = 1; divisor < k; divisor++ )
14+
{
15+
16+
// Find greatest common denominator of (n, k)
17+
if( k % divisor == 0 && number % divisor == 0 )
18+
{
19+
gcd = divisor;
20+
}
21+
}
22+
23+
// If gcd is relatively prime, we have found a totient
24+
if( gcd == 1 )
25+
{
26+
totiatives++;
27+
}
28+
29+
}
30+
31+
return totiatives;
32+
}

0 commit comments

Comments
 (0)