Inconsummate numbers in base 10: Difference between revisions

Added Sidef
m (julia example)
(Added Sidef)
 
(4 intermediate revisions by 3 users not shown)
Line 904:
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">using ResumableFunctions
 
Line 948 ⟶ 949:
</pre>
 
=={{header|Lua}}==
Tested with Lua 5.1.2
<syntaxhighlight lang="lua">
do --[[ find some incomsummate numbers: integers that cannot be expressed as
an integer divided by the sum of its digits
--]]
 
local maxConsummate = 999999
local consummate = {} -- table of numbers that can be formed by n / digit sum n
--[[ calculate the maximum number we must consider to find consummate numbers
up to maxConsummate - which is 9 * the number of digits in maxConsummate
--]]
local maxSum = 9
local v = math.floor( maxConsummate / 10 )
while v > 0 do
maxSum = maxSum + 9
v = math.floor( v / 10 )
end
local maxNumber = maxConsummate * maxSum
-- construct the digit sums of the numbers up to maxNumber and find the consumate numbers
consummate[ 1 ] = true
local tn, hn, th, tt, ht, mi, tm = 1, 0, 0, 0, 0, 0, 0
for n = 10, maxNumber, 10 do
local sumd = tm + mi + ht + tt + th + hn + tn
for d = n, n + 9 do
if d % sumd == 0 then -- d is comsummate
local dRatio = math.floor( d / sumd )
if dRatio <= maxConsummate then
consummate[ dRatio ] = true
end
end
sumd = sumd + 1
end
tn = tn + 1
if tn > 9 then
tn = 0
hn = hn + 1
if hn > 9 then
hn = 0
th = th + 1
if th > 9 then
th = 0
tt = tt + 1
if tt > 9 then
tt = 0
ht = ht + 1
if ht > 9 then
ht = 0
mi = mi + 1
if mi > 9 then
mi = 0
tm = tm + 1
end
end
end
end
end
end
end
local count = 0
io.write( "The first 50 inconsummate numbers:\n" )
for i = 1, maxConsummate do
if count >= 100000 then break end
if not consummate[ i ] then
count = count + 1
if count < 51 then
io.write( string.format( "%6d", i ), ( count % 10 == 0 and "\n" or "" ) )
elseif count == 1000 or count == 10000 or count == 100000 then
io.write( "Inconsummate number ", string.format( "%6d", count )
, ": ", string.format( "%8d", i ), "\n"
)
end
end
end
end
</syntaxhighlight>
{{out}}
<pre>
The first 50 inconsummate numbers:
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
Inconsummate number 1000: 6996
Inconsummate number 10000: 59853
Inconsummate number 100000: 536081
</pre>
 
=={{header|Nim}}==
Line 1,403 ⟶ 1,492:
<pre>
1: { 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 }
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var gen_inconsummate = Enumerator({|callback|
 
for n in (1..Inf) {
for k in (1..Inf) {
 
if (9*k*n < 10**(k-1)) {
callback(n)
break
}
 
var check = false
10.combinations_with_repetition(k, {|*d|
var s = d.sum || next
if (Str(s*n).sort == d.join) {
check = true
break
}
})
check || next
break
}
}
})
 
with (50) {|n|
say "First #{n} inconsummate numbers (in base 10):"
gen_inconsummate.first(n).each_slice(10, {|*s|
say s.map{ '%3s' % _ }.join(' ')
})
}</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
</pre>
 
Line 1,411 ⟶ 1,541:
 
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="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,444 ⟶ 1,574:
{{trans|Python}}
...though much quicker as I'm using lower figures for the second component of the minDigitSums tuples.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
2,747

edits