September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,34 +1,34 @@
# Declare a function to generate the Stern-Brocot sequence
func stern_brocot {
var list = [1, 1];
var list = [1, 1]
func {
list.append(list[0]+list[1], list[1]);
list.shift;
list.append(list[0]+list[1], list[1])
list.shift
}
}
# Show the first fifteen members of the sequence.
1..15 -> map(stern_brocot()).join(' ').say;
say 15.of(stern_brocot()).join(' ')
# 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.
[(1..10)..., 100].each { |i|
var index = 1;
var generator = stern_brocot();
while (generator() != i) {
++index;
for i (1..10, 100) {
var index = 1
var generator = stern_brocot()
while (generator() != i) {
++index
}
say "First occurrence of #{i} is at index #{index}";
say "First occurrence of #{i} is at index #{index}"
}
# Check that the greatest common divisor of all the two consecutive
# members of the series up to the 1000th member, is always one.
var generator = stern_brocot();
var (a, b) = (generator(), generator());
var generator = stern_brocot()
var (a, b) = (generator(), generator())
{
assert_eq(Math.gcd(a, b), 1);
a = b;
b = generator();
} * 1000;
assert_eq(gcd(a, b), 1)
a = b
b = generator()
} * 1000
say "All GCD's are 1";
say "All GCD's are 1"