Prime reciprocal sum: Difference between revisions
Content deleted Content added
Added C |
→{{header|J}}: append Free Pascal |
||
Line 138:
11390125639471674628..31060548964273180103 (2358 digits)
</syntaxhighlight>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Most time consuming is finding the next prime.
<syntaxhighlight lang=pascal>
program primeRezSum;
{$IFDEF FPC} {$MODE DELPHI}{$Optimization ON,ALL} {$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils,
gmp;
procedure OutStr(const s: AnsiString);
var
myString : AnsiString;
l : Integer;
begin
myString := copy(s,1,length(s));
l :=length(pChar(@s[1]));
setlength(myString,l);
IF l< 41 then
writeln(myString)
else
begin
myString[20]:= #0;
myString[21]:= #0;
writeln(pChar(@myString[1]),'...',pChar(@myString[l-19]),' (',l:6,' digits)');
end;
end;
var
nominator,denominator,tmp,tmpDemP,p : mpz_t;
s : AnsiString;
cnt : NativeUint;
begin
setlength(s,10000);
cnt := 1;
mpz_init(nominator);
mpz_init(tmp);
mpz_init(tmpDemP);
mpz_init_set_ui(denominator,1);
mpz_init_set_ui(p,2);
repeat
mpz_sub_ui(p,p,1);
mpz_nextprime(p,p);
write(cnt:3,' ');
repeat
mpz_mul(tmp,nominator,p);
mpz_add(tmp,tmp,denominator);
mpz_mul(tmpDemP,denominator,p);
if mpz_cmp(tmp,tmpDemP)< 0 then
BREAK;
mpz_nextprime (p,p);
until false;
mpz_get_str(pChar(@s[1]),10, p);
OutStr(s);
mpz_set(nominator,tmp);
mpz_mul(denominator,denominator,p);
//next smallest possible number denominator/delta
mpz_sub(tmp,denominator,nominator);
mpz_fdiv_q(p,denominator,tmp);
inc(cnt);
until cnt> 14;
end.</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
1 2
2 3
3 7
4 43
5 1811
6 654149
7 27082315109
8 153694141992520880899
9 337110658273917297268061074384231117039
10 8424197597064114319...13803869133407474043 ( 76 digits)
11 2030075381384823476...91313959045797597991 ( 150 digits)
12 2032370538147127284...21649394434192763213 ( 297 digits)
13 1274824659267207819...20708715953110886963 ( 592 digits)
14 4674902516513883824...65355869250350888941 ( 1180 digits)
Real time: 1.147 s User time: 1.093 s Sys. time: 0.046 s CPU share: 99.28 %</pre>
=={{header|Julia}}==
|