Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,9 @@
;; stern (2n ) = stern (n)
;; stern(2n+1) = stern(n) + stern(n+1)
(define (stern n)
(cond
(( < n 3) 1)
((even? n) (stern (/ n 2)))
(else (let ((m (/ (1- n) 2))) (+ (stern m) (stern (1+ m)))))))
(remember 'stern)

View file

@ -0,0 +1,20 @@
; generate the sequence and check GCD
(for ((n 10000))
(unless (= (gcd (stern n) (stern (1+ n))) 1) (error "BAD GCD" n)))
→ #t
;; first items
(define sterns (cache 'stern))
(subvector sterns 1 16)
→ #( 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4)
;; first occurences index
(for ((i (in-range 1 11))) (write (vector-index i sterns)))
→ 0 3 5 9 11 33 19 21 35 39
;; 100
(writeln (vector-index 100 sterns))
→ 1179
(stern 900000) → 446
(stern 900001) → 2479

View file

@ -0,0 +1,13 @@
;; grouping
(for ((i (in-range 2 40 2))) (write (/ (stern i)(stern (1+ i)))))
→ 1/2 1/3 2/3 1/4 3/5 2/5 3/4 1/5 4/7 3/8 5/7 2/7 5/8 3/7 4/5 1/6 5/9 4/11 7/10
;; computing f(1), f(f(1)), etc.
(define (f x)
(let [(a (/ (- (floor x) -1 (fract x))))]
(if (> a 1) (f a) (cons a a))))
(define T (make-stream f 1))
(for((i 19)) (write (stream-iterate T)))
→ 1/2 1/3 2/3 1/4 3/5 2/5 3/4 1/5 4/7 3/8 5/7 2/7 5/8 3/7 4/5 1/6 5/9 4/11 7/10

View file

@ -0,0 +1,7 @@
: stern(n)
| l i |
ListBuffer new dup add(1) dup add(1) dup ->l
n 1- 2 / loop: i [ l at(i) l at(i 1+) tuck + l add l add ]
n 2 mod ifFalse: [ dup removeLast drop ] dup freeze ;
stern(10000) Constant new: Sterns

View file

@ -0,0 +1,34 @@
# Declare a function to generate the Stern-Brocot sequence
func stern_brocot {
var list = [1, 1];
func {
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;
# 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;
}
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());
{
assert_eq(Math.gcd(a, b), 1);
a = b;
b = generator();
} * 1000;
say "All GCD's are 1";

View file

@ -0,0 +1,12 @@
def until(cond; update):
def _until:
if cond then . else (update | _until) end;
try _until catch if .== "break" then empty else . end ;
def gcd(a; b):
# subfunction expects [a,b] as input
# i.e. a ~ .[0] and b ~ .[1]
def rgcd: if .[1] == 0 then .[0]
else [.[1], .[0] % .[1]] | rgcd
end;
[a,b] | rgcd ;

View file

@ -0,0 +1,18 @@
# If n is non-negative, then A002487(n)
# generates an array with at least n elements of
# the A002487 sequence;
# if n is negative, elements are added until (-n)
# is found.
def A002487(n):
[0,1]
| until(
length as $l
| if n >= 0 then $l >= n
else .[$l-1] == -n
end;
length as $l
| ($l / 2) as $n
| .[$l] = .[$n]
| if (.[$l-2] == -n) then .
else .[$l + 1] = .[$n] + .[$n+1]
end ) ;

View file

@ -0,0 +1,31 @@
# Generate a stream of n integers beginning with 1,1...
def stern_brocot(n): A002487(n+1) | .[1:n+1][];
# Return the index (counting from 1) of n in the
# sequence starting with 1,1,...
def stern_brocot_index(n):
A002487(-n) | length -1 ;
def index_task:
(range(1;11), 100) as $i
| "index of \($i) is \(stern_brocot_index($i))" ;
def gcd_task:
A002487(1000)
| . as $A
| reduce range(0; length-1) as $i
( [];
gcd( $A[$i]; $A[$i+1] ) as $gcd
| if $gcd == 1 then . else . + [$gcd] end)
| if length == 0 then "GCDs are all 1"
else "GCDs include \(.)" end ;
"First 15 integers of the Stern-Brocot sequence",
"as defined in the task description are:",
stern_brocot(15),
"",
"Using an index origin of 1:",
index_task,
"",
gcd_task

View file

@ -0,0 +1,33 @@
$ jq -r -n -f stern_brocot.jq
First 15 integers of the Stern-Brocot sequence
as defined in the task description are:
1
1
2
1
3
2
3
1
4
3
5
2
5
3
4
Using an index origin of 1:
index of 1 is 1
index of 2 is 3
index of 3 is 5
index of 4 is 9
index of 5 is 11
index of 6 is 33
index of 7 is 19
index of 8 is 21
index of 9 is 35
index of 10 is 39
index of 100 is 1179
GCDs are all 1