### Find Prime Number using Factor Command

October31

Hi All,

Here is the one line command which i written to find out the number is prime or not.

1 2 3 4 5 6 7 8 | $read number;result=`factor $number | wc -w`; [ $result = "2" ] && echo "$number is prime Number" || echo "$number is not prime number" ; echo "Factors of" `factor $number` 78 78 is not prime number Factors of 78: 2 3 13 $read number;result=`factor $number | wc -w`; [ $result = "2" ] && echo "$number is prime Number" || echo "$number is not prime number" ; echo "Factors of" `factor $number` 13 13 is prime Number Factors of 13: 13 |

If you like to check for all the numbers then use the below command

1 2 3 4 5 6 7 8 9 10 11 12 | $ for i in `seq 1 10`; do result=`factor $i | wc -w`; [ $result = "2" ] && echo "$i is prime Number" || echo "$i is not prime number" ; done 1 is not prime number 2 is prime Number 3 is prime Number 4 is not prime number 5 is prime Number 6 is not prime number 7 is prime Number 8 is not prime number 9 is not prime number 10 is not prime number |

To display only the prime numbers

1 2 3 4 5 | $ for i in `seq 1 10`; do result=`factor $i | wc -w`; [ $result = "2" ] && echo "$i is prime Number"; done 2 is prime Number 3 is prime Number 5 is prime Number 7 is prime Number |

**Note :** Change the seq command input for more output

## Recent Comments