Search

Linux Script to find prime factors using the factor command

The command "factor" takes as input any integer number and returns the prime factors of the that number.

Example :



We can use this command to find wheter a given number is prime or not. If we pass a prime number as argument to the command factor, it returns the number itself as its prime factor. As shown above for the number 131.
We can pipe this output to grep,search for the number passed to factor in the output. If the output is comprised only of the input number then the given number is prime.
The following script does the same


Save the above script as prime.sh, and give it execute permissions



Now execute the script and see the output


No comments:

Post a Comment