Boustrophedon transform: Difference between revisions

julia example
(→‎Stretch: Simplified.)
(julia example)
Line 44:
;* [[oeis:A230960|OEIS:A230960 - Boustrophedon transform of factorials]]
 
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
 
function bous!(triangle, k, n, seq)
n == 1 && return BigInt(seq[k])
triangle[k][n] > 0 && return triangle[k][n]
return (triangle[k][n] = bous!(triangle, k, n - 1, seq) + bous!(triangle, k - 1, k - n + 1, seq))
end
 
boustrophedon(seq) = (n = length(seq); t = [zeros(BigInt, j) for j in 1:n]; [bous!(t, i, i, seq) for i in 1:n])
boustrophedon(f, range) = boustrophedon(map(f, range))
 
fib(n) = (z = BigInt(0); ccall((:__gmpz_fib_ui, :libgmp), Cvoid, (Ref{BigInt}, Culong), z, n); z)
 
tests = [
((n) -> n < 2, 1:1000, "One followed by an infinite series of zeros -> A000111"),
((n) -> 1, 1:1000, "An infinite series of ones -> A000667"),
((n) -> isodd(n) ? 1 : -1, 1:1000, "(-1)^n: alternating 1, -1, 1, -1 -> A062162"),
((n) -> prime(n), 1:1000, "Sequence of prime numbers -> A000747"),
((n) -> fib(n), 1:1000, "Sequence of Fibonacci numbers -> A000744"),
((n) -> factorial(BigInt(n)), 0:999, "Sequence of factorial numbers -> A230960")
]
 
for (f, rang, label) in tests
println(label)
arr = boustrophedon(f, rang)
println(arr[1:15])
s = string(arr[1000])
println(s[1:20], " ... ", s[991:1000], " ($(length(s)) digits)\n")
end
</syntaxhighlight>{{out}}
<pre>
One followed by an infinite series of zeros -> A000111
BigInt[1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521, 353792, 2702765, 22368256, 199360981]
61065678604283283233 ... 7859944141 (2369 digits)
 
An infinite series of ones -> A000667
BigInt[1, 2, 4, 9, 24, 77, 294, 1309, 6664, 38177, 243034, 1701909, 13001604, 107601977, 959021574]
29375506567920455903 ... 9149120830 (2370 digits)
 
(-1)^n: alternating 1, -1, 1, -1 -> A062162
BigInt[1, 0, 0, 1, 0, 5, 10, 61, 280, 1665, 10470, 73621, 561660, 4650425, 41441530]
12694307397830194676 ... 3726314169 (2369 digits)
 
Sequence of prime numbers -> A000747
BigInt[2, 5, 13, 35, 103, 345, 1325, 5911, 30067, 172237, 1096319, 7677155, 58648421, 485377457, 4326008691]
13250869953362054385 ... 5834040476 (2371 digits)
 
Sequence of Fibonacci numbers -> A000744
BigInt[1, 2, 5, 14, 42, 144, 563, 2526, 12877, 73778, 469616, 3288428, 25121097, 207902202, 1852961189]
56757474139659741321 ... 6347879262 (2370 digits)
 
Sequence of factorial numbers -> A230960
BigInt[1, 2, 5, 17, 73, 381, 2347, 16701, 134993, 1222873, 12279251, 135425553, 1627809401, 21183890469, 296773827547]
13714256926920345740 ... 6070236763 (2566 digits)
</pre>
 
 
4,107

edits