Jump to content

Next highest int from digits: Difference between revisions

Line 466:
 
=={{header|Phix}}==
===algorithm 21===
<lang Phix>function nigh(string n)
sequence p = repeat("",factorial(length(n)))
for i=1 to length(p) do
p[i] = permute(i,n)
end for
p = sort(p)
integer k = rfind(n,p)
return iff(k=length(p)?"0",p[k+1])
end function
constant tests = {"0","9","12","21","12453",
"738440","45072010","95322020"}
-- (crashes on) "9589776899767587796600"}
atom t0 = time()
for i=1 to length(tests) do
string t = tests[i]
printf(1,"%22s => %s\n",{t,nigh(t)})
end for
?elapsed(time()-t0)</lang>
{{out}}
<pre>
0 => 0
9 => 0
12 => 21
21 => 0
12453 => 12534
738440 => 740348
45072010 => 45072100
95322020 => 95322200
"0.2s"
</pre>
===algorithm 2===
<lang Phix>function nigh(string n)
integer hi = n[$]
Line 487 ⟶ 519:
"738440","45072010","95322020",
"9589776899767587796600"}
atom t0 = time()
for i=1 to length(tests) do
string t = tests[i]
printf(1,"%22s => %s\n",{t,nigh(t)})
end for</lang>
?elapsed(time()-t0)</lang>
{{out}}
<pre>
Line 502 ⟶ 536:
95322020 => 95322200
9589776899767587796600 => 9589776899767587900667
"0s"
</pre>
 
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.