Boustrophedon transform: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: change ordering to match task description) |
(Added Wren) |
||
Line 83: | Line 83: | ||
1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547 |
1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547 |
||
1000th term: 13714256926920345740…19230014799151339821 (2566 digits)</pre> |
1000th term: 13714256926920345740…19230014799151339821 (2566 digits)</pre> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
Just the basic task for now. |
|||
<syntaxhighlight lang="ecmascript">import "./math" for Int |
|||
var boustrophedon = Fn.new { |a| |
|||
var k = a.count |
|||
var cache = List.filled(k, null) |
|||
for (i in 0...k) cache[i] = List.filled(k, 0) |
|||
var b = List.filled(k, 0) |
|||
b[0] = a[0] |
|||
var T |
|||
T = Fn.new { |k, n| |
|||
if (n == 0) return a[k] |
|||
if (cache[k][n] > 0) return cache[k][n] |
|||
return cache[k][n] = T.call(k, n-1) + T.call(k-1, k-n) |
|||
} |
|||
for (n in 1...k) b[n] = T.call(n, n) |
|||
return b |
|||
} |
|||
System.print("1 followed by 0's:") |
|||
var a = [1] + ([0] * 14) |
|||
System.print(boustrophedon.call(a)) |
|||
System.print("\nAll 1's:") |
|||
a = [1] * 15 |
|||
System.print(boustrophedon.call(a)) |
|||
System.print("\nAlternating 1, -1") |
|||
a = [1, -1] * 7 + [1] |
|||
System.print(boustrophedon.call(a)) |
|||
System.print("\nPrimes:") |
|||
a = Int.primeSieve(200)[0..14] |
|||
System.print(boustrophedon.call(a)) |
|||
System.print("\nFibonacci numbers:") |
|||
a[0] = 1 // start from fib(1) |
|||
a[1] = 1 |
|||
for (i in 2..14) a[i] = a[i-1] + a[i-2] |
|||
System.print(boustrophedon.call(a)) |
|||
System.print("\nFactorials:") |
|||
a[0] = 1 |
|||
for (i in 1..14) a[i] = a[i-1] * i |
|||
System.print(boustrophedon.call(a))</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> |