Perfect totient numbers: Difference between revisions

Content added Content deleted
(add task to aarch64 assembly raspberry pi)
(Added Lua)
Line 1,428: Line 1,428:
The first 20 perfect totient numbers are:
The first 20 perfect totient numbers are:
[3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571]
[3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571]
</pre>

=={{header|Lua}}==
<lang Lua>local function phi(n)
assert(type(n) == 'number', 'n must be a number!')
local result, i = n, 2
while i <= n do
if n % i == 0 then
while n % i == 0 do n = n // i end
result = result - (result // i)
end
if i == 2 then i = 1 end
i = i + 2
end
if n > 1 then result = result - (result // n) end
return result
end

local function phi_iter(n)
assert(type(n) == 'number', 'n must be a number!')
if n == 2 then
return phi(n) + 0
else
return phi(n) + phi_iter(phi(n))
end
end

local i, count = 2, 0
while count ~= 20 do
if i == phi_iter(i) then
io.write(i, ' ')
count = count + 1
end
i = i + 1
end
</lang>

{{output}}
<pre>
3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571
</pre>
</pre>