Colorful numbers: Difference between revisions
Content added Content deleted
m (→{{header|Free Pascal}}: added Output for coloful numbers< 100.) |
(Created Nim solution.) |
||
Line 1,141: | Line 1,141: | ||
57256</pre> |
57256</pre> |
||
=={{header|Nim}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="Nim">import std/[math, intsets, strformat] |
|||
func digits(n: Natural): seq[int] = |
|||
## Return the digits of "n" in reverse order. |
|||
var n = n |
|||
while true: |
|||
result.add n mod 10 |
|||
n = n div 10 |
|||
if n == 0: break |
|||
var largest = 0 |
|||
proc isColorful(n: Natural): bool = |
|||
## Return true if "n" is colorful. |
|||
if n in 0..9: return true |
|||
let digSeq = n.digits |
|||
var digSet: set[0..9] |
|||
for d in digSeq: |
|||
if d <= 1 or d in digSet: |
|||
return false |
|||
digSet.incl d |
|||
var products = digSeq.toIntSet() |
|||
for i in 0..<digSeq.high: |
|||
for j in (i + 1)..digSeq.high: |
|||
let p = prod(digSeq.toOpenArray(i, j)) |
|||
if p in products: |
|||
return false |
|||
products.incl p |
|||
if n > largest: largest = n |
|||
result = true |
|||
echo "Colorful numbers for 1:25, 26:50, 51:75, and 76:100:" |
|||
for i in countup(1, 100, 25): |
|||
for j in 0..24: |
|||
if isColorful(i + j): |
|||
stdout.write &"{i + j: 5}" |
|||
echo() |
|||
echo() |
|||
var csum = 0 |
|||
for i in 0..7: |
|||
let j = if i == 0: 0 else: 10^i |
|||
let k = 10^(i+1) - 1 |
|||
var n = 0 |
|||
for x in j..k: |
|||
if x.isColorful: inc n |
|||
inc csum, n |
|||
echo &"The count of colorful numbers between {j} and {k} is {n}." |
|||
echo() |
|||
echo &"The largest possible colorful number is {largest}." |
|||
echo &"The total number of colorful numbers is {csum}." |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Colorful numbers for 1:25, 26:50, 51:75, and 76:100: |
|||
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 largest possible colorful number is 98746253. |
|||
The total number of colorful numbers is 57256. |
|||
</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |