RosettaCodeData/Task/Vampire-number/Phix/vampire-number.phix
2019-09-12 10:33:56 -07:00

45 lines
1.2 KiB
Text

function vampire(atom v)
sequence res = {}
if v>=0 then
string vs = sprintf("%d",v)
if mod(length(vs),2)=0 then -- even length
vs = sort(vs)
for i=power(10,length(vs)/2-1) to floor(sqrt(v)) do
if remainder(v,i)=0 then
integer i2 = v/i
string si = sprintf("%d",i),
s2 = sprintf("%d",i2)
if (si[$]!='0' or s2[$]!='0')
and sort(si&s2)=vs then
res = append(res,{i,i2})
end if
end if
end for
end if
end if
return res
end function
integer found = 0
atom i = 0
sequence res
puts(1,"The first 26 vampire numbers and their fangs:\n")
while found<26 do
res = vampire(i)
if length(res) then
found += 1
printf(1,"%d: %d: %s\n",{found,i,sprint(res)})
end if
i += 1
end while
puts(1,"\n")
constant tests = {16758243290880,
24959017348650,
14593825548650}
for t=1 to length(tests) do
i = tests[t]
res = vampire(i)
printf(1,"%d: %s\n",{i,iff(res={}?"not a vampire number":sprint(res))})
end for