Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
;; special fib's starting with 1 2 3 5 ...
|
||||
(define (fibonacci n)
|
||||
(+ (fibonacci (1- n)) (fibonacci (- n 2))))
|
||||
(remember 'fibonacci #(1 2))
|
||||
|
||||
(define-constant Φ (// (1+ (sqrt 5)) 2))
|
||||
(define-constant logΦ (log Φ))
|
||||
;; find i : fib(i) >= n
|
||||
(define (iFib n)
|
||||
(floor (// (log (+ (* n Φ) 0.5)) logΦ)))
|
||||
|
||||
;; left trim zeroes
|
||||
(string-delimiter "")
|
||||
(define (zeck->string digits)
|
||||
(if (!= 0 (first digits))
|
||||
(string-join digits "")
|
||||
(zeck->string (rest digits))))
|
||||
|
||||
(define (Zeck n)
|
||||
(cond
|
||||
(( < n 0) "no negative zeck")
|
||||
((inexact? n) "no floating zeck")
|
||||
((zero? n) "0")
|
||||
(else (zeck->string
|
||||
(for/list ((s (reverse (take fibonacci (iFib n)))))
|
||||
(if ( > s n) 0
|
||||
(begin (-= n s) 1 )))))))
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
' version 17-10-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
#Define max 92 ' max for Fibonacci number
|
||||
|
||||
Dim Shared As ULongInt fib(max)
|
||||
|
||||
fib(0) = 1
|
||||
fib(1) = 1
|
||||
|
||||
For x As Integer = 2 To max
|
||||
fib(x) = fib(x-1) + fib(x-2)
|
||||
Next
|
||||
|
||||
Function num2zeck(n As Integer) As String
|
||||
|
||||
If n < 0 Then
|
||||
Print "Error: no negative numbers allowed"
|
||||
Beep : Sleep 5000,1 : End
|
||||
End If
|
||||
|
||||
If n < 2 Then Return Str(n)
|
||||
|
||||
Dim As String zeckendorf
|
||||
|
||||
For x As Integer = max To 1 Step -1
|
||||
If fib(x) <= n Then
|
||||
zeckendorf = zeckendorf + "1"
|
||||
n = n - fib(x)
|
||||
Else
|
||||
zeckendorf = zeckendorf + "0"
|
||||
End If
|
||||
Next
|
||||
|
||||
return LTrim(zeckendorf, "0") ' get rid of leading zeroes
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Integer x, e
|
||||
Dim As String zeckendorf
|
||||
Print "number zeckendorf"
|
||||
|
||||
For x = 0 To 200000
|
||||
|
||||
zeckendorf = num2zeck(x)
|
||||
If x <= 20 Then Print x, zeckendorf
|
||||
|
||||
' check for two consecutive Fibonacci numbers
|
||||
If InStr(zeckendorf, "11") <> 0 Then
|
||||
Print " Error: two consecutive Fibonacci numbers "; x, zeckendorf
|
||||
e = e +1
|
||||
End If
|
||||
Next
|
||||
|
||||
Print
|
||||
If e = 0 Then
|
||||
Print " No Zeckendorf numbers with two consecutive Fibonacci numbers found"
|
||||
Else
|
||||
Print e; " error(s) found"
|
||||
End If
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
import strutils
|
||||
|
||||
proc z(n): string =
|
||||
if n == 0: return "0"
|
||||
var fib = @[2,1]
|
||||
var n = n
|
||||
while fib[0] < n: fib.insert(fib[0] + fib[1])
|
||||
result = ""
|
||||
for f in fib:
|
||||
if f <= n:
|
||||
result.add '1'
|
||||
n -= f
|
||||
else:
|
||||
result.add '0'
|
||||
if result[0] == '0':
|
||||
result = result[1..result.high]
|
||||
|
||||
for i in 0 .. 20:
|
||||
echo align($i, 3)," ",align(z(i), 8)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
func fib(n) is cached {
|
||||
n < 2 ? 1
|
||||
: (fib(n-1) + fib(n-2));
|
||||
}
|
||||
|
||||
func zeckendorf(n) {
|
||||
n == 0 && return '0';
|
||||
var i = 1;
|
||||
++i while (fib(i) <= n);
|
||||
gather {
|
||||
while (--i > 0) {
|
||||
var f = fib(i);
|
||||
f > n ? (take '0')
|
||||
: (take '1'; n -= f);
|
||||
}
|
||||
}.join('');
|
||||
}
|
||||
|
||||
range(0, 20).each { |n|
|
||||
printf("%4d: %8s\n", n, zeckendorf(n))
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
def zeckendorf:
|
||||
# rfibs(n) returns an array of fibonnaci numbers up to n,
|
||||
# beginning with 1, 2, ..., in reverse order
|
||||
def rfibs(n):
|
||||
# input: [f(i-2), f(i-1)]
|
||||
[1,1] | [recurse( if .[1] >= n then empty
|
||||
else [.[1], add]
|
||||
end ) | .[1]] | reverse;
|
||||
|
||||
. as $n
|
||||
# [n, rfibs, digit ]
|
||||
| [$n, rfibs($n), "" ]
|
||||
| [ recurse( .[0] as $n | .[1] as $f
|
||||
| if ($f|length) == 0 then empty
|
||||
else
|
||||
$f[0] as $next
|
||||
| if $n >= $next then [ ( $n - $next), $f[1:], "1"]
|
||||
else [ $n, $f[1:], "0"]
|
||||
end
|
||||
end )
|
||||
| .[2] ]
|
||||
| if .[1] == "0" then .[2:] else . end # remove leading 0 if any
|
||||
| join("") ;
|
||||
|
|
@ -0,0 +1 @@
|
|||
range(0;21) | "\(.): \(zeckendorf)"
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
$ jq -n -r -f zeckendorf.jq
|
||||
0:
|
||||
1: 1
|
||||
2: 10
|
||||
3: 100
|
||||
4: 101
|
||||
5: 1000
|
||||
6: 1001
|
||||
7: 1010
|
||||
8: 10000
|
||||
9: 10001
|
||||
10: 10010
|
||||
11: 10100
|
||||
12: 10101
|
||||
13: 100000
|
||||
14: 100001
|
||||
15: 100010
|
||||
16: 100100
|
||||
17: 100101
|
||||
18: 101000
|
||||
19: 101001
|
||||
20: 101010
|
||||
Loading…
Add table
Add a link
Reference in a new issue