How to resolve the algorithm Primality by trial division step by step in the Haskell programming language
How to resolve the algorithm Primality by trial division step by step in the Haskell 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 Haskell programming language
The Haskell code provided defines a function isPrime
that determines whether a given integer n
is prime or not. Here's a detailed explanation of each line:
-
The first line defines a function called
isPrime
that takes an integern
as its input and returns a Boolean value indicating whethern
is prime. -
The second line defines the main logic for checking if
n
is prime:- If
n
is equal to 2, it is considered prime. - If
n
is greater than 2, it checks if all numbers from 2 to the square root ofn+1
(inclusive) do not evenly dividen
. If any of these numbers evenly dividesn
, thenn
is not prime.
- If
-
The third line defines a function called
noDivsBy
that takes a list of factorsfactors
and an integern
as its input and checks ifn
is divisible by any factor in the list. It returnsTrue
ifn
is not divisible by any of the factors, andFalse
otherwise. -
The fourth line defines a list of prime numbers called
primeNums
. It starts with 2 and then adds subsequent odd numbers (3, 5, 7, etc.) that are not divisible by any of the previous prime numbers in the list. -
The fifth line modifies the definition of
isPrime
to use thenoDivsBy
function and theprimeNums
list:- It checks if
n
is greater than 1 (to exclude negative numbers and 0). - It then uses
noDivsBy
to check ifn
is not divisible by any of the prime numbers in theprimeNums
list. Ifn
is divisible by any of these primes, it is not prime.
- It checks if
-
The sixth line redefines the
isPrime
function using a list comprehension:- It again checks if
n
is greater than 1. - It then creates a list of all numbers from 2 to
n-1
and filters out the numbers that evenly dividen
. - If the resulting list is empty, it means no numbers from 2 to
n-1
evenly dividen
, and hencen
is prime.
- It again checks if
-
The seventh line provides an alternative definition of
isPrime
using a list comprehension:- It checks if
n
is greater than 1. - It then filters out the numbers from 2 to
n-1
that are both prime and evenly dividen
. - If the resulting list is empty, it means
n
is prime.
- It checks if
Source code in the haskell programming language
isPrime n = n==2 || n>2 && all ((> 0).rem n) (2:[3,5..floor.sqrt.fromIntegral $ n+1])
noDivsBy factors n = foldr (\f r-> f*f>n || ((rem n f /= 0) && r)) True factors
-- primeNums = filter (noDivsBy [2..]) [2..]
-- = 2 : filter (noDivsBy [3,5..]) [3,5..]
primeNums = 2 : 3 : filter (noDivsBy $ tail primeNums) [5,7..]
isPrime n = n > 1 && noDivsBy primeNums n
isPrime n = n > 1 && []==[i | i <- [2..n-1], rem n i == 0]
isPrime n = n > 1 && []==[i | i <- [2..n-1], isPrime i && rem n i == 0]
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the zkl programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Nim programming language
You may also check:How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Haskell programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the J programming language
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the Wren programming language