Yellowstone sequence: Difference between revisions

m
 
(43 intermediate revisions by 27 users not shown)
Line 22:
 
;Task
: Find and show as output the first  '''30'''  Yellowstone numbers.
 
 
Line 32:
:*   [https://rosettacode.org/wiki/Greatest_common_divisor Greatest common divisor].
:*   [https://rosettacode.org/wiki/Plot_coordinate_pairs Plot coordinate pairs].
:*   [[EKG sequence convergence]]
 
 
Line 38 ⟶ 39:
:*   Applegate et al, 2015: The Yellowstone Permutation [https://arxiv.org/abs/1501.01669].
<br><br>
 
=={{header|11l}}==
{{trans|C++}}
 
<syntaxhighlight lang="11l">T YellowstoneGenerator
min_ = 1
n_ = 0
n1_ = 0
n2_ = 0
Set[Int] sequence_
 
F next()
.n2_ = .n1_
.n1_ = .n_
I .n_ < 3
.n_++
E
.n_ = .min_
L !(.n_ !C .sequence_ & gcd(.n1_, .n_) == 1 & gcd(.n2_, .n_) > 1)
.n_++
.sequence_.add(.n_)
L
I .min_ !C .sequence_
L.break
.sequence_.remove(.min_)
.min_++
R .n_
 
print(‘First 30 Yellowstone numbers:’)
V ygen = YellowstoneGenerator()
print(ygen.next(), end' ‘’)
L(i) 1 .< 30
print(‘ ’ygen.next(), end' ‘’)
print()</syntaxhighlight>
 
{{out}}
<pre>
First 30 Yellowstone numbers:
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>
 
{{trans|Ruby}}
 
<syntaxhighlight lang="11l">F yellow(n)
V a = [1, 2, 3]
V b = Set([1, 2, 3])
V i = 4
L n > a.len
I i !C b & gcd(i, a.last) == 1 & gcd(i, a[(len)-2]) > 1
a.append(i)
b.add(i)
i = 4
i++
R a
 
print(yellow(30))</syntaxhighlight>
 
{{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>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC Gcd(BYTE a,b)
BYTE tmp
 
IF a<b THEN
tmp=a a=b b=tmp
FI
 
WHILE b#0
DO
tmp=a MOD b
a=b b=tmp
OD
RETURN (a)
 
BYTE FUNC Contains(BYTE ARRAY a BYTE len,value)
BYTE i
 
FOR i=0 TO len-1
DO
IF a(i)=value THEN
RETURN (1)
FI
OD
RETURN (0)
 
PROC Generate(BYTE ARRAY seq BYTE count)
BYTE i,x
 
seq(0)=1 seq(1)=2 seq(2)=3
FOR i=3 TO COUNT-1
DO
x=1
DO
IF Contains(seq,i,x)=0 AND
Gcd(x,seq(i-1))=1 AND Gcd(x,seq(i-2))>1 THEN
EXIT
FI
x==+1
OD
seq(i)=x
OD
RETURN
 
PROC Main()
DEFINE COUNT="30"
BYTE ARRAY seq(COUNT)
BYTE i
 
Generate(seq,COUNT)
PrintF("First %B Yellowstone numbers:%E",COUNT)
FOR i=0 TO COUNT-1
DO
PrintB(seq(i)) Put(32)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Yellowstone_sequence.png Screenshot from Atari 8-bit computer]
<pre>
First 30 Yellowstone numbers:
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|Ada}}==
{{trans|C++}}
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Ordered_Sets;
 
procedure Yellowstone_Sequence is
 
generic -- Allow more than one generator, but must be instantiated
package Yellowstones is
function Next return Integer;
function GCD (Left, Right : Integer) return Integer;
end Yellowstones;
 
package body Yellowstones
is
package Sequences is
new Ada.Containers.Ordered_Sets (Integer);
 
-- Internal package state
N_0 : Integer := 0;
N_1 : Integer := 0;
N_2 : Integer := 0;
Seq : Sequences.Set;
Min : Integer := 1;
 
function GCD (Left, Right : Integer) return Integer
is (if Right = 0
then Left
else GCD (Right, Left mod Right));
 
function Next return Integer is
begin
N_2 := N_1;
N_1 := N_0;
if N_0 < 3 then
N_0 := N_0 + 1;
else
N_0 := Min;
while
not (not Seq.Contains (N_0)
and then GCD (N_1, N_0) = 1
and then GCD (N_2, N_0) > 1)
loop
N_0 := N_0 + 1;
end loop;
end if;
Seq.Insert (N_0);
while Seq.Contains (Min) loop
Seq.Delete (Min);
Min := Min + 1;
end loop;
return N_0;
end Next;
 
end Yellowstones;
 
procedure First_30 is
package Yellowstone is new Yellowstones; -- New generator instance
use Ada.Text_IO;
begin
Put_Line ("First 30 Yellowstone numbers:");
for A in 1 .. 30 loop
Put (Yellowstone.Next'Image); Put (" ");
end loop;
New_Line;
end First_30;
 
begin
First_30;
end Yellowstone_Sequence;</syntaxhighlight>
{{out}}
<pre>
First 30 Yellowstone numbers:
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|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # find members of the yellowstone sequence: starting from 1, 2, 3 the #
# subsequent members are the lowest number coprime to the previous one #
# and not coprime to the one before that, that haven't appeared in the #
# sequence yet #
# iterative Greatest Common Divisor routine, returns the gcd of m and n #
PROC gcd = ( INT m, n )INT:
BEGIN
INT a := ABS m, b := ABS n;
WHILE b /= 0 DO
INT new a = b;
b := a MOD b;
a := new a
OD;
a
END # gcd # ;
# returns an array of the Yellowstone seuence up to n #
OP YELLOWSTONE = ( INT n )[]INT:
BEGIN
[ 1 : n ]INT result;
IF n > 0 THEN
result[ 1 ] := 1;
IF n > 1 THEN
result[ 2 ] := 2;
IF n > 2 THEN
result[ 3 ] := 3;
# guess the maximum element will be n, if it is larger, used will be enlarged #
REF[]BOOL used := HEAP[ 1 : n ]BOOL;
used[ 1 ] := used[ 2 ] := used[ 3 ] := TRUE;
FOR i FROM 4 TO UPB used DO used[ i ] := FALSE OD;
FOR i FROM 4 TO UPB result DO
INT p1 = result[ i - 1 ];
INT p2 = result[ i - 2 ];
BOOL found := FALSE;
FOR j WHILE NOT found DO
IF j > UPB used THEN
# not enough elements in used - enlarge it #
REF[]BOOL new used := HEAP[ 1 : 2 * UPB used ]BOOL;
new used[ 1 : UPB used ] := used;
FOR k FROM UPB used + 1 TO UPB new used DO new used[ k ] := FALSE OD;
used := new used
FI;
IF NOT used[ j ] THEN
IF found := gcd( j, p1 ) = 1 AND gcd( j, p2 ) /= 1
THEN
result[ i ] := j;
used[ j ] := TRUE
FI
FI
OD
OD
FI
FI
FI;
result
END # YELLOWSTONE # ;
[]INT ys = YELLOWSTONE 30;
FOR i TO UPB ys DO
print( ( " ", whole( ys[ i ], 0 ) ) )
OD
END</syntaxhighlight>
{{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>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">yellowstone: function [n][
result: new [1 2 3]
present: new [1 2 3]
start: new 4
while [n > size result][
candidate: new start
while ø [
if all? @[
not? contains? present candidate
1 = gcd @[candidate last result]
1 <> gcd @[candidate get result (size result)-2]
][
'result ++ candidate
'present ++ candidate
while [contains? present start] -> inc 'start
break
]
inc 'candidate
]
]
return result
]
 
print yellowstone 30</syntaxhighlight>
 
{{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>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">A := [], in_seq := []
loop 30 {
n := A_Index
if n <=3
A[n] := n, in_seq[n] := true
else while true
{
s := A_Index
if !in_seq[s] && relatively_prime(s, A[n-1]) && !relatively_prime(s, A[n-2])
{
A[n] := s
in_seq[s] := true
break
}
}
}
for i, v in A
result .= v ","
MsgBox % result := "[" Trim(result, ",") "]"
return
;--------------------------------------
relatively_prime(a, b){
return (GCD(a, b) = 1)
}
;--------------------------------------
GCD(a, b) {
while b
b := Mod(a | 0x0, a := b)
return a
}</syntaxhighlight>
{{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>
 
=={{header|C}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 285 ⟶ 617:
putc('\n', stdout);
return 0;
}</langsyntaxhighlight>
{{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>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <numeric>
#include <set>
Line 333 ⟶ 665:
std::cout << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 343 ⟶ 675:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.numeric;
import std.range;
import std.stdio;
Line 391 ⟶ 723:
void main() {
new Yellowstone().take(30).writeln();
}</langsyntaxhighlight>
{{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>
Line 400 ⟶ 732:
{{Trans|Go}}
Boost.Generics.Collection and Boost.Process are part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Yellowstone_sequence;
 
Line 481 ⟶ 813:
plot.Kill;
plot.Free;
end.</langsyntaxhighlight>
{{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|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
func gcd a b .
if b = 0
return a
.
return gcd b (a mod b)
.
proc remove_at i . a[] .
for j = i + 1 to len a[]
a[j - 1] = a[j]
.
len a[] -1
.
proc yellowstone count . yellow[] .
yellow[] = [ 1 2 3 ]
num = 4
while len yellow[] < count
yell1 = yellow[len yellow[] - 1]
yell2 = yellow[len yellow[]]
for i to len notyellow[]
test = notyellow[i]
if gcd yell1 test > 1 and gcd yell2 test = 1
break 1
.
.
if i <= len notyellow[]
yellow[] &= notyellow[i]
remove_at i notyellow[]
else
while gcd yell1 num <= 1 or gcd yell2 num <> 1
notyellow[] &= num
num += 1
.
yellow[] &= num
num += 1
.
.
.
print "First 30 values in the yellowstone sequence:"
yellowstone 30 yellow[]
print yellow[]
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: accessors assocs colors.constants
combinators.short-circuit io kernel math prettyprint sequences
sets ui ui.gadgets ui.gadgets.charts ui.gadgets.charts.lines ;
Line 522 ⟶ 899:
line new COLOR: blue >>color
100 <iota> 100 <yellowstone> zip >>data
add-gadget "Yellowstone numbers" open-window</langsyntaxhighlight>
{{out}}
<pre>
Line 528 ⟶ 905:
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|Forth}}==
<syntaxhighlight lang="text">: array create cells allot ;
: th cells + ; \ some helper words
 
30 constant #yellow \ number of yellowstones
 
#yellow array y \ create array
( n1 n2 -- n3)
: gcd dup if tuck mod recurse exit then drop ;
: init 3 0 do i 1+ y i th ! loop ; ( --)
: show cr #yellow 0 do y i th ? loop ; ( --)
: gcd-y[] - cells y + @ over gcd ; ( k i n -- k gcd )
: loop1 begin 1+ over 2 gcd-y[] 1 = >r over 1 gcd-y[] 1 > r> or 0= until ;
: loop2 over true swap 0 ?do over y i th @ = if 0= leave then loop ;
: yellow #yellow 3 do i 3 begin loop1 loop2 until y rot th ! loop ;
: main init yellow show ;
 
main</syntaxhighlight>
{{out}}
<pre>main
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 ok</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function gcd(a as uinteger, b as uinteger) as uinteger
if b = 0 then return a
return gcd( b, a mod b )
Line 564 ⟶ 963:
while inkey=""
wend
end</langsyntaxhighlight>
{{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>
Line 570 ⟶ 969:
=={{header|Go}}==
This uses Gnuplot-X11 to do the plotting rather than a third party Go plotting library.
<langsyntaxhighlight lang="go">package main
 
import (
Line 633 ⟶ 1,032:
w.Close()
g.Wait()
}</langsyntaxhighlight>
 
{{out}}
Line 642 ⟶ 1,041:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr)
 
yellowstone :: [Integer]
yellowstone = 1 : 2 : 3 : unfoldr (Just . f) (2, 3, [4 ..]) where
where
f :: (Integer, Integer, [Integer]) -> (Integer, (Integer, Integer, [Integer]))
f ::
f (p2, p1, rest) = (next, (p1, next, rest_)) where
(nextInteger, rest_)Integer, = select[Integer]) rest->
select :: [(Integer] ->, (Integer, Integer, [Integer]))
f (p2, p1, rest) = (next, (p1, next, rest_))
select (x:xs)
where
| gcd x p1 == 1 && gcd x p2 /= 1 = (x, xs)
| otherwise = (ynext, x:ysrest_) = select rest
select :: [Integer] -> (Integer, [Integer])
where (y, ys) = select xs
select (x : xs)
| gcd x p1 == 1 && gcd x p2 /= 1 = (x, xs)
| otherwise = (y, x : ys)
where
(y, ys) = select xs
 
main :: IO ()
main = print $ take 30 yellowstone</langsyntaxhighlight>
 
{{out}}
Line 666 ⟶ 1,070:
and displaying a chart of the first 100 terms:
 
<syntaxhighlight lang ="haskell">import qualified GraphicsCodec.SVGFonts.ReadFont as FPicture
import Data.Bifunctor (second)
import Graphics.Rendering.Chart.Backend.Diagrams
import Graphics.Rendering.Chart.Easy
import Diagrams.Backend.Rasterific
import Diagrams.Prelude
import Graphics.Rendering.Chart.Backend.Diagrams
import Codec.Picture
import DataGraphics.Bifunctor (second)Rendering.Chart.Easy
import qualified Graphics.SVGFonts.ReadFont as F
 
----------------- YELLOWSTONE PERMUTATION ------------------
yellowstone :: [Integer]
yellowstone =
yellowstone = 1 : 2 : (active <$> iterate nextWindow (2, 3, [4 ..]))
1 :
2 :
(active <$> iterate nextWindow (2, 3, [4 ..]))
where
nextWindow (p2, p1, rest) = (p1, n, residue)
where
[rp2, rp1] = relativelyPrime <$> [p2, p1]
go (x : xs)
| rp1 x && not (rp2 x) = (x, xs)
| otherwise = second ((:) x) (go xs)
Line 690 ⟶ 1,097:
relativelyPrime a b = 1 == gcd a b
 
---------- 30 FIRST TERMS, AND CHART OF FIRST 100 ----------
main :: IO (Image PixelRGBA8)
main = do
Line 697 ⟶ 1,104:
return $
chartRender env $
plot
plot (line "Yellowstone terms" [zip [1 ..] (take 100 yellowstone)])
( line
"Yellowstone terms"
[zip [1 ..] (take 100 yellowstone)]
)
 
--------------------- CHART GENERATION -------------------
 
chartRender ::
--------------------- CHART GENERATION ---------------------
(Default r, ToRenderable r) =>
chartRender
DEnv Double ->
:: (Default r, ToRenderable r)
=> DEnv Double -> EC r () -> Image PixelRGBA8
Image PixelRGBA8
chartRender env ec =
renderDia
renderDia Rasterific (RasterificOptions (mkWidth (fst (envOutputSize env)))) $
Rasterific
fst $ runBackendR env (toRenderable (execEC ec))
( RasterificOptions
(mkWidth (fst (envOutputSize env)))
)
$ fst $ runBackendR env (toRenderable (execEC ec))
 
------------------------ LOCAL FONT ------------------------
chartEnv :: IO (DEnv Double)
chartEnv = do
Line 714 ⟶ 1,130:
sansRB <- F.loadFont "SourceSansPro_RB.svg"
let fontChosen fs =
case ( _font_name fs, _font_slant fs, _font_weight fs) of
_font_slant fs,
("sans-serif", FontSlantNormal, FontWeightNormal) -> sansR
_font_weight fs
("sans-serif", FontSlantNormal, FontWeightBold) -> sansRB
) of
return $ createEnv vectorAlignmentFns 640 400 fontChosen</lang>
( "sans-serif",
FontSlantNormal,
FontWeightNormal
) -> sansR
( "sans-serif",
FontSlantNormal,
FontWeightBold
) -> sansRB
return $ createEnv vectorAlignmentFns 640 400 fontChosen</syntaxhighlight>
{{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>
Line 723 ⟶ 1,148:
=={{header|J}}==
===tacit===
<syntaxhighlight lang="j">
<lang J>
Until=: 2 :'u^:(0-:v)^:_'
assert 44 -: >:Until(>&43) 32 NB. increment until exceeding 43
Line 735 ⟶ 1,160:
ys=: (append term)@]^:(0 >. _3+[) prepare
assert (ys 30) -: 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
</syntaxhighlight>
</lang>
===explicit===
<syntaxhighlight lang="j">
<lang J>
GCD=: +.
relatively_prime=: 1 = GCD
 
yellowstone=: monad define{{
s=. 1 2 3 NB. initial sequence
start=. #\ i. 4 + y NB. prepare minimal starting values
while. y > # s do.
s=. 3 {. start NB. the sequence vector
z=. <./(1+s)-.s NB. lowest positive inteeger not in sequence
start=. 3 }. start
while. yif. >0 #1 -: z relatively_prime _2{.s do. z e. s end. do.
z=. z+1
z=. {. start NB. z is the lowest number not in the sequence
end. NB. find next value for sequence
while.do.
s=. s, z
if. 0 1 -: (_2 {. s) relatively_prime z do.
if. z -.@e. s do.
break.
end.
end.
z =. >: z
end.
}}
start=. start -. z NB. remove z from the list of starting values
</syntaxhighlight>
s=. s , z
end.
s
)
</lang>
<pre>
yellowstone 30
Line 768 ⟶ 1,184:
'marker'plot yellowstone 100
</pre>
 
[https://jsoftware.github.io/j-playground/bin/html2/#code=GCD%3D%3A%20%2B.%0Arelatively_prime%3D%3A%201%20%3D%20GCD%0A%0Ayellowstone%3D%3A%20%7B%7B%0A%20%20s%3D.%201%202%203%20%20%20%20%20%20%20%20%20%20%20%20NB.%20initial%20sequence%0A%20%20while.%20y%20%3E%20%23%20s%20do.%0A%20%20%20%20z%3D.%20%3C.%2F%281%2Bs%29-.s%20%20%20%20NB.%20lowest%20positive%20inteeger%20not%20in%20sequence%0A%20%20%20%20while.%20if.%200%201%20-%3A%20z%20relatively_prime%20_2%7B.s%20do.%20z%20e.%20s%20end.%20do.%0A%20%20%20%20%20%20z%3D.%20z%2B1%0A%20%20%20%20end.%20%20%20NB.%20find%20next%20value%20for%20sequence%0A%20%20%20%20s%3D.%20s%2C%20z%0A%20%20end.%0A%7D%7D%0A%0Arequire'plot'%0A'marker'plot%20yellowstone%20100 try it online]
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 825 ⟶ 1,243:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 836 ⟶ 1,254:
{{Trans|Python}}
{{Works with|ES6}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,026 ⟶ 1,444:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{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>
 
=={{header|jq}}==
{{works with|jq}}
<syntaxhighlight lang="jq">
# jq optimizes the recursive call of _gcd in the following:
def gcd(a;b):
def _gcd:
if .[1] != 0 then [.[1], .[0] % .[1]] | _gcd else .[0] end;
[a,b] | _gcd ;
 
# emit the yellowstone sequence as a stream
def yellowstone:
1,2,3,
({ a: [2, 3], # the last two items only
b: {"1": true, "2": true, "3" : true}, # a record, to avoid having to save the entire history
start: 4 }
| foreach range(1; infinite) as $n (.;
first(
.b as $b
| .start = first( range(.start;infinite) | select($b[tostring]|not) )
| foreach range(.start; infinite) as $i (.;
.emit = null
| ($i|tostring) as $is
| if .b[$is] then .
# "a(n) is relatively prime to a(n-1) and is not relatively prime to a(n-2)"
elif (gcd($i; .a[1]) == 1) and (gcd($i; .a[0]) > 1)
then .emit = $i
| .a = [.a[1], $i]
| .b[$is] = true
else .
end;
select(.emit)) );
.emit ));
</syntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq">"The first 30 entries of the Yellowstone permutation:",
[limit(30;yellowstone)]</syntaxhighlight>
{{out}}
<pre>
The first 30 entries of the Yellowstone permutation:
[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|Julia}}==
<langsyntaxhighlight lang="julia">using Plots
 
function yellowstone(N)
Line 1,062 ⟶ 1,522:
y = yellowstone(100)
plot(x, y)
</langsyntaxhighlight>{{out}}
<pre>
The first 30 entries of the Yellowstone permutation:
[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>
[[File:Yellowstone-sequence-graph.png]]
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun main() {
println("First 30 values in the yellowstone sequence:")
println(yellowstoneSequence(30))
Line 1,118 ⟶ 1,579:
a
} else gcd(b, a % b)
}</langsyntaxhighlight>
{{out}}
<pre>First 30 values in the yellowstone sequence:
Line 1,125 ⟶ 1,586:
=={{header|Lua}}==
{{trans|Java}}
<langsyntaxhighlight lang="lua">function gcd(a, b)
if b == 0 then
return a
Line 1,193 ⟶ 1,654:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 30 values in the yellowstone sequence:
[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|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">state = {1, 2, 3};
MakeNext[state_List] := Module[{i = First[state], done = False, out},
While[! done,
If[FreeQ[state, i],
If[GCD[Last[state], i] == 1,
If[GCD[state[[-2]], i] > 1,
out = Append[state, i];
done = True;
]
]
];
i++;
];
out
]
Nest[MakeNext, state, 30 - 3]
ListPlot[%]</syntaxhighlight>
{{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}
(* Graphical visualisation of the data *)</pre>
 
 
=={{header|Nim}}==
===Procedure version===
This version uses a set and, so, is limited to 65536 elements. It is easy to change this limit by using a HashSet (standard module “sets”) instead of a set. See the iterator version which uses such a HashSet.
<langsyntaxhighlight Nimlang="nim">import math
 
proc yellowstone(n: int): seq[int] =
Line 1,218 ⟶ 1,702:
inc candidate
 
echo yellowstone(30)</langsyntaxhighlight>
{{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>
Line 1,224 ⟶ 1,708:
===Iterator version===
This version uses a HashSet, but using a set as in the previous version is possible if we accept the limit of 65536 elements.
<langsyntaxhighlight Nimlang="nim">import math, sets
 
iterator yellowstone(n: int): int =
Line 1,247 ⟶ 1,731:
for n in yellowstone(30):
stdout.write " ", n
echo()</langsyntaxhighlight>
{{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>
 
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="PARI/GP">
yellowstone(n) = {
my(a=3, o=2, u=[]);
if(n<3, return(n)); \\ Base case: return n if it is less than 3
print1("1, 2"); \\ Print initial values
 
for(i = 4, n, \\ Iterate from 4 to n
print1(", "a); \\ Print current value of a
u = setunion(u, Set(a)); \\ Add a to the set u
 
\\ Remove consecutive elements from u
while(#u > 1 && u[2] == u[1] + 1,
u = vecextract(u, "^1")
);
 
\\ Find next value of a
for(k = u[1] + 1, 1e10,
if(gcd(k, o) <= 1, next); \\ Skip if gcd(k, o) is greater than 1
if(setsearch(u, k), next); \\ Skip if k is in set u
if(gcd(k, a) != 1, next); \\ Skip if gcd(k, a) is not 1
o = a; \\ Update o to current a
a = k; \\ Update a to k
break
)
);
 
a \\ Return the final value of a
}
 
yellowstone(20); \\ Call the function with n = 20
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 9, 8, 15, 14, 5, 6, 25, 12, 35, 16, 7, 10, 21, 20, 27
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,300 ⟶ 1,822:
binmode $fh;
print $fh $gd->png();
close $fh;</langsyntaxhighlight>
{{out}}
<pre>The first 30 terms in the Yellowstone sequence:
Line 1,307 ⟶ 1,829:
 
=={{header|Phix}}==
{{translibheader|JuliaPhix/pGUI}}
{{libheader|Phix/online}}
<!--<lang Phix>(phixonline?)-->
You can run this online [http://phix.x10.mx/p2js/Yellowstone.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Yellowstone_sequence.exw
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">yellowstone</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
Line 1,328 ⟶ 1,858:
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 30 entries of the Yellowstone permutation:\n%Vv\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">yellowstone</span><span style="color: #0000FF;">(</span><span style="color: #000000;">30</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<span style="color: #000080;font-style:italic;">-- a simple plot:</span>
{{out}}
<pre>
The first 30 entries of the Yellowstone permutation:
{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>
=== a simple plot ===
{{libheader|Phix/pGUI}}
<!--<lang Phix>-->
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">y500</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yellowstone</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XTICK"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;"><</span><span style="color: #000000;">640</span><span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">300</span><span style="color: #0000FF;">?</span><span style="color: #000000;">100</span><span style="color: #0000FF;">:</span><span style="color: #000000;">50</span><span style="color: #0000FF;">):</span><span style="color: #000000;">20</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YTICK"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">250</span><span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">140</span><span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">120</span><span style="color: #0000FF;">?</span><span style="color: #000000;">700</span><span style="color: #0000FF;">:</span><span style="color: #000000;">350</span><span style="color: #0000FF;">):</span><span style="color: #000000;">200</span><span style="color: #0000FF;">):</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">y500</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE=960x600"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupControlsOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #0040807060A8;">IhandleIupSetAttributes</span> <span style="color: #000000;">plot</span> <span style="color: #0000FF;">=(</span> <span style="color: #7060A8000000;">IupPlotgraph</span><span style="color: #0000FF;">(,</span><span style="color: #008000;">`GTITLE="MENUITEMPROPERTIES=Yes,Yellowstone SIZE=640x320Numbers"`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttributeIupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plotgraph</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLETITLESTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000004600;">"Yellowstone Numbers"CD_ITALIC</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttributeIupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plotgraph</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLEFONTSIZE"</span><span style`XNAME="color: #0000FF;n">,</span> <span styleYNAME="color: #008000;">"10a(n)"`</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttributeIupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plotgraph</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLEFONTSTYLE"</span><span styleXTICK="color: #0000FF;">20,</span> <span styleXMIN=0,XMAX="color: #008000;">"ITALIC500"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttributeIupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plotgraph</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"GRIDLINESTYLE"</span><span styleYTICK="color: #0000FF;">100,</span> <span styleYMIN=0,YMAX="color: #008000;">"DOTTED1400"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8004080;">IupSetAttributeIhandle</span> <span style="color: #0000FF000000;">(dlg</span> <span style="color: #0000000000FF;">plot=</span> <span style="color: #0000FF7060A8;">,IupDialog</span> <span style="color: #0080000000FF;">(</span><span style="GRIDcolor: #000000;">graph</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">`TITLE="YESYellowstone Names"`</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttributeIupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plotdlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_XLABEL"</span><span styleMINSIZE="color: #0000FF;">,</span> <span style="color: #008000;">"n290x140"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YLABEL"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"a(n)"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_XFONTSTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ITALIC"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YFONTSTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ITALIC"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKSIZEAUTO"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"NO"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKMAJORSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"8"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKMINORSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0"</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">IupPlotBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">y500</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yellowstone</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">500</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">IupPlotAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y500</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupPlotEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--IupSetAttribute(plot, "DS_MODE", "BAR") -- (optional)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Yellowstone Names"`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
Line 1,368 ⟶ 1,887:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
The first 30 entries of the Yellowstone permutation:
{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|Phixmonti}}==
{{trans|Ruby}}Require Utilitys library version 1.3
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
def gcd /# u v -- n #/
Line 1,410 ⟶ 1,934:
def test n a len nip > enddef
"The first 30 entries of the Yellowstone permutation:" ? 30 yellow ?</langsyntaxhighlight>
{{out}}
<pre>The first 30 entries of the Yellowstone permutation:
Line 1,418 ⟶ 1,942:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/frac.l")
(de yellow (N)
(let (L (list 3 2 1) I 4 C 3 D)
Line 1,433 ⟶ 1,957:
(inc 'I) )
(flip L) ) )
(println (yellow 30))</langsyntaxhighlight>
{{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>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure.i gcd(x.i,y.i)
While y<>0 : t=x : x=y : y=t%y : Wend : ProcedureReturn x
EndProcedure
 
If OpenConsole()
Dim Y.i(100)
For i=1 To 100
If i<=3 : Y(i)=i : Continue : EndIf : k=3
Repeat
RepLoop:
k+1
For j=1 To i-1 : If Y(j)=k : Goto RepLoop : EndIf : Next
If gcd(k,Y(i-2))=1 Or gcd(k,Y(i-1))>1 : Continue : EndIf
Y(i)=k : Break
ForEver
Next
For i=1 To 30 : Print(Str(Y(i))+" ") : Next : Input()
EndIf</syntaxhighlight>
{{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>
 
=={{header|Python}}==
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Yellowstone permutation OEIS A098550'''
 
from itertools import chain, count, islice
Line 1,587 ⟶ 2,134:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{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>
 
=={{header|Quackery}}==
 
<code>gcd</code> is defined at [[Greatest common divisor#Quackery]].
 
<syntaxhighlight lang="quackery"> [ stack ] is seqbits ( --> s )
 
[ bit
seqbits take |
seqbits put ] is seqadd ( n --> )
 
[ bit
seqbits share & not ] is notinseq ( n --> b )
 
[ temp put
' [ 1 2 3 ]
7 seqbits put
4
[ dip
[ dup -1 peek
over -2 peek ]
dup dip
[ tuck gcd 1 !=
unrot gcd 1 =
and ]
swap if
[ dup dip join
seqadd
3 ]
[ 1+
dup notinseq until ]
over size temp share
< not until ]
drop
seqbits release
temp take split drop ] is yellowstones ( n --> [ )
 
30 yellowstones echo</syntaxhighlight>
 
{{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>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require plot)
Line 1,615 ⟶ 2,204:
 
(plot (points
(map (λ (i) (vector i (a098550 i))) (range 1 (add1 100)))))</langsyntaxhighlight>
 
{{out}}
Line 1,629 ⟶ 2,218:
Not really clear whether a line graph or bar graph was desired, so generate both. Also, 100 points don't really give a good feel for the overall shape so do 500.
 
<syntaxhighlight lang="raku" perl6line>my @yellowstone = 1, 2, 3, -> $q, $p {
state @used = True xx 4;
state $min = 3;
Line 1,663 ⟶ 2,252:
 
$line.spurt: SVG.serialize: $chart.plot: :lines;
$bars.spurt: SVG.serialize: $chart.plot: :bars;</langsyntaxhighlight>
{{out}}
<pre>The first 30 terms in the Yellowstone sequence:
Line 1,672 ⟶ 2,261:
=={{header|REXX}}==
===horizontal list of numbers===
<langsyntaxhighlight lang="rexx">/*REXX program calculates any number of terms in the Yellowstone (permutation) sequence.*/
parse arg m . /*obtain optional argument from the CL.*/
if m=='' | m=="," then m= 30 /*Not specified? Then use the default.*/
Line 1,691 ⟶ 2,280:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: parse arg x,y; do until y==0; parse value x//y y with y x; end; return x</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input: &nbsp; &nbsp; <tt> 30 </tt>}}
<pre>
Line 1,699 ⟶ 2,288:
===vertical histogram plot===
A horizontal histogram could also be shown, &nbsp; but it would require a taller (higher) plot with more vertical screen real estate.
<langsyntaxhighlight lang="rexx">/*REXX program calculates any number of terms in the Yellowstone (permutation) sequence.*/
parse arg m . /*obtain optional argument from the CL.*/
if m=='' | m=="," then m= 30 /*Not specified? Then use the default.*/
Line 1,719 ⟶ 2,308:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: parse arg x,y; do until y==0; parse value x//y y with y x; end; return x</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 532 </tt>}}
 
Line 1,785 ⟶ 2,374:
5 9 3 1 5 5 1 7 3 9 5 5 5 5 5 9 30741 361 258525634187 0729 074389016 6525458525 0367 45892189 4703490 016725652189476389 274189016 0967214525185456530987230945701839652545673852903618349410327658307497216907251258545658541701839036329496381072145658590421165458547725610309278143653048327437658545256530909276981985452510667810927416387092110343456732987437658590741670329638523123418309612985456309214765381458525497836585907094169369012307412965839072314387
3 1 7 7 5 5 3
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "working..." + nl
row = 3
num = 2
numbers = 1:51
first = 2
second = 3
see "Yellowstone numbers are:" + nl
see "1 " + first + " " + second + " "
 
for n = 4 to len(numbers)
flag1 = 1
flag2 = 1
if first < numbers[n]
min = first
else
min = numbers[n]
ok
for m = 2 to min
if first%m = 0 and numbers[n]%m = 0
flag1 = 0
exit
ok
next
if second < numbers[n]
min = second
else
min = numbers[n]
ok
for m = 2 to min
if second%m = 0 and numbers[n]%m = 0
flag2 = 0
exit
ok
next
if flag1 = 0 and flag2 = 1
see "" + numbers[n] + " "
first = second
second = numbers[n]
del(numbers,n)
row = row+1
if row%10 = 0
see nl
ok
num = num + 1
if num = 29
exit
ok
n = 3
ok
next
 
see "Found " + row + " Yellowstone numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
working...
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
Found 30 Yellowstone numbers
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
'''IF''' DUP2 < '''THEN''' SWAP '''END'''
'''WHILE''' DUP '''REPEAT''' SWAP OVER MOD '''END'''
DROP
≫ 'GCD' STO
DUP SIZE 1 - GETI ROT ROT GET → am2 am1
≪ 3 '''DO'''
'''DO''' 1 + '''UNTIL''' DUP2 POS NOT '''END'''
'''UNTIL''' DUP am1 GCD 1 == OVER am2 GCD 1 ≠ AND '''END'''
+
≫ ≫ 'YELLO' STO
|
''( a b -- gcd(a,b) )''
Ensure a > b
Euclidean algorithm
''( { a(1)..a(n-1) } -- { a(1)..a(n) } )''
Store locally a(n-1) and a(n-2)
Find smallest number not already in sequence
Check primality requirements
Add a(n) to the sequence
|}
The following words in the command line deliver what is required:
{ 1 2 3 }
≪ 1 27 START YELLO NEXT ≫ EVAL
{{out}}
<pre>
1: { 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|Ruby}}==
<langsyntaxhighlight lang="ruby">def yellow(n)
a = [1, 2, 3]
b = { 1 => true, 2 => true, 3 => true }
Line 1,803 ⟶ 2,502:
end
 
p yellow(30)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,810 ⟶ 2,509:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// num = "0.3"
// plotters = "^0.2.15"
Line 1,873 ⟶ 2,572:
Err(error) => eprintln!("Error: {}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,881 ⟶ 2,580:
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>
[[Media:Yellowstone sequence rust.png]]
See: [https://slack-files.com/T0CNUL56D-F016J1UB554-6e7059bbcc yellowstone.png] (offsite PNG image)
 
 
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import scala.util.control.Breaks._
 
 
object YellowstoneSequence extends App {
 
println(s"First 30 values in the yellowstone sequence:\n${yellowstoneSequence(30)}")
 
def yellowstoneSequence(sequenceCount: Int): List[Int] = {
var yellowstoneList = List(1, 2, 3)
var num = 4
var notYellowstoneList = List[Int]()
while (yellowstoneList.size < sequenceCount) {
val foundIndex = notYellowstoneList.indexWhere(test =>
gcd(yellowstoneList(yellowstoneList.size - 2), test) > 1 &&
gcd(yellowstoneList.last, test) == 1
)
 
if (foundIndex >= 0) {
yellowstoneList = yellowstoneList :+ notYellowstoneList(foundIndex)
notYellowstoneList = notYellowstoneList.patch(foundIndex, Nil, 1)
} else {
breakable({
while (true) {
if (gcd(yellowstoneList(yellowstoneList.size - 2), num) > 1 &&
gcd(yellowstoneList.last, num) == 1) {
yellowstoneList = yellowstoneList :+ num
num += 1
// break the inner while loop
break
}
notYellowstoneList = notYellowstoneList :+ num
num += 1
}
});
}
}
yellowstoneList
}
 
def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
}
</syntaxhighlight>
{{out}}
<pre>
First 30 values in the yellowstone sequence:
List(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|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc gcd {a b} {
while {$b} {
lassign [list $b [expr {$a % $b}]] a b
Line 1,913 ⟶ 2,667:
}
puts "The first 30 Yellowstone numbers are:"
puts [gen_yellowstones]</langsyntaxhighlight>
{{out}}
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
 
=={{header|uBasic/4tH}}==
{{trans|VBA}}
{{works with|v3.64.0}}
<syntaxhighlight lang="text">Dim @y(30)
@y(0) = 1
@y(1) = 2
@y(2) = 3
For i = 3 To 29
k = 3
Do
k = k + 1
If (FUNC(_gcd(k, @y(i-2))) = 1) + (FUNC(_gcd(k, @y(i-1))) > 1) Then
Continue
EndIf
 
For j = 0 To i - 1
If @y(j) = k Then Unloop : Continue
Next
 
@y(i) = k : Break
Loop
Next
For i = 0 To 29
Print @y(i); " ";
Next
 
Print : End
 
_gcd Param (2)
If b@ = 0 Then Return (a@)
Return (FUNC(_gcd(b@, a@ % b@)))</syntaxhighlight>
{{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
 
0 OK, 0:670
</pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="tcl">
<lang Tcl>
Function gcd(a As Long, b As Long) As Long
If b = 0 Then
Line 1,953 ⟶ 2,748:
Next i
End Sub
</syntaxhighlight>
</lang>
{{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>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn gcd(xx int, yy int) int {
mut x := xx
mut y := yy
for y != 0 {
x, y = y, x%y
}
return x
}
fn yellowstone(n int) []int {
mut m := map[int]bool{}
mut a := []int{len: n+1}
for i in 1..4 {
a[i] = i
m[i] = true
}
mut min := 4
for c := 4; c <= n; c++ {
for i := min; ; i++ {
if !m[i] && gcd(a[c-1], i) == 1 && gcd(a[c-2], i) > 1 {
a[c] = i
m[i] = true
if i == min {
min++
}
break
}
}
}
return a[1..]
}
fn main() {
mut x := []int{len: 100}
for i in 0..100 {
x[i] = i + 1
}
y := yellowstone(100)
println("The first 30 Yellowstone numbers are:")
println(y[..30])
}</syntaxhighlight>
 
{{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]
</pre>
 
Line 1,963 ⟶ 2,808:
{{libheader|Wren-math}}
Without the extra credit part.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var yellowstone = Fn.new { |n|
Line 1,992 ⟶ 2,837:
var y = yellowstone.call(30)
System.print("The first 30 Yellowstone numbers are:")
System.print(y)</langsyntaxhighlight>
 
{{out}}
Line 1,998 ⟶ 2,843:
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]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func GCD(N, D); \Return the greatest common divisor of N and D
int N, D, R; \numerator, denominator, remainder
[if D > N then
[R:=D; D:=N; N:=R]; \swap D and N
while D > 0 do
[R:= rem(N/D);
N:= D;
D:= R;
];
return N;
];
 
int I, A(30+1), N, T;
[for I:= 1 to 3 do A(I):= I; \givens
N:= 4;
repeat T:= 4;
loop [if GCD(T, A(N-1)) = 1 and \relatively prime
GCD(T, A(N-2)) # 1 then \not relatively prime
[loop [for I:= 1 to N-1 do \test if in sequence
if T = A(I) then quit;
quit;
];
if I = N then \T is not in sequence so
[A(N):= T; \ add it in
N:= N+1;
quit;
];
];
T:= T+1; \next trial
];
until N > 30;
for N:= 1 to 30 do
[IntOut(0, A(N)); ChOut(0, ^ )];
\\for N:= 1 to 100 do Point(N, A(N)); \plot demonstration
]</syntaxhighlight>
 
{{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>
 
Line 2,003 ⟶ 2,890:
{{trans|Julia}}
This sequence is limited to the max size of a Dictionary, 64k
<langsyntaxhighlight lang="zkl">fcn yellowstoneW{ // --> iterator
Walker.zero().tweak(fcn(a,b){
foreach i in ([1..]){
Line 2,013 ⟶ 2,900:
}
}.fp(List(2,3), Dictionary(1,True, 2,True, 3,True))).push(1,2,3);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("The first 30 entries of the Yellowstone permutation:");
yellowstoneW().walk(30).concat(", ").println();</langsyntaxhighlight>
{{out}}
<pre>
Line 2,022 ⟶ 2,909:
</pre>
Plot using Gnuplot
<langsyntaxhighlight lang="zkl">gnuplot:=System.popen("gnuplot","w");
gnuplot.writeln("unset key; plot '-'");
yellowstoneW().pump(1_000, gnuplot.writeln.fp(" ")); // " 1\n", " 2\n", ...
gnuplot.writeln("e");
gnuplot.flush();
ask("Hit return to finish"); gnuplot.close();</langsyntaxhighlight>
Offsite Image: [http://www.zenkinetic.com/Images/RosettaCode/yellowstone1000.zkl.png yellowstone]
1,983

edits