How to resolve the algorithm AKS test for primes step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm AKS test for primes step by step in the C# programming language

Table of Contents

Problem Statement

The AKS algorithm for testing whether a number is prime is a polynomial-time algorithm based on an elementary theorem about Pascal triangles. The theorem on which the test is based can be stated as follows: are divisible by

p

{\displaystyle p}

.

Using

p

3

{\displaystyle p=3}

:

And all the coefficients are divisible by 3,   so 3 is prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm AKS test for primes step by step in the C# programming language

The provided C# code is designed to perform various mathematical operations, including computing binomial coefficients (coef() function), checking prime numbers (is_prime() function), and generating the expansion of the binomial expression (x-1)^n (show() function). A breakdown of the code:

  1. Binomial Coefficients (coef() Function):

    • Takes an integer n as input.
    • Computes the binomial coefficients for the expansion of (x-1)^n using Pascal's triangle.
    • Stores the coefficients in the array c.
  2. Prime Number Check (is_prime() Function):

    • Takes an integer n as input.
    • Computes the binomial coefficients for (x-1)^n.
    • Adjusts the coefficients c[0] and c[n] by adding and subtracting 1, respectively.
    • Checks if all the coefficients c[1] to c[n-1] are divisible by n.
    • If all coefficients are divisible, returns true (prime); otherwise, returns false (not prime).
  3. Binomial Expansion (show() Function):

    • Takes an integer n as input.
    • Outputs the expansion of (x-1)^n using the computed binomial coefficients stored in the c array.
    • Each term is displayed in the format "+(coefficient)x^(exponent)".
  4. Main Function:

    • Computes and displays the expansion of (x-1)^n for n from 0 to 9.
    • Generates and displays prime numbers up to 63 using the is_prime() function.

Example Usage:

When run, the code will output something like this:

(x-1)^0 = +1
(x-1)^1 = +x-1
(x-1)^2 = +x^2-2x+1
(x-1)^3 = +x^3-3x^2+3x-1
(x-1)^4 = +x^4-4x^3+6x^2-4x+1
(x-1)^5 = +x^5-5x^4+10x^3-10x^2+5x-1
(x-1)^6 = +x^6-6x^5+15x^4-20x^3+15x^2-6x+1
(x-1)^7 = +x^7-7x^6+21x^5-35x^4+35x^3-21x^2+7x-1
(x-1)^8 = +x^8-8x^7+28x^6-56x^5+70x^4-56x^3+28x^2-8x+1
(x-1)^9 = +x^9-9x^8+36x^7-84x^6+126x^5-126x^4+84x^3-36x^2+9x-1
Primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61

Source code in the csharp programming language

using System;
    public class AksTest
    {
        static long[] c = new long[100];

        static void Main(string[] args)
        {
        for (int n = 0; n < 10; n++) {
		coef(n);
		Console.Write("(x-1)^" + n + " = ");
		show(n);
		Console.WriteLine("");
	}	 
	   Console.Write("Primes:");
	  for (int n = 1; n <= 63; n++)
	     if (is_prime(n))
	       Console.Write(n + " ");
	 
	    Console.WriteLine('\n');
            Console.ReadLine();
        }

        static void coef(int n)
        {
            int i, j;

            if (n < 0 || n > 63) System.Environment.Exit(0);// gracefully deal with range issue

            for (c[i = 0] = 1L; i < n; c[0] = -c[0], i++)
                for (c[1 + (j = i)] = 1L; j > 0; j--)
                    c[j] = c[j - 1] - c[j];
        }

        static bool is_prime(int n)
        {
            int i;

            coef(n);
            c[0] += 1;
            c[i = n] -= 1;

            while (i-- != 0 && (c[i] % n) == 0) ;

            return i < 0;
        }

        static void show(int n)
	    {
		    do {
                Console.Write("+" + c[n] + "x^" + n);
		    }while (n-- != 0);
	    }
    }


  

You may also check:How to resolve the algorithm SHA-1 step by step in the Neko programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the Comal programming language
You may also check:How to resolve the algorithm Variadic function step by step in the zkl programming language
You may also check:How to resolve the algorithm A+B step by step in the NS-HUBASIC programming language