42 lines
1.1 KiB
Text
42 lines
1.1 KiB
Text
with javascript_semantics
|
|
requires("1.0.2") -- (for in)
|
|
function vampire(atom v)
|
|
sequence res = {}
|
|
if v>=0 then
|
|
string vs = sprintf("%d",v)
|
|
if even(length(vs) then
|
|
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: %v\n",{found,i,res})
|
|
end if
|
|
i += 1
|
|
end while
|
|
puts(1,"\n")
|
|
|
|
for i in {16758243290880,24959017348650,14593825548650} do
|
|
res = vampire(i)
|
|
printf(1,"%d: %s\n",{i,iff(res={}?"not a vampire number":sprint(res))})
|
|
end for
|