Writing a recursive function to compute the Jacobi symbol

In summary, the Jacobi symbol J(a, n) is a recursive function that is defined for relatively prime integers a and n, where a is greater than 0 and n is greater than 0. It returns a value of 1 if a = 1, a value of J(a/2, n)*(-1)^(n^2-1)/8 if a is even, and a value of J(n % a, a)*(-1)^((a-1)*(n-1))/4 otherwise. The function is implemented in the main function with an example of calculating the Jacobi symbol for a = 5 and n = 7. A relative prime number is a pair of integers that have no
  • #1
John O' Meara
330
0
Write a recursive function to compute the Jacobi symbol J(a, n), is defined for relatively prime integers a and n, [tex] a> o, \mbox{ and } n > 0[/tex] by the formula
[tex] J(a, n) = 1 \mbox{ if a = 1, } = J(a/2, n)*(-1)^\frac{n^2-1}{8} \mbox{ if a is even, }[/tex]
[tex]=J(n \% a, a)*(-1)^\frac{(a-1)*(n-1)}{4} \mbox{ otherwise } [/tex]

Code:
int jacobi(int a, int n)
{
   if (a == 1)
     return 1;
   else {
      if ((a % 2) == 0)
          return jacobi(a/2, n)*(pow(-1.0, (n*n-1)/8);
      else
          return jacobi(n % a, a)*(pow(-1.0, ((a-1)*(n-1))/4)); 
    }
 }
 int main() {
  cout << jacobi(5, 7);
   return 0;
 }
I seem not to be able to remember what a relative prime number is? And I don't think I understand the Jacobi function anyway. Any help would be welcome. Thanks in advance.
 
Last edited:
Physics news on Phys.org
  • #2

1. What is a recursive function?

A recursive function is a function that calls itself within its own code. This allows for repetitive tasks to be solved more efficiently and concisely.

2. What is the Jacobi symbol?

The Jacobi symbol is a mathematical symbol used to represent the relationship between two numbers, often used in number theory and cryptography. It is denoted as (a/n) and represents whether or not a number is a quadratic residue modulo n.

3. How does a recursive function compute the Jacobi symbol?

A recursive function for computing the Jacobi symbol uses the following formula: (a/n) = (a/p)(a/q) where p and q are prime factors of n. The function will continue to break down the prime factors until it reaches the base case of (a/1) = 1 or (a/2) = 1, and then work its way back up the recursion tree to compute the final result.

4. What are the advantages of using a recursive function to compute the Jacobi symbol?

Using a recursive function allows for a more efficient and concise way to solve repetitive tasks, as it eliminates the need for repetitive loops. It also allows for a clearer and more organized approach to solving complex problems such as computing the Jacobi symbol.

5. Are there any limitations to using a recursive function for computing the Jacobi symbol?

While recursive functions are useful for solving many problems, they can also be memory-intensive and may not be the most optimal solution for certain cases. Additionally, if the function is not properly coded, it can lead to infinite recursion and cause the program to crash.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
620
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
944
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top