- Joined
- Apr 24, 2012
- Messages
- 5,111
JASS:
library Commons // by Almia
// Determines the greatest common divisor.
function GCD takes integer a, integer b returns integer
local integer n
if b == 0 then
return a
endif
loop
set n = a
set a = b
set b = ModuloInteger(n, a)
if b == 0 then
return a
endif
endloop
return 0
endfunction
// Gets the lowest common multiple
function LCM takes integer a, integer b returns integer
local integer n = GCD(a, b)
if n == 0 then
return 0
endif
return a*b/n
endfunction
// Checks if two integers are relatively prime to each other
function IsCoprime takes integer a, integer b returns integer
return GCD(a, b) == 1
endfunction
/*
Gets the totatives between 0 and n, that is the positive integers
less than or equal to n that are relatively prime to n.
*/
function Totient takes integer n returns integer
local integer count = 0
local integer i = 1
if n < 0 then
return 0
endif
loop
if IsCoprime(n, i) then
set count = count + 1
endif
set i = i + 1
exitwhen i == n
endloop
return count + 1
endfunction
endlibrary
Last edited: