Jacobi symbol: Difference between revisions

No edit summary
Line 810:
</pre>
 
=={{header|jq}}==
{{trans|Julia}}
<lang jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l)[:$l];
 
def jacobi(a; n):
{a: (a % n), n: n, result: 1}
| until( .a == 0;
until( .a % 2 != 0;
.a /= 2
| if (.n % 8) | IN(3, 5) then .result *= -1 else . end )
| . + {a: .n, n: .a} # swap .a and .n
| if (.a % 4 == .n % 4 and .n % 4 == 3) then .result *= -1 else . end
| .a = .a % .n
)
| if .n == 1 then .result else 0 end
;
" Table of jacobi(a; n)",
"n\\k 1 2 3 4 5 6 7 8 9 10 11 12",
"_____________________________________________________",
(range( 1; 32; 2) as $n
| "\($n|rpad(3))" + reduce range(1; 13) as $a (""; . + (jacobi($a; $n) | lpad(4) ))
)</lang>
{{out}}
<pre>
Table of jacobi(a, n)
n\k 1 2 3 4 5 6 7 8 9 10 11 12
_____________________________________________________
1 1 1 1 1 1 1 1 1 1 1 1 1
3 1 -1 0 1 -1 0 1 -1 0 1 -1 0
5 1 -1 -1 1 0 1 -1 -1 1 0 1 -1
7 1 1 -1 1 -1 -1 0 1 1 -1 1 -1
9 1 1 0 1 1 0 1 1 0 1 1 0
11 1 -1 1 1 1 -1 -1 -1 1 -1 0 1
13 1 -1 1 1 -1 -1 -1 -1 1 1 -1 1
15 1 1 0 1 0 0 -1 1 0 0 -1 0
17 1 1 -1 1 -1 -1 -1 1 1 -1 -1 -1
19 1 -1 -1 1 1 1 1 -1 1 -1 1 -1
21 1 -1 0 1 1 0 0 -1 0 -1 -1 0
23 1 1 1 1 -1 1 -1 1 1 -1 -1 1
25 1 1 1 1 0 1 1 1 1 0 1 1
27 1 -1 0 1 -1 0 1 -1 0 1 -1 0
29 1 -1 -1 1 1 1 1 -1 1 -1 -1 -1
31 1 1 -1 1 1 -1 1 1 1 1 -1 -1</pre>
=={{header|Julia}}==
{{trans|Python}}
2,496

edits