2016-12-05 23:44:36 +01:00
|
|
|
|
# Declare a function to generate the Stern-Brocot sequence
|
|
|
|
|
|
func stern_brocot {
|
2017-09-23 10:01:46 +02:00
|
|
|
|
var list = [1, 1]
|
2018-06-22 20:57:24 +00:00
|
|
|
|
{
|
2017-09-23 10:01:46 +02:00
|
|
|
|
list.append(list[0]+list[1], list[1])
|
|
|
|
|
|
list.shift
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Show the first fifteen members of the sequence.
|
2017-09-23 10:01:46 +02:00
|
|
|
|
say 15.of(stern_brocot()).join(' ')
|
2016-12-05 23:44:36 +01:00
|
|
|
|
|
|
|
|
|
|
# Show the (1-based) index of where the numbers 1-to-10 first appears
|
|
|
|
|
|
# in the sequence, and where the number 100 first appears in the sequence.
|
2017-09-23 10:01:46 +02:00
|
|
|
|
for i (1..10, 100) {
|
|
|
|
|
|
var index = 1
|
|
|
|
|
|
var generator = stern_brocot()
|
|
|
|
|
|
while (generator() != i) {
|
|
|
|
|
|
++index
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
2017-09-23 10:01:46 +02:00
|
|
|
|
say "First occurrence of #{i} is at index #{index}"
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Check that the greatest common divisor of all the two consecutive
|
|
|
|
|
|
# members of the series up to the 1000th member, is always one.
|
2017-09-23 10:01:46 +02:00
|
|
|
|
var generator = stern_brocot()
|
|
|
|
|
|
var (a, b) = (generator(), generator())
|
2016-12-05 23:44:36 +01:00
|
|
|
|
{
|
2017-09-23 10:01:46 +02:00
|
|
|
|
assert_eq(gcd(a, b), 1)
|
|
|
|
|
|
a = b
|
|
|
|
|
|
b = generator()
|
|
|
|
|
|
} * 1000
|
2016-12-05 23:44:36 +01:00
|
|
|
|
|
2017-09-23 10:01:46 +02:00
|
|
|
|
say "All GCD's are 1"
|