Colorful numbers: Difference between revisions

Added AppleScript.
(Added AppleScript.)
Line 92:
The total number of colorful numbers is 57256.
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">(*
The task requirement that colorful numbers be identified using a test handler is observed
here, but the information regarding 1s and 0s is used along with some logic to skip obvious
non-starters and spare a few minutes' running time. Since multiplication is commutative,
the "colorful" status of any number is matched by that of its reverse. Working up from 0,
the lower number of each colorful pair is encountered first and testing at that magnitude
must continue at least as far as the other. If the largest reversal of a colorful number
calculated so far is actually reached, it's the last colorful number at that magnitude.
Testing either jumps to the first possibility at the next level or stops there.
*)
on isColorful(n)
if ((n > 98765432) or (n < 0) or (n mod 1 > 0)) then return false
set products to {n mod 10}
repeat while (n > 9)
set n to n div 10
set digit to n mod 10
if ((digit < 2) or (digit is in products)) then return false
set end of products to digit
end repeat
set nDigits to (count products)
repeat with i from 1 to (nDigits - 1)
set prod to products's item i
repeat with j from (i + 1) to nDigits
set prod to prod * (products's item j)
if (prod is in products) then return false
set end of products to prod
end repeat
end repeat
return true
end isColorful
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set colorfuls to {}
set counter to 0
set counts to {}
set magnitude to 1 -- Number of digits in tested numbers.
set resumptionCoeff to 2.3456789 -- Governs where to resume after a magnitude increase.
set searchLimit to 98765432 -- Highest number possible with the allowed digits.
set largestReverse to 9 -- Largest reverse of a single-digit number!
set n to -1
repeat
-- Check numbers not ending with 1 or 0 and all numbers < 100
-- (to show isColorful() working in all cases).
if ((n mod 10 > 1) or (magnitude < 2)) and (isColorful(n)) then
if (n < 100) then set end of colorfuls to text -1 thru -1 of " " & n
set counter to counter + 1
if (n = largestReverse) then
set end of counts to counter
set counter to 0
set magnitude to magnitude * 10
if (magnitude > searchLimit) then exit repeat
set n to resumptionCoeff * magnitude div 1 - 1
else
set temp to n
set |reverse| to temp mod 10
repeat while (temp > 9)
set temp to temp div 10
set |reverse| to |reverse| * 10 + temp mod 10
end repeat
if (|reverse| > largestReverse) then set largestReverse to |reverse|
end if
end if
set n to n + 1
end repeat
set output to {"The colorful numbers below 100:"}
repeat with i from 1 to 66 by 11
set end of output to join(colorfuls's items i thru (i + 10), "")
end repeat
set end of output to linefeed & "The largest colorful number is " & largestReverse
set counter to counts's beginning
set end of output to linefeed & "The number of them with 1 digit is " & counter
repeat with i from 2 to (count counts)
set end of output to "The number with " & i & " digits is " & (counts's item i)
set counter to counter + (counts's item i)
end repeat
set end of output to "The total number overall is " & counter
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"The colorful numbers below 100:
0 1 2 3 4 5 6 7 8 9 23
24 25 26 27 28 29 32 34 35 36 37
38 39 42 43 45 46 47 48 49 52 53
54 56 57 58 59 62 63 64 65 67 68
69 72 73 74 75 76 78 79 82 83 84
85 86 87 89 92 93 94 95 96 97 98
 
The largest colorful number is 98746253
 
The number of them with 1 digit is 10
The number with 2 digits is 56
The number with 3 digits is 328
The number with 4 digits is 1540
The number with 5 digits is 5514
The number with 6 digits is 13956
The number with 7 digits is 21596
The number with 8 digits is 14256
The total number overall is 57256"</syntaxhighlight>
 
=={{header|C}}==
557

edits