Colorful numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Changed to Phix's approach which is over 100 times faster than I had before.)
Line 259: Line 259:


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Phix}}
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-set}}
{{libheader|Wren-set}}
Line 287: Line 288:
}
}


var count = List.filled(9, 0)
System.print("The colorful numbers less than 100 are:")
var used = List.filled(11, false)
var colorful = (0..99).where { |i| isColorful.call(i) }.toList
for (chunk in Lst.chunks(colorful, 10)) Fmt.print("$2d", chunk)

var largest = 0
var largest = 0
System.print("\nThe largest possible colorful number is:")
for (i in 1e8-1..0) {
if (isColorful.call(i)) {
Fmt.print("$,d", i)
largest = i
break
}
}


var countColorful // recursive
var count = List.filled(9, 0)
countColorful = Fn.new { |taken, n|
var dc = 1
if (taken == 0) {
var pow = 10
for (digit in 0..9) {
System.print("\nCount of colorful numbers for each order of magnitude:")
var i = 0
var dx = digit + 1
used[dx] = true
while (true) {
countColorful.call((digit < 2) ? 9 : 1, String.fromByte(digit + 48))
if (dc > 1) {
var rem = i % 10
used[dx] = false
if (rem == 0 || rem == 1) {
i = i + 2 - rem
continue
}
}
}
if (isColorful.call(i)) count[dc] = count[dc] + 1
if (i == pow - 1 || i == largest) {
var total = (dc == 1) ? 10 : pow * 0.9
var pc = 100 * count[dc] / total
Fmt.print(" $d digit colorful number count: $,6d - $7.3f\%", dc, count[dc], pc)
if (i == largest) break
dc = dc + 1
pow = pow * 10
i = pow * 0.2 + 2
} else {
} else {
i = i + 1
var nn = Num.fromString(n)
if (isColorful.call(nn)) {
var ln = n.count
count[ln] = count[ln] + 1
if (nn > largest) largest = nn
}
if (taken < 9) {
for (digit in 2..9) {
var dx = digit + 1
if (!used[dx]) {
used[dx] = true
countColorful.call(taken + 1, n + String.fromByte(digit + 48))
used[dx] = false
}
}
}
}
}
}

var cn = (0..99).where { |i| isColorful.call(i) }.toList
System.print("The %(cn.count) colorful numbers less than 100 are:")
for (chunk in Lst.chunks(cn, 10)) Fmt.print("$2d", chunk)

countColorful.call(0, "")
System.print("\nThe largest possible colorful number is:")
Fmt.print("$,d\n", largest)

System.print("Count of colorful numbers for each order of magnitude:")
var pow = 10
for (dc in 1...count.count) {
Fmt.print(" $d digit colorful number count: $,6d - $7.3f\%", dc, count[dc], 10 * count[dc] / pow)
pow = (pow == 10) ? 90 : pow * 10
}
}


Line 332: Line 340:
{{out}}
{{out}}
<pre>
<pre>
The colorful numbers less than 100 are:
The 66 colorful numbers less than 100 are:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
23 24 25 26 27 28 29 32 34 35
23 24 25 26 27 28 29 32 34 35