Yellowstone sequence: Difference between revisions

m
m (syntax highlighting fixup automation)
(13 intermediate revisions by 8 users not shown)
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 817 ⟶ 818:
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}}==
Line 1,120 ⟶ 1,166:
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
s=. s , z
end.
s
)
</syntaxhighlight>
<pre>
Line 1,147 ⟶ 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}}==
Line 1,695 ⟶ 1,734:
{{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}}==
Line 2,364 ⟶ 2,441:
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>
 
Line 2,460 ⟶ 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}}==
Line 2,579 ⟶ 2,754:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn gcd(xx int, yy int) int {
mut x := xx
mut y := yy
Line 2,633 ⟶ 2,808:
{{libheader|Wren-math}}
Without the extra credit part.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var yellowstone = Fn.new { |n|
2,063

edits