Yellowstone sequence: Difference between revisions

Content added Content deleted
m (→‎a simple plot: IupCloseonEscape no longer needed)
(Added Delphi example)
Line 393: Line 393:
{{out}}
{{out}}
<pre>[1, 2, 3, 4, 9, 8, 15, 14, 5, 6, 25, 12, 35, 16, 7, 10, 21, 20, 27, 22, 39, 11, 13, 33, 26, 45, 28, 51, 32, 17]</pre>
<pre>[1, 2, 3, 4, 9, 8, 15, 14, 5, 6, 25, 12, 35, 16, 7, 10, 21, 20, 27, 22, 39, 11, 13, 33, 26, 45, 28, 51, 32, 17]</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| Boost.Generics.Collection}}
{{libheader| Boost.Process}}
{{Trans|Go}}
Boost.Generics.Collection and Boost.Process are part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<lang Delphi>
program Yellowstone_sequence;

{$APPTYPE CONSOLE}

uses
System.SysUtils,
Boost.Generics.Collection,
Boost.Process;

function gdc(x, y: Integer): Integer;
begin
while y <> 0 do
begin
var tmp := x;
x := y;
y := tmp mod y;
end;
Result := x;
end;

function Yellowstone(n: Integer): TArray<Integer>;
var
m: TDictionary<Integer, Boolean>;
a: TArray<Integer>;
begin
m.Init;
SetLength(a, n + 1);
for var i := 1 to 3 do
begin
a[i] := i;
m[i] := True;
end;

var min := 4;

for var c := 4 to n do
begin
var i := min;
repeat
if not m[i, false] and (gdc(a[c - 1], i) = 1) and (gdc(a[c - 2], i) > 1) then
begin
a[c] := i;
m[i] := true;
if i = min then
inc(min);
Break;
end;
inc(i);
until false;
end;

Result := copy(a, 1, length(a));
end;

begin
var x: TArray<Integer>;
SetLength(x, 100);
for var i in Range(100) do
x[i] := i + 1;

var y := yellowstone(High(x));

writeln('The first 30 Yellowstone numbers are:');
for var i := 0 to 29 do
Write(y[i], ' ');
Writeln;

//Plotting

var plot := TPipe.Create('gnuplot -p', True);
plot.WritelnA('unset key; plot ''-''');

for var i := 0 to High(x) do
plot.WriteA('%d %d'#10, [x[i], y[i]]);
plot.WritelnA('e');

writeln('Press enter to close');
Readln;
plot.Kill;
plot.Free;
end.</lang>
{{out}}
<pre>The first 30 Yellowstone numbers are:
1 2 3 4 9 8 15 14 5 6 25 12 35 16 7 10 21 20 27 22 39 11 13 33 26 45 28 51 32 17
Press enter to close</pre>


=={{header|Factor}}==
=={{header|Factor}}==