Sisyphus sequence: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: Off by one)
(→‎{{header|Perl}}: prepend ==={{header|Free Pascal}=== 36 found in ~16,5 min)
Line 557: Line 557:
Numbers under 250 which occur the most in the first 100_000_000 terms:
Numbers under 250 which occur the most in the first 100_000_000 terms:
7 28 14
7 28 14
</pre>

=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program Sisyphus;
{$IFDEF FPC}
{$MODE DELPHI}{$Coperators ON}{$Optimization ON}
{$ENDIF}
{$IFDEF WINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils,
primsieve;// https://rosettacode.org/wiki/Extensible_prime_generator#Pascal

function CommatizeUint64(num:Uint64):AnsiString;
var
fromIdx,toIdx :Int32;
Begin
str(num,result);
fromIdx := length(result);
toIdx := fromIdx-1;
if toIdx < 3 then
exit;

toIdx := 4*(toIdx DIV 3)+toIdx MOD 3 +1 ;
setlength(result,toIdx);
repeat
result[toIdx] := result[FromIdx];
result[toIdx-1] := result[FromIdx-1];
result[toIdx-2] := result[FromIdx-2];
result[toIdx-3] := ',';
dec(toIdx,4);
dec(FromIdx,3);
until FromIdx<=3;
end;

procedure OutOne(n,count: Uint64);
Begin
write(n:5);
If count mod 10 = 0 then
writeln;
end;

procedure OutRes(n,count,p:Uint64);
Begin
writeln(CommatizeUint64(n):15, ' found after ',CommatizeUint64(count):12,' iterations: Max prime ',CommatizeUint64(p):12);
end;

procedure CheckSmall;
var
CntOccurence : array[0..255] of byte;
n,count,p,limit : Uint64;
begin
InitPrime;
writeln('The first 100 members of the Sisyphus sequence');
fillchar(CntOccurence,SizeOf(CntOccurence),#0);
n := 1;
count := 1;
Limit := 1000;
CntOccurence[n] := 1;

repeat
if count <= 100 then
Begin
OutOne(n,count);
if count = 100 then
writeln;
end;

if n AND 1 = 0 then
n := n shr 1
else
begin
p := nextprime;
n += p;
end;
count+= 1;
iF n < 255 then
inc(CntOccurence[n]);

if (count = Limit) then
Begin
OutRes(n,count,p);
Limit *= 10;
end;
until Limit > 100*1000*1000;

writeln(#10,'Not found numbers below 250 after ',CommatizeUint64(count),' iterations');
p := 0;// maximum
For n := 1 to 250 do
begin
if CntOccurence[n] = 0 then
write(n:4);
if p < CntOccurence[n] then
p := CntOccurence[n];
end;
Writeln;

writeln(#10,'Mostly found numbers below 250 after ',CommatizeUint64(count),' iterations');
For n := 1 to 250 do
if CntOccurence[n] = p then
write(n:4);
Writeln(' found ',p,' times');
end;

procedure Check;
var
n,count,target,p : Uint64;
begin
InitPrime;
n := 1;
count := 1;
Target := 36;
repeat
if n AND 1 = 0 then
n := n shr 1
else
begin
p := nextprime;
n += p;
end;
count+= 1;
if (n = target) then
Begin
OutRes(n,count,p);
BREAK;
end;
until false;
end;

BEGIN
CheckSmall;
writeln;
CHECK;
end.</syntaxhighlight>
{{out|@Home Ryzen 5600G 3.6 Ghz }}
<pre>
The first 100 members of the Sisyphus sequence
1 3 6 3 8 4 2 1 8 4
2 1 12 6 3 16 8 4 2 1
18 9 28 14 7 30 15 44 22 11
42 21 58 29 70 35 78 39 86 43
96 48 24 12 6 3 62 31 92 46
23 90 45 116 58 29 102 51 130 65
148 74 37 126 63 160 80 40 20 10
5 106 53 156 78 39 146 73 182 91
204 102 51 178 89 220 110 55 192 96
48 24 12 6 3 142 71 220 110 55

990 found after 1,000 iterations: Max prime 2,273
24,975 found after 10,000 iterations: Max prime 30,713
265,781 found after 100,000 iterations: Max prime 392,111
8,820,834 found after 1,000,000 iterations: Max prime 4,761,697
41,369,713 found after 10,000,000 iterations: Max prime 55,900,829
1,179,614,168 found after 100,000,000 iterations: Max prime 640,692,323

Not found numbers below 250 after 100,000,000 iterations
36 72 97 107 115 127 144 167 194 211 214 230 232

Mostly found numbers below 250 after 100,000,000 iterations
7 14 28 found 7 times
--0.63 s
36 found after 77,534,485,877 iterations: Max prime 677,121,348,413

real 16m20.573s
</pre>
</pre>