Jump to content

Two identical strings: Difference between revisions

→‎{{header|AppleScript}}: Added an idiomatic solution.
(Added Go)
(→‎{{header|AppleScript}}: Added an idiomatic solution.)
Line 784:
957 -> 1110111101
990 -> 1111011110</pre>
----
===Idiomatic===
<lang applescript>on task(maxN)
script o
property bits : {0} -- Change to {-1} to include 0
property output : {}
end script
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
repeat
-- Add 1 to 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
set columnSum to (item i of o's bits) + carry
set item i of o's bits to columnSum mod 2
set carry to columnSum div 2
if (carry = 0) then exit repeat
end repeat
if (carry = 1) then set beginning of o's bits to carry
-- Work out the number represented by two such lists end-on.
set n to 0
repeat 2 times
repeat with d in o's bits
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
end repeat
set AppleScript's text item delimiters to linefeed
set o's output to o's output as text
set AppleScript's text item delimiters to astid
return o's output
end task
 
task(999)</lang>
 
{{output}}
<lang applescript>"3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110"</lang>
 
=={{header|AWK}}==
557

edits

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