RosettaCodeData/Task/Brazilian-numbers/Pluto/brazilian-numbers.pluto
2026-04-30 12:34:36 -04:00

54 lines
1.1 KiB
Text

local int = require "int"
local function same_digits(n, b)
local f = n % b
n //= b
while n > 0 do
if n % b != f then return false end
n //= b
end
return true
end
local function is_brazilian(n)
if n < 7 then return false end
if n % 2 == 0 and n >= 8 then return true end
for b = 2, n - 2 do
if same_digits(n, b) then return true end
end
return false
end
for {" ", " odd ", " prime "} as kind do
print($"First 20{kind}Brazilian numbers:")
local c = 0
local n = 7
while true do
if is_brazilian(n) then
io.write($"{n} ")
c += 1
if c == 20 then
print("\n")
break
end
end
if kind == " " then
n += 1
elseif kind == " odd " then
n += 2
else
while true do
n += 2
if int.isprime(n) then break end
end
end
end
end
local c = 0
local n = 7
while c < 1e5 do
if is_brazilian(n) then c += 1 end
n += 1
end
print($"The 100,000th Brazilian number: {n - 1}")