How to resolve the algorithm AKS test for primes step by step in the Kotlin programming language
How to resolve the algorithm AKS test for primes step by step in the Kotlin 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 Kotlin programming language
Binomial Coefficient Calculator (binomial
Function)
The binomial
function calculates the binomial coefficient C(n, k)
using an optimized algorithm. It takes two integers, n
and k
, and returns the value of the binomial coefficient:
- For negative
n
ork
, it throws anIllegalArgumentException
. - For
k
equal to 0 orn
, it returns 1. - Otherwise, it uses a loop to calculate the binomial coefficient and simplifies the result by dividing out common factors.
Prime Number Checker (isPrime
Function)
The isPrime
function checks if a given integer n
is prime. It uses a simple algorithm that iterates through all integers from 1 to n-1
and checks if n
is divisible by any of them. If it finds a divisor, it returns false
, indicating that n
is not prime. Otherwise, it returns true
.
Main Function
The main
function is the entry point of the program. It performs two tasks:
-
Expanding the Binomial Series: It iterates from
n = 0
ton = 9
and calculates the expansion of the binomial series(x - 1)^n
for each value ofn
. It prints the expansion in the form of a polynomial with proper signs. -
Generating Prime Numbers: It generates prime numbers under 62 using the
isPrime
function. It stores the prime numbers in a mutable list calledprimes
.
Overall Functionality
The program demonstrates the use of the binomial coefficient calculation and prime number checking functions to perform two operations:
- Expand the binomial series
(x - 1)^n
for small values ofn
. - Generate all prime numbers under 62.
Source code in the kotlin programming language
// version 1.1
fun binomial(n: Int, k: Int): Long = when {
n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed")
k == 0 -> 1L
k == n -> 1L
else -> {
var prod = 1L
var div = 1L
for (i in 1..k) {
prod *= (n + 1 - i)
div *= i
if (prod % div == 0L) {
prod /= div
div = 1L
}
}
prod
}
}
fun isPrime(n: Int): Boolean {
if (n < 2) return false
return (1 until n).none { binomial(n, it) % n.toLong() != 0L }
}
fun main(args: Array<String>) {
var coeff: Long
var sign: Int
var op: String
for (n in 0..9) {
print("(x - 1)^$n = ")
sign = 1
for (k in n downTo 0) {
coeff = binomial(n, k)
op = if (sign == 1) " + " else " - "
when (k) {
n -> print("x^$n")
0 -> println("${op}1")
else -> print("$op${coeff}x^$k")
}
if (n == 0) println()
sign *= -1
}
}
// generate primes under 62
var p = 2
val primes = mutableListOf<Int>()
do {
if (isPrime(p)) primes.add(p)
if (p != 2) p += 2 else p = 3
}
while (p < 62)
println("\nThe prime numbers under 62 are:")
println(primes)
}
You may also check:How to resolve the algorithm Generator/Exponential step by step in the C# programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Crystal programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Pure programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Rust programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the PicoLisp programming language