Deceptive numbers: Difference between revisions

Content added Content deleted
(→‎UNIX Shell: simplify)
(Added Lua)
Line 660: Line 660:
(91 259 451 481 703 1729 2821 2981 3367 4141)
(91 259 451 481 703 1729 2821 2981 3367 4141)
ok
ok
</pre>

=={{header|Lua}}==
Based on the second Wren sample, finds the numbers without needing large integers - see the talk page for more details.
<syntaxhighlight lang="lua">
do -- find deceptive numbers - repunits evenly divisible by composite numbers
-- see tha task talk page based on the second Wren sample

-- returns true if n is prime, false otherwise, uses trial division %
local function isPrime ( n )
if n < 3 then return n == 2
elseif n % 3 == 0 then return n == 3
elseif n % 2 == 0 then return false
else
local prime = true
local f, f2, toNext = 5, 25, 24
while f2 <= n and prime do
prime = n % f ~= 0
f = f + 2
f2 = toNext
toNext = toNext + 8
end
return prime
end
end

do -- task
local n, count = 47, 0
while count < 25 do
n = n + 2
if n % 3 ~= 0 and n % 5 ~= 0 and not isPrime( n ) then
local mp = 10
for p = 2, n - 1 do mp = ( mp * 10 ) % n end
if mp == 1 then
count = count + 1
io.write( string.format( " %5d", n ) )
if count % 10 == 0 then io.write( "\n" ) end
end
end
end
end

end
</syntaxhighlight>
{{out}}
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141
4187 5461 6533 6541 6601 7471 7777 8149 8401 8911
10001 11111 12403 13981 14701
</pre>
</pre>