Klarner-Rado sequence: Difference between revisions

m
→‎{{header|AppleScript}}: Minor improvements to the recursive version.
(Created Nim solution.)
m (→‎{{header|AppleScript}}: Minor improvements to the recursive version.)
Line 138:
 
=={{header|AppleScript}}==
One way to test numbers for membership of the sequence is to feed them to a recursive handler which determines whether or not there's a Klarner-Rado route from them down to 01. It makes finding the elements in order simple, but takes aboutnearly five and a halfsix minutes to get toa themillion millionthof onethem.
 
<syntaxhighlight lang="applescript">-- Is n in the Klarner-Rado sequence?
-- Fully recursive:
(*
(* on isKlarnerRado(n)
set n to n - 1
return ((n = 01) or ((n mod 23 = 01) and (isKlarnerRado(n div 23))) or ((n mod 3 = 0) and (isKlarnerRado(n div 3))))¬
end ((n mod 2 = 1) and (isKlarnerRado(n div *2))))
end isKlarnerRado
*)
 
-- Optimised withWith tail call elimination. 90About a secondsminute faster than the above in this script.
-- Interestingly, leaving out the 'else's and comparing n mod 2 directly with 0 slows it down!
on isKlarnerRado(n)
set n to n - 1
repeat
if ((n = 01) or ((n mod 3 = 01) and (isKlarnerRado(n div 3)))) then
return true
else if (n mod 2 =< 01) then
set n to n div 2 - 1
else
return false
set n to n - 1else
set n to n -div 12
end if
end repeat
557

edits