Colorful numbers: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(→{{header|AppleScript}}: Added an alternative using a translation from the Phix solution.) |
||
Line 309: | Line 309: | ||
The number with 8 digits is 14256 |
The number with 8 digits is 14256 |
||
The total number overall is 57256"</syntaxhighlight> |
The total number overall is 57256"</syntaxhighlight> |
||
Or here's an alternative using the same <code>isColorful</code> handler but with a translation of the [[#Phix|Phix]] solution's <code>count_colourful</code> function feeding in the numbers. Multi-digit numbers containing 0, 1, or duplicated digits are studiously not produced, so the execution time is very much shorter. (Just over nine seconds on the test machine as opposed to just over six minutes.) Same output as above. |
|||
<syntaxhighlight lang="applescript">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 {} |
|||
repeat 100 times |
|||
set end of colorfuls to missing value |
|||
end repeat |
|||
set counts to {0, 0, 0, 0, 0, 0, 0, 0} |
|||
set largest to 0 |
|||
script o |
|||
property used : {false, false, false, false, false, false, false, false, false, false} |
|||
on count_colourful(taken, n) |
|||
if (taken = 0) then |
|||
repeat with digit from 0 to 9 |
|||
set dx to digit + 1 |
|||
set used's item dx to true |
|||
count_colourful((9 - digit) div 8 * 8 + 1, digit) |
|||
set used's item dx to false |
|||
end repeat |
|||
else |
|||
if (isColorful(n)) then |
|||
set ln to 1 |
|||
repeat until ((10 ^ ln) > n) |
|||
set ln to ln + 1 |
|||
end repeat |
|||
set counts's item ln to (counts's item ln) + 1 |
|||
if (n < 100) then set colorfuls's item (n + 1) to (" " & n)'s text -3 thru -1 |
|||
if (n > largest) then set largest to n |
|||
end if |
|||
if (taken < 9) then |
|||
repeat with digit from 2 to 9 |
|||
set dx to digit + 1 |
|||
if (not used's item dx) then |
|||
set used's item dx to true |
|||
count_colourful(taken + 1, n * 10 + digit) |
|||
set used's item dx to false |
|||
end if |
|||
end repeat |
|||
end if |
|||
end if |
|||
end count_colourful |
|||
end script |
|||
o's count_colourful(0, 0) |
|||
set colorfuls to colorfuls's every text |
|||
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 " & largest |
|||
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> |
|||
=={{header|C}}== |
=={{header|C}}== |