How to resolve the algorithm Jacobi symbol step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Jacobi symbol step by step in the Swift programming language
Table of Contents
Problem Statement
The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)p_2^(k_2)...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p) If n is prime, then the Jacobi symbol (a | n) equals the Legendre symbol (a | n). Calculate the Jacobi symbol (a | n).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Jacobi symbol step by step in the Swift programming language
Source code in the swift programming language
import Foundation
func jacobi(a: Int, n: Int) -> Int {
var a = a % n
var n = n
var res = 1
while a != 0 {
while a & 1 == 0 {
a >>= 1
if n % 8 == 3 || n % 8 == 5 {
res = -res
}
}
(a, n) = (n, a)
if a % 4 == 3 && n % 4 == 3 {
res = -res
}
a %= n
}
return n == 1 ? res : 0
}
print("n/a 0 1 2 3 4 5 6 7 8 9")
print("---------------------------------")
for n in stride(from: 1, through: 17, by: 2) {
print(String(format: "%2d", n), terminator: "")
for a in 0..<10 {
print(String(format: " % d", jacobi(a: a, n: n)), terminator: "")
}
print()
}
You may also check:How to resolve the algorithm Queue/Definition step by step in the Lasso programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Phix programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the CLU programming language
You may also check:How to resolve the algorithm Negative base numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Arrays step by step in the PostScript programming language