Boustrophedon transform: Difference between revisions

→‎{{header|J}}: Alternate approach
m (trim output)
(→‎{{header|J}}: Alternate approach)
Line 73:
!@x: b 15
1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547</syntaxhighlight>
 
=== Alternate implementation ===
 
Instead of relying on recursion and memoization, we can deliberately perform the operations in the correct order:
 
<syntaxhighlight lang=J>B=: {{
M=: |:y#,:u i.y
for_i.(#~>:/"1)1+(,#:i.@*)~y-1 do.
M=:M (<i)}~(M{~<i-0 1)+M{~<(-/\i)-1 0
end.
M|:~<1 0
}}</syntaxhighlight>
 
Here, we start with a square matrix with the <code>a</code> values in sequence in the first column (first line). Then we fill in the remaining needed <code>T</code> values in row major order (for loop). Finally, we extract the diagonal (last line). Usage and results are the same as before.
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
6,962

edits