Numbers which are the cube roots of the product of their proper divisors: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 19:
<br>
 
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32 and 3.0.3}}
Whilst deceptively simple, the task reuires large integers as some of the divisor prioducts needed are large. This ignores numbers where the divisor product is going to be larger than the cube of the maximum number it considers.
<br>
Reuires an implementation of Algol 68 where LONG INT is longer than 64 bits, in ALGOL 68G, LONG INT is 128 bit in version 3 and allows up to 35 digits in version 2. Constructs a table of proper divisor products to avoid factorising each number. In order to run this under Windows (and probably other operating systems) with Algol 68G, a large heap size must be specified, e.g. with <code>-heap 256M</code> of the command line.
<syntaxhighlight lang="algol68">
BEGIN # find some numbers which are the cube roots of the product of their #
# proper divisors #
INT max number = 500 000; # maximum number we will consider #
# form a table of proper divisor products - assume the pdp of 1 is 1 #
LONG INT max product = LENG max number * LENG max number * LENG max number;
[ 1 : max number ]LONG INT pdp; FOR i TO UPB pdp DO pdp[ i ] := IF ODD i THEN 1 ELSE 2 FI OD;
FOR i FROM 3 TO UPB pdp DO
FOR j FROM i + i BY i TO UPB pdp DO
IF pdp[ j ] < 0 THEN
# the product has already overflowed #
SKIP
ELIF pdp[ j ] >= max product THEN
# the product will be too large to be a cube of interest #
pdp[ j ] := -1
ELSE
# the product hasn't overflowed yet #
pdp[ j ] *:= i
FI
OD
OD;
# show the numbers which are the cube root of their pdp, i.e. wh #
# the pdp is the cube of the number #
INT next show := 500;
INT c count := 0;
FOR n TO UPB pdp DO
IF LONG INT n3 = LENG n * LENG n * LENG n;
n3 = pdp[ n ]
THEN
# found a suitable number #
IF ( c count +:= 1 ) <= 50 THEN
print( ( " ", whole( n, -3 ) ) );
IF c count MOD 10 = 0 THEN print( ( newline ) ) FI
ELIF c count = next show THEN
print( ( whole( c count, -9 ), "th: ", whole( n, 0 ), newline ) );
next show *:= 10
FI
FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
1 24 30 40 42 54 56 66 70 78
88 102 104 105 110 114 128 130 135 136
138 152 154 165 170 174 182 184 186 189
190 195 222 230 231 232 238 246 248 250
255 258 266 273 282 285 286 290 296 297
500th: 2526
5000th: 23118
50000th: 223735
</pre>
 
=={{header|J}}==
3,048

edits