Square-free integers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: square_free() now a builtin)
Line 1,773: Line 1,773:


=={{header|Phix}}==
=={{header|Phix}}==
<lang Phix>function square_free(atom start, finish)
<lang Phix>function square_frees(atom start, finish)
sequence res = {}
sequence res = {}
if start=1 then res = {1} start = 2 end if
if start=1 then res = {1} start = 2 end if
integer maxprime = get_maxprime(finish)
while start<=finish do
while start<=finish do
sequence pf = prime_factors(start, duplicates:=true)
if square_free(start,maxprime) then
for i=2 to length(pf) do
if pf[i]=pf[i-1] then
pf = {}
exit
end if
end for
if pf!={} then
res &= start
res &= start
end if
end if
Line 1,791: Line 1,785:
return res
return res
end function
end function

function format_res(sequence res, string fmt)
function format_res(sequence res, string fmt)
for i=1 to length(res) do
for i=1 to length(res) do
Line 1,798: Line 1,792:
return res
return res
end function
end function

constant ONE_TRILLION = 1_000_000_000_000
constant ONE_TRILLION = 1_000_000_000_000

procedure main()
procedure main()
sequence res = square_free(1,145)
sequence res = square_frees(1,145)
printf(1,"There are %d square-free integers from 1 to 145:\n",length(res))
printf(1,"There are %d square-free integers from 1 to 145:\n",length(res))
puts(1,join_by(format_res(res,"%4d"),1,20,""))
puts(1,join_by(format_res(res,"%4d"),1,20,""))
res = square_free(ONE_TRILLION,ONE_TRILLION+145)
res = square_frees(ONE_TRILLION,ONE_TRILLION+145)
printf(1,"\nThere are %d square-free integers from %,d to %,d:\n",
printf(1,"\nThere are %d square-free integers from %,d to %,d:\n",
{length(res),ONE_TRILLION, ONE_TRILLION+145})
{length(res),ONE_TRILLION, ONE_TRILLION+145})
Line 1,814: Line 1,808:
for i=2 to 6 do
for i=2 to 6 do
integer lim = power(10,i),
integer lim = power(10,i),
len = length(square_free(1,lim))
len = length(square_frees(1,lim))
printf(1," from %,d to %,d = %,d\n", {1,lim,len})
printf(1," from %,d to %,d = %,d\n", {1,lim,len})
end for
end for