Inconsummate numbers in base 10: Difference between revisions

→‎{{header|Wren}}: Added a Python translation.
m (Python example)
(→‎{{header|Wren}}: Added a Python translation.)
Line 132:
 
One thousandth: 6,996
</pre>
 
Alternatively and more generally:
{{trans|Python}}
Though I think the Python version is in fact wrong for the 100,000th number since if you enumerate up to 10,000 you get the 10,000th inconsummate number to be 42,171 rather than 59,853.
 
The problem seems to be that the minimum divisor for (say) 6 digit numbers is not 999999/54 = 18518 but 109999/37 = 2972. I've corrected for that in the following translation.
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./fmt" for Fmt
 
var generateInconsummate = Fn.new { |maxWanted|
var minDigitSums = (2..14).map { |i| [10.pow(i), ((10.pow(i-2) * 11 - 1) / (9 * i - 17)).floor] }
var limit = Nums.min(minDigitSums.where { |p| p[1] > maxWanted }.map { |p| p[0] })
var arr = List.filled(limit, 0)
arr[0] = 1
for (dividend in 1...limit) {
var ds = Int.digitSum(dividend)
var quo = (dividend/ds).floor
var rem = dividend % ds
if (rem == 0 && quo < limit) arr[quo] = 1
}
for (j in 0...arr.count) {
if (arr[j] == 0) Fiber.yield(j)
}
}
 
var gi = Fiber.new(generateInconsummate)
var incons = List.filled(50, 0)
var incons1k
var incons10k
var incons100k
System.print("First 50 inconsummate numbers in base 10:")
 
for (i in 1..100000) {
var j = gi.call(100000)
if (i <= 50) {
incons[i-1] = j
} else if (i == 1000) {
incons1k = j
} else if (i == 10000) {
incons10k = j
} else if (i == 100000) {
incons100k = j
}
}
Fmt.tprint("$3d", incons, 10)
Fmt.print("\nOne thousandth $,d", incons1k)
Fmt.print("Ten thousandth $,d", incons10k)
Fmt.print("100 thousandth $,d", incons100k)</syntaxhighlight>
 
{{out}}
<pre>
First 50 inconsummate numbers in base 10:
62 63 65 75 84 95 161 173 195 216
261 266 272 276 326 371 372 377 381 383
386 387 395 411 416 422 426 431 432 438
441 443 461 466 471 476 482 483 486 488
491 492 493 494 497 498 516 521 522 527
 
One thousandth 6,996
Ten thousandth 59,853
100 thousandth 536,081
</pre>
9,476

edits