RosettaCodeData/Task/Square-free-integers/Pluto/square-free-integers.pluto
2026-04-30 12:34:36 -04:00

36 lines
963 B
Text

local fmt = require "fmt"
local function is_square_free(n)
local i = 2
while i * i <= n do
if n % (i * i) == 0 then return false end
i = (i > 2) ? i + 2 : i + 1
end
return true
end
local ranges = { range(1, 145), range(1e12, 1e12 + 145) }
local params = { {3, 20}, {12, 5} }
for i = 1, #ranges do
local r = ranges[i]
print($"The square-free integers between {r[1]} and {r:back()} inclusive are:")
local count = 0
for r as j do
if is_square_free(j) then
++count
fmt.write($"%{params[i][1]}d ", j)
if count % params[i][2] == 0 then print() end
end
end
print("\n")
end
print("Counts of square-free integers:")
local count = 0
local lims = {0, 100, 1000, 1e4, 1e5, 1e6}
for i = 2, #lims do
for j = lims[i - 1] + 1, lims[i] do
if is_square_free(j) then ++count end
end
fmt.print(" from 1 to (inclusive) %-7d = %d", lims[i], count)
end