Inconsummate numbers in base 10: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: prepend Pascal. no divisor of a niven number)
(→‎{{header|ALGOL 68}}: Simplify - no need for the divisor sum table, find the consummate numbers while finding the sums, handle the stretch goal)
Line 31: Line 31:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Constructs a table of digit sums and from that a table of consummate numbers. The table of consummate numbers will be inaccurate for numbers > 9999.
<syntaxhighlight lang="algol68">
<syntaxhighlight lang="algol68">
BEGIN # find some incomsummate numbers: integers that cannot be expressed as #
BEGIN # find some incomsummate numbers: integers that cannot be expressed as #
# an integer divided by the sum of its digits #
# an integer divided by the sum of its digits #
INT max number = 499 999; # maximum number we will consider #
# table of numbers that can be formed by n / digit sum n #
[ 0 : 999 999 ]BOOL consummate;
# chosen because if we assume the 1000th #
FOR i FROM LWB consummate TO UPB consummate DO
# inconsummate number is <= 9 999, then the #
consummate[ i ] := FALSE
# maximum possible digit sum is 45 and so the #
OD;
# maximum number to test would be 45 x 9 999 #
# calculate the maximum number we must consider to find consummate #
# i.e.: 449 955 #
# numbers up to UPB consummate - which is 9 * the number of digits in #
# UPB consummate #
INT max sum := 9;
INT v := UPB consummate;
WHILE ( v OVERAB 10 ) > 0 DO max sum +:= 9 OD;
INT max number = UPB consummate * max sum;
# construct the digit sums of the numbers up to max number #
# construct the digit sums of the numbers up to max number #
# and find the consumate numbers, we start the loop from 10 to avoid #
[ 0 : max number ]INT dsum;
INT tn := 0, hn := 0, th := 0, tt := 0, ht := 0, dpos := -1;
# having to deal with 0-9 #
WHILE ht /= 5 DO
consummate[ 1 ] := TRUE;
INT sumd = ht + tt + th + hn + tn;
INT tn := 1, hn := 0, th := 0, tt := 0, ht := 0, mi := 0, tm := 0;
dsum[ dpos +:= 1 ] := sumd;
FOR n FROM 10 BY 10 TO max number DO
dsum[ dpos +:= 1 ] := sumd + 1;
INT sumd := tm + mi + ht + tt + th + hn + tn;
dsum[ dpos +:= 1 ] := sumd + 2;
FOR d FROM n TO n + 9 DO
dsum[ dpos +:= 1 ] := sumd + 3;
IF d MOD sumd = 0 THEN
# d is comsummate #
dsum[ dpos +:= 1 ] := sumd + 4;
dsum[ dpos +:= 1 ] := sumd + 5;
IF INT d ratio = d OVER sumd;
dsum[ dpos +:= 1 ] := sumd + 6;
d ratio <= UPB consummate
dsum[ dpos +:= 1 ] := sumd + 7;
THEN
dsum[ dpos +:= 1 ] := sumd + 8;
consummate[ d ratio ] := TRUE
dsum[ dpos +:= 1 ] := sumd + 9;
FI
FI;
sumd +:= 1
OD;
IF ( tn +:= 1 ) > 9 THEN
IF ( tn +:= 1 ) > 9 THEN
tn := 0;
tn := 0;
IF ( hn +:= 1 ) > 9 THEN
IF ( hn +:= 1 ) > 9 THEN
hn := 0;
hn := 0;
Line 64: Line 72:
IF ( tt +:= 1 ) > 9 THEN
IF ( tt +:= 1 ) > 9 THEN
tt := 0;
tt := 0;
ht +:= 1
IF ( ht +:= 1 ) > 9 THEN
ht := 0;
IF ( mi +:= 1 ) > 9 THEN
mi := 0;
tm +:= 1
FI
FI
FI
FI
FI
FI
FI
FI
FI
OD;
# table of numbers that can be formed by n / dsum[ n ] #
[ 0 : max number ]BOOL consummate;
FOR i FROM LWB consummate TO UPB consummate DO
consummate[ i ] := FALSE
OD;
FOR i TO UPB d sum DO
IF i MOD dsum[ i ] = 0 THEN
consummate[ i OVER dsum[ i ] ] := TRUE
FI
FI
OD;
OD;
INT count := 0;
INT count := 0;
print( ( "The first 50 inconsummate numbers:", newline ) );
print( ( "The first 50 inconsummate numbers:", newline ) );
FOR i TO UPB consummate WHILE count < 1000 DO
FOR i TO UPB consummate WHILE count < 100 000 DO
IF NOT consummate[ i ] THEN
IF NOT consummate[ i ] THEN
IF ( count +:= 1 ) < 51 THEN
IF ( count +:= 1 ) < 51 THEN
print( ( whole( i, -6 ) ) );
print( ( whole( i, -6 ) ) );
IF count MOD 10 = 0 THEN print( ( newline ) ) FI
IF count MOD 10 = 0 THEN print( ( newline ) ) FI
ELIF count = 1 000 THEN
ELIF count = 1 000 OR count = 10 000 OR count = 100 000 THEN
print( ( "Inconsummate number ", whole( count, 0 ), ": ", whole( i, 0 ), newline ) )
print( ( "Inconsummate number ", whole( count, -6 )
, ": ", whole( i, -8 ), newline
)
)
FI
FI
FI
FI
Line 102: Line 109:
441 443 461 466 471 476 482 483 486 488
441 443 461 466 471 476 482 483 486 488
491 492 493 494 497 498 516 521 522 527
491 492 493 494 497 498 516 521 522 527
Inconsummate number 1000: 6996
Inconsummate number 1000: 6996
Inconsummate number 10000: 59853
Inconsummate number 100000: 536081
</pre>
</pre>