Ruby, 101 86 91 90 bytes
A Ruby port of my Actually answermy Actually answer. Golfing suggestions welcome.
Edit: -4 bytes from removing a
but +9 bytes from fixing a bug where 1
returned nil
. -1 byte thanks to Cyoce.
require'prime'
->n{((n%8<1?n/2:n).prime_division<<[2,1]).map{|x,y|x**~-y*~-x}.reduce :lcm}
Ungolfing
require 'prime'
def carmichael(n)
if n%8 < 1
n /= 2
end
a = []
n.prime_division.do each |x,y|
a << x**(y-1)*(x-1)
end
return a.reduce :lcm
end