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.
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.
the program is not working
ReplyDeleteWhat is the error you are getting ?
DeleteWhat if I want to store the output elements in an array and want the output as a single list???
ReplyDeleteUse the following script if you want the results in an array
Delete#! /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[*]}
This comment has been removed by the author.
Deletedo
Deleteret=$(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 🙏
how to get the sum off all prime numbers? plz
ReplyDeleteWhat do you mean by all ?
DeleteFor 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?
Deletesorry for my bad english
This comment has been removed by the author.
DeleteRefer to the post https://tuxthink.blogspot.com/2022/07/linux-bash-script-to-find-average-of.html
DeleteThis comment has been removed by the author.
ReplyDeletedo
ReplyDeleteret=$(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 🙏
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# !./bin/bash -x
ReplyDelete#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