Inconsummate numbers in base 10: Difference between revisions
Content added Content deleted
(J) |
m (→{{header|Wren}}: Changes to preamble and made second version more compact.) |
||
Line 643: | Line 643: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
It appears to be more than enough to calculate ratios for all numbers up to 999,999 (which only takes about 0.4 seconds on my machine) to be sure of finding the 1,000th inconsummate number. |
It appears to be more than enough to calculate ratios for all numbers up to 999,999 (which only takes about 0.4 seconds on my machine) to be sure of finding the 1,000th inconsummate number. |
||
In fact it still finds the 1,000th number if you limit the search to the first 249,999 (but not 248,999) numbers which may be where the first magic number of '250' comes from in the Pascal/Phix entries. |
|||
<syntaxhighlight lang="ecmascript">import "./math" for Int |
<syntaxhighlight lang="ecmascript">import "./math" for Int |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
Line 675: | Line 677: | ||
Alternatively and more generally: |
Alternatively and more generally: |
||
{{trans|Python}} |
{{trans|Python}} |
||
...though much quicker as I'm using lower figures for the second component of the minDigitSums tuples. |
|||
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 |
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
var generateInconsummate = |
var generateInconsummate = Fiber.new { |maxWanted| |
||
var minDigitSums = (2..14).map { |i| [10.pow(i), ((10.pow(i-2) * 11 - 1) / (9 * i - 17)).floor] } |
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 limit = Nums.min(minDigitSums.where { |p| p[1] > maxWanted }.map { |p| p[0] }) |
||
var arr = List.filled(limit, 0) |
var arr = List.filled(limit, 0) |
||
arr[0] = 1 |
arr[0] = 1 |
||
for (dividend in 1...limit) { |
|||
var ds = Int.digitSum(dividend) |
var ds = Int.digitSum(dividend) |
||
var quo = (dividend/ds).floor |
var quo = (dividend/ds).floor |
||
Line 697: | Line 697: | ||
} |
} |
||
var gi = Fiber.new(generateInconsummate) |
|||
var incons = List.filled(50, 0) |
var incons = List.filled(50, 0) |
||
var incons1k |
|||
var incons10k |
|||
var incons100k |
|||
System.print("First 50 inconsummate numbers in base 10:") |
System.print("First 50 inconsummate numbers in base 10:") |
||
for (i in 1..100000) { |
for (i in 1..100000) { |
||
var j = |
var j = generateInconsummate.call(100000) |
||
if (i <= 50) { |
if (i <= 50) { |
||
incons[i-1] = j |
incons[i-1] = j |
||
⚫ | |||
} else if (i == 1000) { |
} else if (i == 1000) { |
||
Fmt.print("\nOne thousandth $,d", j) |
|||
} else if (i == 10000) { |
} else if (i == 10000) { |
||
Fmt.print("Ten thousandth $,d", j) |
|||
} else if (i == 100000) { |
} else if (i == 100000) { |
||
Fmt.print("100 thousandth $,d", j) |
|||
} |
} |
||
}</syntaxhighlight> |
|||
} |
|||
⚫ | |||
Fmt.print("\nOne thousandth $,d", incons1k) |
|||
Fmt.print("Ten thousandth $,d", incons10k) |
|||
Fmt.print("100 thousandth $,d", incons100k)</syntaxhighlight> |
|||
{{out}} |
{{out}} |