Arithmetic numbers: Difference between revisions

Content deleted Content added
PureFox (talk | contribs)
Promoted to 'full' task - plenty of implementations, minor problem fixed.
Added Algol 68
Line 29:
<br><br>
 
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find arithmetic numbers - numbers whose average divisor is an integer #
# i.e. sum of divisors MOD count of divisors = 0 #
INT max number = 500 000; # maximum number we will consider #
[ 1 : max number ]INT d sum;
[ 1 : max number ]INT d count;
# all positive integers are divisible by 1 and so have at least 1 divisor #
FOR i TO max number DO d sum[ i ] := d count[ i ] := 1 OD;
# construct the divisor sums and counts #
FOR i FROM 2 TO max number OVER 2 DO
FOR j FROM i BY i TO max number DO
d count[ j ] +:= 1;
d sum[ j ] +:= i
OD
OD;
# count arithmetic numbers, and show the first 100, the 1 000th, 10 000th #
# and the 100 000th and show how many are composite #
INT max arithmetic = 100 000;
INT a count := 0;
INT c count := 0;
FOR i TO max number WHILE a count < max arithmetic DO
IF d sum[ i ] MOD d count[ i ] = 0 THEN
# have an arithmetic number #
IF d count[ i ] > 2 THEN
# the number is composite #
c count +:= 1
FI;
a count +:= 1;
IF a count <= 100 THEN
print( ( " ", whole( i, -3 ) ) );
IF a count MOD 10 = 0 THEN print( ( newline ) ) FI
ELIF a count = 1 000
OR a count = 10 000
OR a count = 100 000
THEN
print( ( newline ) );
print( ( "The ", whole( a count, 0 )
, "th arithmetic number is: ", whole( i, 0 )
, newline
)
);
print( ( " There are ", whole( c count, 0 )
, " composite arithmetic numbers up to ", whole( i, 0 )
, newline
)
)
FI
FI
OD
END</lang>
{{out}}
<pre>
1 3 5 6 7 11 13 14 15 17
19 20 21 22 23 27 29 30 31 33
35 37 38 39 41 42 43 44 45 46
47 49 51 53 54 55 56 57 59 60
61 62 65 66 67 68 69 70 71 73
77 78 79 83 85 86 87 89 91 92
93 94 95 96 97 99 101 102 103 105
107 109 110 111 113 114 115 116 118 119
123 125 126 127 129 131 132 133 134 135
137 138 139 140 141 142 143 145 147 149
 
The 1000th arithmetic number is: 1361
There are 782 composite arithmetic numbers up to 1361
 
The 10000th arithmetic number is: 12953
There are 8458 composite arithmetic numbers up to 12953
 
The 100000th arithmetic number is: 125587
There are 88219 composite arithmetic numbers up to 125587
</pre>
 
=={{header|BASIC}}==
Line 172 ⟶ 245:
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
 
=={{header|C++}}==