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 # |
||
# table of numbers that can be formed by n / digit sum n # |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# maximum number to test would be 45 x 9 999 # |
|||
# calculate the maximum number we must consider to find consummate # |
|||
⚫ | |||
# numbers up to UPB consummate - which is 9 * the number of digits in # |
|||
⚫ | |||
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; |
|||
# having to deal with 0-9 # |
|||
consummate[ 1 ] := TRUE; |
|||
INT tn := 1, hn := 0, th := 0, tt := 0, ht := 0, mi := 0, tm := 0; |
|||
FOR n FROM 10 BY 10 TO max number DO |
|||
INT sumd := tm + mi + ht + tt + th + hn + tn; |
|||
FOR d FROM n TO n + 9 DO |
|||
IF d MOD sumd = 0 THEN |
|||
# d is comsummate # |
|||
dsum[ dpos +:= 1 ] := sumd + 4; |
|||
IF INT d ratio = d OVER sumd; |
|||
d ratio <= UPB consummate |
|||
THEN |
|||
consummate[ d ratio ] := TRUE |
|||
FI |
|||
⚫ | |||
sumd +:= 1 |
|||
⚫ | |||
IF ( tn +:= 1 ) > 9 THEN |
IF ( tn +:= 1 ) > 9 THEN |
||
tn |
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; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
FI |
|||
FI |
|||
FI |
FI |
||
FI |
FI |
||
FI |
FI |
||
⚫ | |||
⚫ | |||
# table of numbers that can be formed by n / dsum[ n ] # |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
FOR i TO UPB d sum DO |
|||
⚫ | |||
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 < |
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, |
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> |
||