Boustrophedon transform: Difference between revisions

Added FreeBASIC
(Added Easylang)
(Added FreeBASIC)
 
Line 371:
[ 1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547 ]
</pre>
 
=={{header|FreeBASIC}}==
===Basic===
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Function T(Byval k As Integer, Byval n As Integer, a() As Integer, cache() As Integer) As Integer
If n = 0 Then
Return a(k)
Elseif cache(k * Ubound(a) + n) <> 0 Then
Return cache(k * Ubound(a) + n)
Else
cache(k * Ubound(a) + n) = T(k, n - 1, a(), cache()) + T(k - 1, k - n, a(), cache())
Return cache(k * Ubound(a) + n)
End If
End Function
 
Sub Boustrophedon(a() As Integer)
Dim As Integer k = Ubound(a)
Dim As Integer cache(k * k + k)
Dim As Integer b(k)
b(0) = a(0)
For n As Integer = 1 To k
b(n) = T(n, n, a(), cache())
Next
Print "[";
For n As Integer = 0 To k
Print b(n); ",";
Next
Print Chr(8); !" ]\n"
End Sub
 
Dim As Integer i
Print "1 followed by 0's:"
Dim As Integer a0(14) = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Boustrophedon(a0())
 
Print "All 1's:"
Dim As Integer a1(14) = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Boustrophedon(a1())
 
Print "Alternating 1, -1:"
Dim As Integer a2(14)
For i = 0 To 14
a2(i) = Iif((i\2 = i/2), 1, -1)
Next i
Boustrophedon(a2())
 
Print "Primes:"
Sub primeSieve(n As Integer, primes() As Integer)
Redim primes(n - 1)
Dim As Integer j, i = 2, count = 0
While count < n
j = 2
While j <= i \ 2
If i Mod j = 0 Then Exit While
j += 1
Wend
If j > i \ 2 Then
primes(count) = i
count += 1
End If
i += 1
Wend
End Sub
Dim As Integer a3(14)
primeSieve(15, a3())
Boustrophedon(a3())
 
Print "Fibonacci numbers:"
Dim As Integer a4(14)
a4(0) = 1 ' start from fib(1)
a4(1) = 1
For i = 2 To 14
a4(i) = a4(i-1) + a4(i-2)
Next i
Boustrophedon(a4())
 
Print "Factorials:"
Dim As Integer a5(14)
a5(0) = 1
For i = 1 To 14
a5(i) = a5(i-1) * i
Next i
Boustrophedon(a5())
 
Sleep</syntaxhighlight>
{{out}}
<pre>1 followed by 0's:
[ 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521, 353792, 2702765, 22368256, 199360981 ]
 
All 1's:
[ 1, 2, 4, 9, 24, 77, 294, 1309, 6664, 38177, 243034, 1701909, 13001604, 107601977, 959021574 ]
 
Alternating 1, -1:
[ 1, 0, 0, 1, 0, 5, 10, 61, 280, 1665, 10470, 73621, 561660, 4650425, 41441530 ]
 
Primes:
[ 2, 5, 13, 35, 103, 345, 1325, 5911, 30067, 172237, 1096319, 7677155, 58648421, 485377457, 4326008691 ]
 
Fibonacci numbers:
[ 1, 2, 5, 14, 42, 144, 563, 2526, 12877, 73778, 469616, 3288428, 25121097, 207902202, 1852961189 ]
 
Factorials:
[ 1, 2, 5, 17, 73, 381, 2347, 16701, 134993, 1222873, 12279251, 135425553, 1627809401, 21183890469, 296773827547 ]</pre>
 
=={{header|F_Sharp|F#}}==
2,169

edits