Colorful numbers: Difference between revisions
Content added Content deleted
(julia example) |
|||
Line 57: | Line 57: | ||
(Note that 0, here is a different order of magnitude than 1.) |
(Note that 0, here is a different order of magnitude than 1.) |
||
=={{header|Julia}}== |
|||
<lang julia>function iscolorful(n, base=10) |
|||
0 <= n < 10 && return true |
|||
dig = digits(n, base=base) |
|||
(1 in dig || 0 in dig || !allunique(dig)) && return false |
|||
products = copy(dig) |
|||
for i in 2:length(dig), j in 1:length(dig)-i+1 |
|||
p = prod(dig[j:j+i-1]) |
|||
p in products && return false |
|||
push!(products, p) |
|||
end |
|||
return true |
|||
end |
|||
function testcolorfuls() |
|||
println("Colorful numbers for 1:25, 26:50, 51:75, and 76:100:") |
|||
for i in 1:100 |
|||
iscolorful(i) && print(rpad(i, 5)) |
|||
i % 25 == 0 && println() |
|||
end |
|||
csum = 0 |
|||
for i in 0:7 |
|||
j, k = i == 0 ? 0 : 10^i, 10^(i+1) - 1 |
|||
n = count(i -> iscolorful(i), j:k) |
|||
csum += n |
|||
println("The count of colorful numbers between $j and $k is $n.") |
|||
end |
|||
println("The total number of colorful numbers is $csum.") |
|||
end |
|||
testcolorfuls() |
|||
</lang>{{out}} |
|||
<pre> |
|||
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 count of colorful numbers between 0 and 9 is 10. |
|||
The count of colorful numbers between 10 and 99 is 56. |
|||
The count of colorful numbers between 100 and 999 is 328. |
|||
The count of colorful numbers between 1000 and 9999 is 1540. |
|||
The count of colorful numbers between 10000 and 99999 is 5514. |
|||
The count of colorful numbers between 100000 and 999999 is 13956. |
|||
The count of colorful numbers between 1000000 and 9999999 is 21596. |
|||
The count of colorful numbers between 10000000 and 99999999 is 14256. |
|||
The total number of colorful numbers is 57256. |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |