Pages

Linux script to find prime numbers in a range of numbers

In the post " " we saw how we can use the command factor to find whether a number is prime of not. We can extend the same script further to generate all the prime numbers in a given range of numbers.

To generate a range of numbers we can use the command "seq" . The usage of seq is shown in the post " ".

We can use seq in a for loop to iterate over one number at time.



Save the script as seq_for.sh, give it execute permissions and run the script.



Thus we can see that using seq along with for loop we can work on a range of numbers, one number at a time.

This script can be combined with the script in " " to generate all the prime numbers in a range of numbers.

We request the user to enter two numbers to indicate the lower and upper limits of the range of numbers between which the user wants to generate the prime numbers. The lower limit can not be 1 because the number 1 does not have any factors and the command "factor" does not return any thing for 1. Thus we need to make sure the user enters a number bigger than 1. If the user enters 1, then we will prompt the user again to enter number greater than 1.



Then we pass these two numbers to seq in a for loop



In each iteration of the for loop we use the factor command to find out if a number is prime or not.



The full script looks as below.



Save the script as range_prime.sh,give it execute permissions and run it.



Thus we can see the script is able to generate a list of all the prime numbers between 2 and 20.

15 comments:

  1. What if I want to store the output elements in an array and want the output as a single list???

    ReplyDelete
    Replies
    1. Use the following script if you want the results in an array

      #! /bin/bash
      low=1
      count=0

      while [ $low -eq 1 ]
      do
      echo "Enter the lower limit,greater than 1"
      read low
      done

      echo "Enter the upper limit"
      read upper


      for mun in `seq $low $upper`
      do
      ret=$(factor $mun | grep $mun | cut -d ":" -f 2 | cut -d " " -f 2)

      if [ "$ret" -eq "$mun" ]
      then
      #echo "$mun is prime"
      res[count]=$mun
      ((count++))
      fi
      done

      echo -e "\n There are $count number of prime numbers, which are "
      echo ${res[*]}

      Delete
    2. This comment has been removed by the author.

      Delete
    3. do
      ret=$(factor $mun | grep $mun | cut -d ":" -f 2 | cut -d " " -f 2)
      if [ "$ret" -eq "$mun" ]
      then
      echo "$mun is prime"
      fi
      done

      How does this part work? Help 🙏

      Delete
  2. how to get the sum off all prime numbers? plz

    ReplyDelete
    Replies
    1. For example, I want to add the prime numbers between 22-444 and divide by how many prime numbers there are and get the average. How can I do it?

      sorry for my bad english

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Refer to the post https://tuxthink.blogspot.com/2022/07/linux-bash-script-to-find-average-of.html

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. do
    ret=$(factor $mun | grep $mun | cut -d ":" -f 2 | cut -d " " -f 2)
    if [ "$ret" -eq "$mun" ]
    then
    echo "$mun is prime"
    fi
    done

    How does this part work? Help 🙏

    ReplyDelete
    Replies
    1. number which is also listed before the list of factors and from the remaining we pick the first factors, which would be the number itself if its a prime number. we compare the result of the above sequence of commands with the original number and if they match, the given number is prime

      Delete
  5. # !./bin/bash -x
    #Extend the program to take a range of number as input and output the Prime Numbers in that range.

    echo "enter the number of Range:"
    echo Formate L and M
    read L H
    for a in $(seq $L $H)
    do
    k=0
    for i in $(seq 2 $(expr $a - 1))
    do
    if [ $(expr $a % $i) -eq 0 ]
    then
    k=1
    break
    fi
    done
    if [ $k -eq 0 ]
    then
    echo $a "prime Number:"
    fi
    #Try this Formats

    ReplyDelete