Skip to content

Commit 33d00bb

Browse files
committed
Add phi() to the Util package
1 parent 455d8c6 commit 33d00bb

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

julia/Euler/src/lib/util.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Primes: factor, primes
55

66
export is_palindrome, is_pandigital, is_pandigital_nz
77
export is_triangular, is_square, is_pentagonal, is_hexagonal
8-
export divisors, proper_divisors
8+
export divisors, proper_divisors, phi
99
export get_σ_tbl, get_max_exp, undigits
1010
export with_replacement_permutations
1111

@@ -91,6 +91,35 @@ function proper_divisors(num)
9191
divisors(num)[1:end-1]
9292
end
9393

94+
function phi(n)
95+
ret = n
96+
97+
if n % 2 == 0
98+
ret -= div(ret, 2)
99+
while n % 2 == 0
100+
n = div(n, 2)
101+
end
102+
end
103+
104+
i = 3
105+
while i * i <= n
106+
if n % i == 0
107+
ret -= div(ret, i)
108+
while n % i == 0
109+
n = div(n, i)
110+
end
111+
end
112+
113+
i += 2
114+
end
115+
116+
if n > 1
117+
ret -= div(ret, n)
118+
end
119+
120+
ret
121+
end
122+
94123
# https://en.wikipedia.org/wiki/Divisor_function
95124
function get_σ_tbl(z, upper)
96125
p_lst = primes(upper)

0 commit comments

Comments
 (0)