How to resolve the algorithm Primality by trial division step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by trial division step by step in the Sidef programming language
Table of Contents
Problem Statement
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime. Use trial division. Even numbers greater than 2 may be eliminated right away. A loop from 3 to √ n will suffice, but other loops are allowed.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the Sidef programming language
Source code in the sidef programming language
func is_prime(a) {
given (a) {
when (2) { true }
case (a <= 1 || a.is_even) { false }
default { 3 .. a.isqrt -> any { .divides(a) } -> not }
}
}
func is_prime(n) {
return (n >= 2) if (n < 4)
return false if (n%%2 || n%%3)
for k in (5 .. n.isqrt -> by(6)) {
return false if (n%%k || n%%(k+2))
}
return true
}
You may also check:How to resolve the algorithm Fork step by step in the HicEst programming language
You may also check:How to resolve the algorithm Input loop step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm XML/Input step by step in the C programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Prolog programming language