Writing a recursive function to compute the Jacobi symbol

Click For Summary
SUMMARY

The forum discussion focuses on writing a recursive function to compute the Jacobi symbol J(a, n) for relatively prime integers a and n using C++. The function is defined with specific conditions: it returns 1 if a equals 1, computes J(a/2, n) multiplied by (-1) raised to the power of (n^2-1)/8 if a is even, and computes J(n % a, a) multiplied by (-1) raised to the power of ((a-1)*(n-1))/4 if a is odd. The provided C++ implementation demonstrates the function's usage by calculating the Jacobi symbol for the integers 5 and 7.

PREREQUISITES
  • Understanding of recursive functions in programming
  • Familiarity with C++ syntax and data types
  • Knowledge of number theory concepts, specifically relative prime numbers (coprime)
  • Basic understanding of mathematical functions and exponentiation
NEXT STEPS
  • Study the properties of coprime numbers and their significance in number theory
  • Learn about recursive algorithms and their applications in mathematical computations
  • Explore advanced C++ features such as templates and error handling for robust function design
  • Investigate the use of modular arithmetic in number theory problems
USEFUL FOR

Mathematicians, computer scientists, and software developers interested in number theory, particularly those implementing algorithms for mathematical functions in C++. This discussion is especially beneficial for individuals looking to deepen their understanding of recursive programming techniques.

John O' Meara
Messages
325
Reaction score
0
Write a recursive function to compute the Jacobi symbol J(a, n), is defined for relatively prime integers a and n, a> o, \mbox{ and } n > 0 by the formula
J(a, n) = 1 \mbox{ if a = 1, } = J(a/2, n)*(-1)^\frac{n^2-1}{8} \mbox{ if a is even, }
=J(n \% a, a)*(-1)^\frac{(a-1)*(n-1)}{4} \mbox{ otherwise }

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

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
5
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K