Inconsummate numbers in base 10: Difference between revisions

Added Sidef
No edit summary
(Added Sidef)
 
(15 intermediate revisions by 8 users not shown)
Line 1:
{{draft task}}
A consummate number is a non-negative integer that can be formed by some integer '''N''' divided by the the digital sum of '''N'''.
 
Line 358:
 
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 M=999
Line 383 ⟶ 384:
491 492 493 494 497
498 516 521 522 527</pre>
 
==={{header|BASIC256}}===
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">m = 999
dim c(m) fill false
z = m * 9 * (length(string(m))-1)
 
for i = 10 to z
j = i
s = 0
while j <> 0
s += (j % 10)
j /= 10
end while
if i % s = 0 then j = i \ s : if j <= m then c[j] = true
next i
 
cont = 0
print "The first 50 inconsummate numbers:"
for i = 10 to m
if cont = 50 then end
if not c[i] then cont += 1 : print rjust(string(i), 4);
next i</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|BASIC}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|Just BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">110 m = 999
120 dim c(m)
130 z = m*9*(len(str$(m))-1)
140 for i = 10 to z
150 j = i : s = 0
160 s = s+j mod 10 : j = int(j/10) : if j then goto 160
170 if i mod s = 0 then j = int(i/s) : if j <= m then c(j) = -1
180 next i
190 j = 0
200 for i = 10 to m
210 if j = 50 then end
220 if c(i) <> -1 then j = j+1 : print i,
230 next i</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|True BASIC}}===
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">LET m = 999
DIM c(0)
MAT REDIM c(m)
LET z = m * 9 * (LEN(STR$(m))-1)
FOR i = 10 TO z
LET j = i
LET s = 0
DO
LET s = s + REMAINDER(j,10)
LET j = INT(j/10)
LOOP WHILE j <> 0
IF REMAINDER(i,s) = 0 THEN
LET j = INT(i/s)
IF j <= m THEN LET c(j) = -1
END IF
NEXT i
 
LET cont = 0
PRINT "The first 50 inconsummate numbers:"
FOR i = 10 TO m
IF cont = 50 THEN EXIT FOR
IF c(i) <> -1 THEN
LET cont = cont + 1
PRINT i;
END IF
NEXT i
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{trans|BASIC256}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Inconsummate numbers in base 10"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
m = 999
DIM c[m]
x$ = STRING$(m)
z = m * 9 * LEN(x$) - 1
 
FOR i = 10 TO z
j = i
s = 0
DO WHILE j <> 0
s = s + (j MOD 10)
j = INT(j / 10)
LOOP
IF i MOD s = 0 THEN
j = INT(i / s)
IF j <= m THEN c[j] = $$TRUE
END IF
NEXT i
 
cont = 0
PRINT "The first 50 inconsummate numbers:"
FOR i = 10 TO m
IF cont = 50 THEN EXIT FOR
IF NOT c[i] THEN INC cont : PRINT FORMAT$("####",i);
NEXT i
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|BASIC}}
<syntaxhighlight lang="vb">m = 999
dim c(m)
z = m * 9 * (len(str$(m))-1)
 
for i = 10 to z
j = i
s = 0
while j <> 0
s = s + mod(j, 10)
j = int(j / 10)
wend
if mod(i, s) = 0 then j = int(i / s) : if j <= m then c(j) = true : fi : fi
next i
 
cont = 0
print "The first 50 inconsummate numbers:"
for i = 10 to m
if cont = 50 end
if not c(i) then cont = cont + 1 : print i using("####"); : fi
next i
print</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
const uint32_t sieve_size = 10'000;
const uint32_t maximum = 9 * std::to_string(sieve_size).length() * sieve_size;
 
std::vector<bool> is_consummate(sieve_size + 1, false);
 
uint32_t digital_sum(const uint32_t& number) {
uint32_t result = 0;
const std::string text = std::to_string(number);
for ( const char ch : text ) {
result += ch - (int) '0';
}
return result;
}
 
void create_is_consummate() {
for ( uint32_t n = 1; n < maximum; ++n ) {
uint32_t sum = digital_sum(n);
if ( n % sum == 0 ) {
uint32_t quotient = n / sum;
if ( quotient <= sieve_size ) {
is_consummate[quotient] = true;
}
}
}
}
 
int main() {
create_is_consummate();
std::vector<uint32_t> inconsummates;
for ( uint32_t i = 0; i <= sieve_size; ++i ) {
if ( ! is_consummate[i] ) {
inconsummates.emplace_back(i);
}
}
 
std::cout << "The first 50 inconsummate numbers in base 10:" << std::endl;
for ( uint32_t i = 1; i <= 50; ++i ) {
std::cout << std::setw(3) << inconsummates[i] << ( i % 10 == 0 ? "\n" : " " );
}
std::cout << "\n" << "The 1,000 inconsummate number is " << inconsummates[1'000] << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The 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
 
The 1,000 inconsummate number is 6996
</pre>
 
=={{header|Delphi}}==
Line 456 ⟶ 662:
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
limit = 10000
maxn = 10000 * 9 * 4
#
len consummate[] limit
#
func digsum i .
res = 0
while i > 0
res += i mod 10
i = i div 10
.
return res
.
for d = 1 to maxn
ds = digsum d
if d mod ds = 0
q = d / ds
if q <= limit
consummate[q] = 1
.
.
.
d = 1
repeat
if d > len consummate[]
print "error - increase limit"
break 1
.
if consummate[d] = 0
cnt += 1
if cnt <= 50
write d & " "
.
.
until cnt = 1000
d += 1
.
print ""
print ""
print d
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 556 ⟶ 806:
99999 { incons 1000000
536081</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.stream.IntStream;
 
public final class InconsummateNumbersInBase10 {
 
public static void main(String[] args) {
createIsConsummate();
int[] inconsummates = IntStream.rangeClosed(0, sieveSize).filter( i -> ! isConsummate[i] ).toArray();
System.out.println("The first 50 inconsummate numbers in base 10:");
for ( int i = 1; i <= 50; i++ ) {
System.out.print(String.format("%3d%s", inconsummates[i], ( i % 10 == 0 ? "\n" : " " )));
}
System.out.println();
System.out.println("The 1,000 inconsummate number is " + inconsummates[1_000]);
}
private static void createIsConsummate() {
isConsummate = new boolean[sieveSize + 1];
for ( int n = 1; n < maximum; n++ ) {
int digitalSum = digitalSum(n);
if ( n % digitalSum == 0 ) {
int quotient = n / digitalSum;
if ( quotient <= sieveSize ) {
isConsummate[quotient] = true;
}
}
}
}
private static int digitalSum(int number) {
return String.valueOf(number).chars().map( i -> i - (int) '0' ).sum();
}
private static boolean[] isConsummate;
private static final int sieveSize = 10_000;
private static final int maximum = 9 * String.valueOf(sieveSize).length() * sieveSize;
}
</syntaxhighlight>
{{ out }}
<pre>
The 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
 
The 1,000 inconsummate number is 6996
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
Line 596 ⟶ 901:
The 1000th:
6996
</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">using ResumableFunctions
 
@resumable function gen_inconsummate(maxwanted)
mindigitsums = map(i -> (10^i, (10^(i-2) * 11 - 1) ÷ (9 * i - 17)), 2:14)
limit = minimum(p[1] for p in mindigitsums if p[2] > maxwanted)
arr = zeros(Int, limit)
arr[1] = 1
for dividend in 1:limit-1
dsum = sum(digits(dividend))
quo, rem = divrem(dividend, dsum)
rem == 0 && quo < limit && (arr[quo] = 1)
end
for j in eachindex(arr)
arr[j] == 0 && @yield(j)
end
end
 
println("The first 50 inconsummate numbers in base 10:")
for (i, j) in enumerate(gen_inconsummate(100000))
if i <= 50
print(rpad(j, 6), i % 10 == 0 ? "\n" : "")
elseif i == 1000
println("\nThe one-thousandth inconsummate number in base 10 is $j")
elseif i == 10000
println("The ten-thousandth inconsummate number in base 10 is $j")
elseif i == 100000
println("The hundred-thousandth inconsummate number in base 10 is $j")
break
end
end
</syntaxhighlight>{{out}}
<pre>
The 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
 
The one-thousandth inconsummate number in base 10 is 6996
The ten-thousandth inconsummate number in base 10 is 59853
The hundred-thousandth inconsummate number in base 10 is 536081
</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>
 
Line 1,033 ⟶ 1,473:
 
One thousandth: 6996</pre>
 
=={{header|RPL}}==
<code>∑DIGITS</code> is defined at [[Sum digits of an integer#RPL|Sum digits of an integer]]
{{trans|BASIC}}
« 999 DUP { } + 0 CON
10 ROT DUP XPON 1 + 9 * * '''FOR''' n
n DUP <span style="color:blue">∑DIGITS</span>
'''IF''' DUP2 MOD NOT '''THEN'''
/ '''IFERR''' 1 PUT '''THEN''' DROP2 '''END'''
'''ELSE''' DROP2 '''END'''
'''NEXT'''
{ } SWAP 10
'''WHILE''' 3 PICK SIZE 50 < '''REPEAT'''
'''IF''' GETI NOT '''THEN''' ROT OVER 1 - + ROT ROT '''END'''
'''END''' DROP2
» '<span style="color:blue">TASK</span>' STO
{{out}}
<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>
 
=={{header|Wren}}==
Line 1,040 ⟶ 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,073 ⟶ 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