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,43 @@
|
|||
PROGRAM FIBON
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
DIM F[20]
|
||||
|
||||
PROCEDURE FIB(TIPO$,F$)
|
||||
FOR I%=0 TO 20 DO
|
||||
F[I%]=0
|
||||
END FOR
|
||||
B=0
|
||||
LOOP
|
||||
Q=INSTR(F$,",")
|
||||
B=B+1
|
||||
IF Q=0 THEN
|
||||
F[B]=VAL(F$)
|
||||
EXIT
|
||||
ELSE
|
||||
F[B]=VAL(MID$(F$,1,Q-1))
|
||||
F$=MID$(F$,Q+1)
|
||||
END IF
|
||||
END LOOP
|
||||
|
||||
PRINT(TIPO$;" =>";)
|
||||
FOR I=B TO 14+B DO
|
||||
IF I<>B THEN PRINT(",";) END IF
|
||||
PRINT(F[I-B+1];)
|
||||
FOR J=(I-B)+1 TO I DO
|
||||
F[I+1]=F[I+1]+F[J]
|
||||
END FOR
|
||||
END FOR
|
||||
PRINT
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);) ! CLS
|
||||
FIB("Fibonacci","1,1")
|
||||
FIB("Tribonacci","1,1,2")
|
||||
FIB("Tetranacci","1,1,2,4")
|
||||
FIB("Lucas","2,1")
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
;; generate a recursive lambda() for a x-nacci
|
||||
;; equip it with memoïzation
|
||||
;; bind it to its name
|
||||
(define (make-nacci name seed)
|
||||
(define len (1+ (vector-length seed)))
|
||||
(define-global name
|
||||
`(lambda(n) (for/sum ((i (in-range (1- n) (- n ,len) -1))) (,name i))))
|
||||
(remember name seed)
|
||||
name)
|
||||
|
||||
(define nacci-family `(
|
||||
(Fibonacci #(1 1))
|
||||
(Tribonacci #(1 1 2))
|
||||
(Tetranacci #(1 1 2 4))
|
||||
(Decanacci #(1 1 2 4 8 16 32 64 128 256))
|
||||
(Random-😜-nacci ,(list->vector (take 6 (shuffle (iota 100)))))
|
||||
(Lucas #(2 1))))
|
||||
|
||||
(define (task naccis)
|
||||
(for ((nacci naccis))
|
||||
(define-values (name seed) nacci)
|
||||
(make-nacci name seed)
|
||||
(printf "%s[%d] → %d" name (vector-length seed) (take name 16))))
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' Deduces the step, n, from the length of the dynamic array passed in
|
||||
' and fills it out to 'size' elements
|
||||
Sub fibN (a() As Integer, size As Integer)
|
||||
Dim lb As Integer = LBound(a)
|
||||
Dim ub As Integer = UBound(a)
|
||||
Dim length As Integer = ub - lb + 1
|
||||
If length < 2 OrElse length >= size Then Return
|
||||
ub = lb + size - 1
|
||||
Redim Preserve a(lb To ub)
|
||||
Dim sum As Integer
|
||||
For i As Integer = lb + length to ub
|
||||
sum = 0
|
||||
For j As Integer = 1 To Length
|
||||
sum += a(i - j)
|
||||
Next j
|
||||
a(i) = sum
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub printSeries(a() As Integer, name_ As String) '' name is a keyword
|
||||
Print name_; " =>";
|
||||
For i As Integer = LBound(a) To UBound(a)
|
||||
Print Using "####"; a(i);
|
||||
Print " ";
|
||||
Next
|
||||
Print
|
||||
End Sub
|
||||
|
||||
Const size As Integer = 13 '' say
|
||||
Redim a(1 To 2) As Integer
|
||||
a(1) = 1 : a(2) = 1
|
||||
fibN(a(), size)
|
||||
printSeries(a(), "fibonacci ")
|
||||
Redim Preserve a(1 To 3)
|
||||
a(3) = 2
|
||||
fibN(a(), size)
|
||||
printSeries(a(), "tribonacci")
|
||||
Redim Preserve a(1 To 4)
|
||||
a(4) = 4
|
||||
fibN(a(), size)
|
||||
printSeries(a(), "tetranacci")
|
||||
erase a
|
||||
Redim a(1 To 2)
|
||||
a(1) = 2 : a(2) = 1
|
||||
fibN(a(), size)
|
||||
printSeries(a(), "lucas ")
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import util.TextTable
|
||||
native scala.collection.mutable.Queue
|
||||
|
||||
def fibLike( init ) =
|
||||
q = Queue()
|
||||
|
||||
for i <- init do q.enqueue( i )
|
||||
|
||||
def fib =
|
||||
q.enqueue( sum(q) )
|
||||
q.dequeue() # fib()
|
||||
|
||||
0 # fib()
|
||||
|
||||
def fibN( n ) = fibLike( [1] + [2^i | i <- 0:n-1] )
|
||||
|
||||
val lucas = fibLike( [2, 1] )
|
||||
|
||||
t = TextTable()
|
||||
t.header( 'k', 'Fibonacci', 'Tribonacci', 'Tetranacci', 'Lucas' )
|
||||
t.line()
|
||||
|
||||
for i <- 1..5
|
||||
t.rightAlignment( i )
|
||||
|
||||
seqs = (fibN(2), fibN(3), fibN(4), lucas)
|
||||
|
||||
for k <- 1..10
|
||||
t.row( ([k] + [seqs(i)(k) | i <- 0:4]).toIndexedSeq() )
|
||||
|
||||
print( t )
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import sequtils, strutils
|
||||
|
||||
proc fiblike(start: seq[int]): auto =
|
||||
var memo = start
|
||||
proc fibber(n: int): int =
|
||||
if n < memo.len:
|
||||
return memo[n]
|
||||
else:
|
||||
var ans = 0
|
||||
for i in n-start.len .. <n:
|
||||
ans += fibber(i)
|
||||
memo.add ans
|
||||
return ans
|
||||
return fibber
|
||||
|
||||
let fibo = fiblike(@[1,1])
|
||||
echo toSeq(0..9).map(fibo)
|
||||
let lucas = fiblike(@[2,1])
|
||||
echo toSeq(0..9).map(lucas)
|
||||
|
||||
for n, name in items({2: "fibo", 3: "tribo", 4: "tetra", 5: "penta", 6: "hexa",
|
||||
7: "hepta", 8: "octo", 9: "nona", 10: "deca"}):
|
||||
var se = @[1]
|
||||
for i in 0..n-2:
|
||||
se.add(1 shl i)
|
||||
let fibber = fiblike(se)
|
||||
echo "n = ", align($n,2), ", ", align(name, 5), "nacci ->
|
||||
", toSeq(0..14).mapIt(string, $fibber(it)).join(" "), " ..."
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
func fib(n, xs=[1]) {
|
||||
loop {
|
||||
var len = xs.len
|
||||
len >= 20 && break
|
||||
xs.append(xs.ft(0.max(len - n)).sum)
|
||||
}
|
||||
return xs
|
||||
}
|
||||
|
||||
for i in (2..10) {
|
||||
say fib(i).join(' ')
|
||||
}
|
||||
say fib(2, [2, 1]).join(' ')
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# Input: the initial array
|
||||
def nacci(arity; len):
|
||||
arity as $arity | len as $len
|
||||
| reduce range(length; $len) as $i
|
||||
(.;
|
||||
([0, (length - $arity)] | max ) as $lower
|
||||
| . + [ .[ ($lower) : length] | add] ) ;
|
||||
|
||||
def fib(arity; len):
|
||||
arity as $arity | len as $len
|
||||
| [1,1] | nacci($arity; $arity) | nacci($arity; $len) ;
|
||||
|
||||
def lucas(arity; len):
|
||||
arity as $arity | len as $len
|
||||
| [2,1] | nacci($arity; $arity) | nacci($arity; $len) ;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def main:
|
||||
(range(2; 11) | "fib(\(.)): \(fib(.; 15))"),
|
||||
(range(2; 11) | "lucas(\(.)): \(lucas(.; 15))")
|
||||
;
|
||||
|
||||
main
|
||||
Loading…
Add table
Add a link
Reference in a new issue