How to resolve the algorithm Primality by trial division step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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:

  1. The first line defines a function called isPrime that takes an integer n as its input and returns a Boolean value indicating whether n is prime.

  2. 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 of n+1 (inclusive) do not evenly divide n. If any of these numbers evenly divides n, then n is not prime.
  3. The third line defines a function called noDivsBy that takes a list of factors factors and an integer n as its input and checks if n is divisible by any factor in the list. It returns True if n is not divisible by any of the factors, and False otherwise.

  4. 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.

  5. The fifth line modifies the definition of isPrime to use the noDivsBy function and the primeNums list:

    • It checks if n is greater than 1 (to exclude negative numbers and 0).
    • It then uses noDivsBy to check if n is not divisible by any of the prime numbers in the primeNums list. If n is divisible by any of these primes, it is not prime.
  6. 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 divide n.
    • If the resulting list is empty, it means no numbers from 2 to n-1 evenly divide n, and hence n is prime.
  7. 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 divide n.
    • If the resulting list is empty, it means n is prime.

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