Two identical strings: Difference between revisions

m
(→‎{{header|AppleScript}}: Added an idiomatic solution.)
Line 786:
----
===Idiomatic===
<lang applescript><lang applescript>on task(maxN)
set startWith0 to false -- Change to true to start with 0 and "00".
set rhv to -(startWith0 as integer) -- Start value of "right hand" string.
script o
property bits : {0rhv} -- Change to {-1} to include 0
property output : {}
end script
Line 795 ⟶ 797:
set AppleScript's text item delimiters to ""
repeat
-- Add 1 to the binary-digit list's LSD and perform any carries.
set carry to 1
repeat with i from (count o's bits) to 1 by -1
Line 804 ⟶ 806:
end repeat
if (carry = 1) then set beginning of o's bits to carry
-- WorkAdd out1 to the number"right-hand" representedvalue byand twowork suchout liststhe end-oncorresponding n.
set nrhv to 0rhv + 1
set n to rhv * (2 ^ (count o's bits)) div 1 + rhv
repeat 2 times
-- Unless n exceeds repeatmaxN, withappend dit inand o'sits bitsbinary form to the output.
set n to n * 2 + d
end repeat
end repeat
-- Unless the number exceeds maxN, append it and its binary form to the output.
if (n > maxN) then exit repeat
set end of o's output to (n as text) & ": " & o's bits & o's bits
557

edits