Jacobi symbol: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16: | Line 16: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F jacobi(=a, =n) |
||
I n <= 0 |
I n <= 0 |
||
X ValueError(‘'n' must be a positive integer.’) |
X ValueError(‘'n' must be a positive integer.’) |
||
Line 50: | Line 50: | ||
L(k) 0..kmax |
L(k) 0..kmax |
||
print(‘#3’.format(jacobi(k, n)), end' ‘’) |
print(‘#3’.format(jacobi(k, n)), end' ‘’) |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 71: | Line 71: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit |
||
INT FUNC Jacobi(INT a,n) |
INT FUNC Jacobi(INT a,n) |
||
Line 136: | Line 136: | ||
Put(125) PutE() ;clear the screen |
Put(125) PutE() ;clear the screen |
||
PrintTable(10,39) |
PrintTable(10,39) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Jacobi_symbol.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Jacobi_symbol.png Screenshot from Atari 8-bit computer] |
||
Line 166: | Line 166: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="rebol">jacobi: function [n,k][ |
||
N: n % k |
N: n % k |
||
K: k |
K: k |
||
Line 195: | Line 195: | ||
'item -> pad.left item 2 |
'item -> pad.left item 2 |
||
] |
] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 214: | Line 214: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">result := "n/k|" |
||
loop 20 |
loop 20 |
||
result .= SubStr(" " A_Index, -1) " " |
result .= SubStr(" " A_Index, -1) " " |
||
Line 246: | Line 246: | ||
} |
} |
||
return (n=1) ? t : 0 |
return (n=1) ? t : 0 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n/k| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<pre>n/k| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
||
Line 264: | Line 264: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f JACOBI_SYMBOL.AWK |
# syntax: GAWK -f JACOBI_SYMBOL.AWK |
||
BEGIN { |
BEGIN { |
||
Line 311: | Line 311: | ||
return(0) |
return(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 334: | Line 334: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 372: | Line 372: | ||
print_table(20, 21); |
print_table(20, 21); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 393: | Line 393: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <cassert> |
#include <cassert> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 436: | Line 436: | ||
print_table(std::cout, 20, 21); |
print_table(std::cout, 20, 21); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 457: | Line 457: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Swift}} |
{{trans|Swift}} |
||
< |
<syntaxhighlight lang="ruby">def jacobi(a, n) |
||
raise ArgumentError.new "n must b positive and odd" if n < 1 || n.even? |
raise ArgumentError.new "n must b positive and odd" if n < 1 || n.even? |
||
res = 1 |
res = 1 |
||
Line 478: | Line 478: | ||
(0..10).each { |a| printf(" % 2d", jacobi(a, n)) } |
(0..10).each { |a| printf(" % 2d", jacobi(a, n)) } |
||
puts |
puts |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 497: | Line 497: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
jacobi(_, N) when N =< 0 -> jacobi_domain_error; |
jacobi(_, N) when N =< 0 -> jacobi_domain_error; |
||
jacobi(_, N) when (N band 1) =:= 0 -> jacobi_domain_error; |
jacobi(_, N) when (N band 1) =:= 0 -> jacobi_domain_error; |
||
Line 525: | Line 525: | ||
false -> J2 |
false -> J2 |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
//Jacobi Symbol. Nigel Galloway: July 14th., 2020 |
//Jacobi Symbol. Nigel Galloway: July 14th., 2020 |
||
let J n m=let rec J n m g=match n with |
let J n m=let rec J n m g=match n with |
||
Line 537: | Line 537: | ||
printfn "n\m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30\n ----------------------------------------------------------------------------------------------------------------------" |
printfn "n\m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30\n ----------------------------------------------------------------------------------------------------------------------" |
||
[1..2..29]|>List.iter(fun m->printf "%3d" m; [1..30]|>List.iter(fun n->printf "%4d" (J n m)); printfn "") |
[1..2..29]|>List.iter(fun m->printf "%3d" m; [1..30]|>List.iter(fun n->printf "%4d" (J n m)); printfn "") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 563: | Line 563: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function gcdp( a as uinteger, b as uinteger ) as uinteger |
||
if b = 0 then return a |
if b = 0 then return a |
||
return gcdp( b, a mod b ) |
return gcdp( b, a mod b ) |
||
Line 607: | Line 607: | ||
next k |
next k |
||
print outstr |
print outstr |
||
next pn</ |
next pn</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<pre> k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
||
Line 633: | Line 633: | ||
This translates the Lua code in the above referenced Wikipedia article to Go (for 8 byte integers) and checks that it gives the same answers for a small table of values - which it does. |
This translates the Lua code in the above referenced Wikipedia article to Go (for 8 byte integers) and checks that it gives the same answers for a small table of values - which it does. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 692: | Line 692: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 725: | Line 725: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{trans|Scheme}} |
{{trans|Scheme}} |
||
< |
<syntaxhighlight lang="haskell">jacobi :: Integer -> Integer -> Integer |
||
jacobi 0 1 = 1 |
jacobi 0 1 = 1 |
||
jacobi 0 _ = 0 |
jacobi 0 _ = 0 |
||
Line 738: | Line 738: | ||
else if rem a_mod_n 4 == 3 && rem n 4 == 3 |
else if rem a_mod_n 4 == 3 && rem n 4 == 3 |
||
then negate $ jacobi n a_mod_n |
then negate $ jacobi n a_mod_n |
||
else jacobi n a_mod_n</ |
else jacobi n a_mod_n</syntaxhighlight> |
||
Or, expressing it slightly differently, and adding a tabulation: |
Or, expressing it slightly differently, and adding a tabulation: |
||
< |
<syntaxhighlight lang="haskell">import Data.Bool (bool) |
||
import Data.List (replicate, transpose) |
import Data.List (replicate, transpose) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
Line 813: | Line 813: | ||
justifyRight :: Int -> a -> [a] -> [a] |
justifyRight :: Int -> a -> [a] -> [a] |
||
justifyRight n c = (drop . length) <*> (replicate n c <>)</ |
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 10 |
<pre> 0 1 2 3 4 5 6 7 8 9 10 |
||
Line 828: | Line 828: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang J> |
|||
NB. functionally equivalent translation of the Lua program found |
NB. functionally equivalent translation of the Lua program found |
||
NB. at https://en.wikipedia.org/wiki/Jacobi_symbol |
NB. at https://en.wikipedia.org/wiki/Jacobi_symbol |
||
Line 842: | Line 842: | ||
end. |
end. |
||
t*x=1 |
t*x=1 |
||
}}"0</ |
}}"0</syntaxhighlight> |
||
Line 887: | Line 887: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
public class JacobiSymbol { |
public class JacobiSymbol { |
||
Line 938: | Line 938: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 961: | Line 961: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
||
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l)[:$l]; |
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l)[:$l]; |
||
Line 982: | Line 982: | ||
(range( 1; 32; 2) as $n |
(range( 1; 32; 2) as $n |
||
| "\($n|rpad(3))" + reduce range(1; 13) as $a (""; . + (jacobi($a; $n) | lpad(4) )) |
| "\($n|rpad(3))" + reduce range(1; 13) as $a (""; . + (jacobi($a; $n) | lpad(4) )) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,007: | Line 1,007: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia">function jacobi(a, n) |
||
a %= n |
a %= n |
||
result = 1 |
result = 1 |
||
Line 1,030: | Line 1,030: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Table of jacobi(a, n) for a 1 to 12, n 1 to 31 |
Table of jacobi(a, n) for a 1 to 12, n 1 to 31 |
||
Line 1,055: | Line 1,055: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">fun jacobi(A: Int, N: Int): Int { |
||
assert(N > 0 && N and 1 == 1) |
assert(N > 0 && N and 1 == 1) |
||
var a = A % N |
var a = A % N |
||
Line 1,077: | Line 1,077: | ||
} |
} |
||
return if (n == 1) result else 0 |
return if (n == 1) result else 0 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">TableForm[Table[JacobiSymbol[n, k], {n, 1, 17, 2}, {k, 16}], |
||
TableHeadings -> {ReplacePart[Range[1, 17, 2], 1 -> "n=1"], |
TableHeadings -> {ReplacePart[Range[1, 17, 2], 1 -> "n=1"], |
||
ReplacePart[Range[16], 1 -> "k=1"]}]</ |
ReplacePart[Range[16], 1 -> "k=1"]}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Produces a nicely typeset table. |
Produces a nicely typeset table. |
||
Line 1,088: | Line 1,088: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Translation of the Lua program from Wikipedia page. |
Translation of the Lua program from Wikipedia page. |
||
< |
<syntaxhighlight lang="nim">template isOdd(n: int): bool = (n and 1) != 0 |
||
template isEven(n: int): bool = (n and 1) == 0 |
template isEven(n: int): bool = (n and 1) == 0 |
||
Line 1,121: | Line 1,121: | ||
for n in 1..20: |
for n in 1..20: |
||
stdout.write align($jacobi(n, k), 3) |
stdout.write align($jacobi(n, k), 3) |
||
echo ""</ |
echo ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,140: | Line 1,140: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
Line 1,172: | Line 1,172: | ||
printf '%4d', J($_, $n) for 1..$maxa; |
printf '%4d', J($_, $n) for 1..$maxa; |
||
print "\n" |
print "\n" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n\k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<pre>n\k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
||
Line 1,193: | Line 1,193: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobi</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobi</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
||
Line 1,219: | Line 1,219: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,243: | Line 1,243: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def jacobi(a, n): |
||
if n <= 0: |
if n <= 0: |
||
raise ValueError("'n' must be a positive integer.") |
raise ValueError("'n' must be a positive integer.") |
||
Line 1,263: | Line 1,263: | ||
return result |
return result |
||
else: |
else: |
||
return 0</ |
return 0</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,269: | Line 1,269: | ||
{{works with|Rakudo|2019.11}} |
{{works with|Rakudo|2019.11}} |
||
<lang |
<syntaxhighlight lang="raku" line># Jacobi function |
||
sub infix:<J> (Int $k is copy, Int $n is copy where * % 2) { |
sub infix:<J> (Int $k is copy, Int $n is copy where * % 2) { |
||
$k %= $n; |
$k %= $n; |
||
Line 1,298: | Line 1,298: | ||
} |
} |
||
print "\n"; |
print "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n\k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<pre>n\k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
||
Line 1,322: | Line 1,322: | ||
<br>A little extra code was added to make a prettier grid. |
<br>A little extra code was added to make a prettier grid. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm computes/displays the Jacobi symbol, the # of rows & columns can be specified*/ |
||
parse arg rows cols . /*obtain optional arguments from the CL*/ |
parse arg rows cols . /*obtain optional arguments from the CL*/ |
||
if rows='' | rows=="," then rows= 17 /*Not specified? Then use the default.*/ |
if rows='' | rows=="," then rows= 17 /*Not specified? Then use the default.*/ |
||
Line 1,355: | Line 1,355: | ||
end /*while a\==0*/ |
end /*while a\==0*/ |
||
if n==1 then return $ |
if n==1 then return $ |
||
return 0</ |
return 0</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,374: | Line 1,374: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Crystal}} |
{{trans|Crystal}} |
||
< |
<syntaxhighlight lang="ruby">def jacobi(a, n) |
||
raise ArgumentError.new "n must b positive and odd" if n < 1 || n.even? |
raise ArgumentError.new "n must b positive and odd" if n < 1 || n.even? |
||
res = 1 |
res = 1 |
||
Line 1,395: | Line 1,395: | ||
(0..10).each { |a| printf(" % 2d", jacobi(a, n)) } |
(0..10).each { |a| printf(" % 2d", jacobi(a, n)) } |
||
puts |
puts |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,414: | Line 1,414: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="rust">fn jacobi(mut n: i32, mut k: i32) -> i32 { |
||
assert!(k > 0 && k % 2 == 1); |
assert!(k > 0 && k % 2 == 1); |
||
n %= k; |
n %= k; |
||
Line 1,460: | Line 1,460: | ||
fn main() { |
fn main() { |
||
print_table(20, 21); |
print_table(20, 21); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,480: | Line 1,480: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
def jacobi(a_p: Int, n_p: Int): Int = |
def jacobi(a_p: Int, n_p: Int): Int = |
||
{ |
{ |
||
Line 1,513: | Line 1,513: | ||
} yield println("n = " + n + ", a = " + a + ": " + jacobi(a, n)) |
} yield println("n = " + n + ", a = " + a + ": " + jacobi(a, n)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out|output}} |
{{out|output}} |
||
Line 1,629: | Line 1,629: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define jacobi (lambda (a n) |
||
(let ((a-mod-n (modulo a n))) |
(let ((a-mod-n (modulo a n))) |
||
(if (zero? a-mod-n) |
(if (zero? a-mod-n) |
||
Line 1,641: | Line 1,641: | ||
(if (and (= (modulo a-mod-n 4) 3) (= (modulo n 4) 3)) |
(if (and (= (modulo a-mod-n 4) 3) (= (modulo n 4) 3)) |
||
(- (jacobi n a-mod-n)) |
(- (jacobi n a-mod-n)) |
||
(jacobi n a-mod-n)))))))</ |
(jacobi n a-mod-n)))))))</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Line 1,647: | Line 1,647: | ||
Also built-in as '''kronecker(n,k)'''. |
Also built-in as '''kronecker(n,k)'''. |
||
< |
<syntaxhighlight lang="ruby">func jacobi(n, k) { |
||
assert(k > 0, "#{k} must be positive") |
assert(k > 0, "#{k} must be positive") |
||
Line 1,666: | Line 1,666: | ||
for n in (0..50), k in (0..50) { |
for n in (0..50), k in (0..50) { |
||
assert_eq(jacobi(n, 2*k + 1), kronecker(n, 2*k + 1)) |
assert_eq(jacobi(n, 2*k + 1), kronecker(n, 2*k + 1)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func jacobi(a: Int, n: Int) -> Int { |
func jacobi(a: Int, n: Int) -> Int { |
||
Line 1,709: | Line 1,709: | ||
print() |
print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,727: | Line 1,727: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">fn jacobi(aa u64, na u64) ?int { |
||
mut a := aa |
mut a := aa |
||
mut n := na |
mut n := na |
||
Line 1,768: | Line 1,768: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,787: | Line 1,787: | ||
{{trans|Python}} |
{{trans|Python}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var jacobi = Fn.new { |a, n| |
var jacobi = Fn.new { |a, n| |
||
Line 1,819: | Line 1,819: | ||
System.print() |
System.print() |
||
n = n + 2 |
n = n + 2 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,844: | Line 1,844: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn jacobi(a,n){ |
||
if(n.isEven or n<1) |
if(n.isEven or n<1) |
||
throw(Exception.ValueError("'n' must be a positive odd integer")); |
throw(Exception.ValueError("'n' must be a positive odd integer")); |
||
Line 1,858: | Line 1,858: | ||
} |
} |
||
if(n==1) result else 0 |
if(n==1) result else 0 |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println("Using hand-coded version:"); |
||
println("n/a 0 1 2 3 4 5 6 7 8 9"); |
println("n/a 0 1 2 3 4 5 6 7 8 9"); |
||
println("---------------------------------"); |
println("---------------------------------"); |
||
Line 1,866: | Line 1,866: | ||
foreach a in (10){ print(" % d".fmt(jacobi(a,n))) } |
foreach a in (10){ print(" % d".fmt(jacobi(a,n))) } |
||
println(); |
println(); |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
||
< |
<syntaxhighlight lang="zkl">var [const] BI=Import.lib("zklBigNum"); // libGMP |
||
println("\nUsing BigInt library function:"); |
println("\nUsing BigInt library function:"); |
||
println("n/a 0 1 2 3 4 5 6 7 8 9"); |
println("n/a 0 1 2 3 4 5 6 7 8 9"); |
||
Line 1,876: | Line 1,876: | ||
foreach a in (10){ print(" % d".fmt(BI(a).jacobi(n))) } |
foreach a in (10){ print(" % d".fmt(BI(a).jacobi(n))) } |
||
println(); |
println(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |