Colorful numbers: Difference between revisions

→‎{{header|Wren}}: Changed to Phix's approach which is over 100 times faster than I had before.
(→‎{{header|Wren}}: Changed to Phix's approach which is over 100 times faster than I had before.)
Line 259:
 
=={{header|Wren}}==
{{trans|Phix}}
{{libheader|Wren-math}}
{{libheader|Wren-set}}
Line 287 ⟶ 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
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 (dctaken >== 10) {
var pow = 10
ifor =(digit pow *in 0.2 +.9) 2{
System.print("\nCount of colorful numbers for each order of magnitude:")
var idx = 0digit + 1
continueused[dx] = true
while (true) {
countColorful.call((digit < 2) ? 9 : 1, String.fromByte(digit + 48))
if (dc > 1) {
var rem = i %used[dx] = 10false
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 {
ivar nn = i + 1Num.fromString(n)
if (isColorful.call(inn)) {
var ln = n.count
if (isColorful.call(i)) count[dcln] = count[dcln] + 1
if (inn ==> largest) breaklargest = nn
break}
if (remtaken ==< 0 || rem == 19) {
for (digit in 2..9) {
dc var dx = dcdigit + 1
if (!used[dx]) {
used[dx] = true
countColorful.call(taken + 1, n + String.fromByte(digit + 48))
used[dx] = false
i = i + 2 - rem}
largest = i }
}
}
 
var colorfulcn = (0..99).where { |i| isColorful.call(i) }.toList
System.print("The %(cn.count) colorful numbers less than 100 are:")
for (chunk in Lst.chunks(colorfulcn, 10)) Fmt.print("$2d", chunk)
 
countColorful.call(0, "")
System.print("\nThe largest possible colorful number is:")
Fmt.print("$,d\n", ilargest)
 
System.print("\nCountCount of colorful numbers for each order of magnitude:")
var dcpow = 110
for (idc in 1e8-1..0.count.count) {
Fmt.print(" $d digit colorful number count: $,6d - $7.3f\%", dc, count[dc], pc10 * count[dc] / pow)
var totalpow = (dcpow == 110) ? 1090 : pow * 0.910
}
 
Line 332 ⟶ 340:
{{out}}
<pre>
The 66 colorful numbers less than 100 are:
0 1 2 3 4 5 6 7 8 9
23 24 25 26 27 28 29 32 34 35
9,476

edits