Prime reciprocal sum: Difference between revisions
Content added Content deleted
m (→{{header|Free Pascal}}: using presieving with small primes ( 2..65519)) |
(Added Algol 68) |
||
Line 28: | Line 28: | ||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
Uses Algol 68G's LONG LONG INT which has programmer defined precision.<br> |
|||
Re-uses code from the [[Arithmetic/Rational]] task, modified to use LONG LONG INT. |
|||
{{libheader|ALGOL 68-primes}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # find a sequence of primes whose members are the smallest prime whose # |
|||
# reciprocal can be added to the sum or the reciprocals of the # |
|||
# previous primes and the sum remain below 1 # |
|||
PR precision 2500 PR # set the precision of LONG LONG INT # |
|||
PR read "primes.incl.a68" PR # include prime utilities # |
|||
# iterative Greatest Common Divisor routine, returns the gcd of m and n # |
|||
PROC gcd = ( LONG LONG INT m, n )LONG LONG INT: |
|||
BEGIN |
|||
LONG LONG INT a := ABS m, b := ABS n; |
|||
WHILE b /= 0 DO |
|||
LONG LONG INT new a = b; |
|||
b := a MOD b; |
|||
a := new a |
|||
OD; |
|||
a |
|||
END # gcd # ; |
|||
# code from the Arithmetic/Rational task modified to use LONG LONG INT # |
|||
MODE FRAC = STRUCT( LONG LONG INT num #erator#, den #ominator# ); |
|||
PROC lcm = ( LONG LONG INT a, b )LONG LONG INT: # least common multiple # |
|||
a OVER gcd(a, b) * b; |
|||
PRIO // = 9; # higher then the ** operator # |
|||
OP // = ( LONG LONG INT num, den )FRAC: ( # initialise and normalise # |
|||
LONG LONG INT common = gcd( num, den ); |
|||
IF den < 0 THEN |
|||
( -num OVER common, -den OVER common ) |
|||
ELSE |
|||
( num OVER common, den OVER common ) |
|||
FI |
|||
); |
|||
OP + = (FRAC a, b)FRAC: ( |
|||
LONG LONG INT common = lcm( den OF a, den OF b ); |
|||
FRAC result := ( common OVER den OF a * num OF a + common OVER den OF b * num OF b, common ); |
|||
num OF result//den OF result |
|||
); |
|||
OP +:= = (REF FRAC a, FRAC b)REF FRAC: ( a := a + b ); |
|||
# end code from the Arithmetic/Rational task modified to use LONG LONG INT # |
|||
# the sequence starts with the reciprocal of the first prime > 0, i.e. 2 # |
|||
LONG LONG INT one = 1; |
|||
FRAC sum := one // LONG LONG 2; |
|||
print( ( " 1: 2", newline ) ); |
|||
# each succeeding prime is the next prime > the denominator of the sum # |
|||
# divided by the difference of the denominator and the numerator # |
|||
FOR i FROM 2 TO 14 DO |
|||
LONG LONG INT next := IF num OF sum + 1 = den OF sum |
|||
THEN den OF sum + 1 |
|||
ELSE ( den OF sum OVER ( den OF sum - num OF sum ) ) + 1 |
|||
FI; |
|||
IF NOT ODD next THEN next +:= 1 FI; |
|||
WHILE NOT is probably prime( next ) DO next +:= 2 OD; |
|||
print( ( whole( i, -2 ), ": " ) ); |
|||
STRING prime text = whole( next, 0 ); |
|||
IF INT digits = ( UPB prime text - LWB prime text ) + 1; |
|||
digits <= 40 |
|||
THEN |
|||
print( ( prime text ) ) |
|||
ELSE |
|||
print( ( prime text[ : LWB prime text + 19 ], "..." |
|||
, prime text[ UPB prime text - 19 : ], " " |
|||
, whole( digits, -6 ), " digits" |
|||
) |
|||
) |
|||
FI; |
|||
print( ( newline ) ); |
|||
sum +:= one // next |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1: 2 |
|||
2: 3 |
|||
3: 7 |
|||
4: 43 |
|||
5: 1811 |
|||
6: 654149 |
|||
7: 27082315109 |
|||
8: 153694141992520880899 |
|||
9: 337110658273917297268061074384231117039 |
|||
10: 84241975970641143191...13803869133407474043 76 digits |
|||
11: 20300753813848234767...91313959045797597991 150 digits |
|||
12: 20323705381471272842...21649394434192763213 297 digits |
|||
13: 12748246592672078196...20708715953110886963 592 digits |
|||
14: 46749025165138838243...65355869250350888941 1180 digits |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |