Yellowstone sequence: Difference between revisions

m
(Add PARI/GP implementation)
 
(3 intermediate revisions by 3 users not shown)
Line 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 2,536 ⟶ 2,581:
</pre>
[[Media:Yellowstone sequence rust.png]]
 
 
 
=={{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,708 ⟶ 2,808:
{{libheader|Wren-math}}
Without the extra credit part.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var yellowstone = Fn.new { |n|
2,052

edits