Haskell program to display all prime numbers from 1 to n


This tutorial will discuss writing a program to display all prime numbers from 1 to N in Haskell Programming Language. Haskell is a declarative, strongly typed, and functional language. The computations in Haskell are mathematical functions.

A prime number is one that must have two positive factors 1 and the number itself.

Example 2,3,5,7,..

Note 1 is not a prime number because it has only one factor.

Algorithm steps

  • Implementing a function to check whether a number is prime.

  • Implementing a function to generate all prime in a range.

  • Display the prime numbers.

  • Program to Display all primes from 1 to N

  • We break the program into simpler functions.

Syntax

Function to find all factors of a number.

–- function declaration
factors :: Int->[Int]
–- function definition
factors n = [x | x<-[1..n], (mod n x)==0]

Above function is an utility function useful for checking whether a number is prime. We declared a function factor that takes the input of an Integer and returns a list of Integers. In the function, we are generating numbers 1 to n (argument) using list comprehension and Return numbers that divide argument n i.e list comprehension generates all factors of an argument n and returns them as a list.

Ex: output for factors 50 is:
[1,2,5,10,25,50]
Function to check a number is prime
-- function declaration
isPrime :: Int->Bool
-- function definition
isPrime n = (factors n) == [1,n]

Above function is an utility function useful in generating prime numbers in a given range. We declared a function isPrime which takes Integer as an input and returns a boolean. In the function definition, we are invoking the factors function with the argument as a first parameter and comparing the returned list with a list containing two numbers one, and an argument i.e This function checks whether the factors of a number are equal to one and the number itself which is a definition of a prime number, Returns true if the number is prime else returns false.

Ex: Output for isPrime 13 is:
True

Function to generate primes from 1 to N in iterative way

-- function declaration
generatePrime :: Int->[Int]
-- function definition
generatePrime n = [x | x<-[1..n], isPrime x]

Above function generates all primes from 1 to N. We declared a function generatePrime which takes an Integer as an argument and returns the list of Integers. In the function definition, we are generating a list of integers from 1 to n using list comprehension and filtering them using isPrime function which returns only numbers that are primes.

Ex: output for generatePrime 50 is:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]

Function to generate primes from 1 to N in recursive way

-- function declaration
generatePrime2 :: Int->[Int]
-- function definition

-- base case
generatePrime2 0 = []

generatePrime2 n = if (isPrime n)
then generatePrime2 (n-1) ++ [n]
else generatePrime2 (n-1)

Above function generatePrime2 generate all primes from 1 to N in a recursive way. We declared the function as it takes an integer as input and returns a list of integers. In the function definition, we are taking an integer n as an argument and we are checking whether it is prime, If n is prime we are concatenating n with the result returned by a recursive call with argument n-1. If n is not prime we are returning the recursive call with an argument n-1. This goes on until the base case is reached, i.e when n reaches 0 where a function returns an empty list. This checks from n to 0 for primes recursively.

Example: Output for generatePrime 60 is:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59]

Overall Program

factors :: Int->[Int] factors n = [x | x<-[1..n], (mod n x)==0] isPrime :: Int->Bool isPrime n = (factors n) == [1,n] generatePrime :: Int->[Int] generatePrime n = [x | x<-[1..n], isPrime x] main = do let n = 100 print (generatePrime n)

Output

[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

In the above program we combined all the utility functions to generate prime numbers in a given range. In the main function, we declared and initialized the number n with the value 100 and we invoked the function generatePrime which generates all prime numbers from 1 to N. Finally, we printed the returned list of the invoked function.

Conclusion

In this tutorial, we discussed writing a program to display all primes from 1 to N in Haskekell Programming Language.

Updated on: 27-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements