Linux factor command for beginners (with examples)

If you are a Linux command line user and your work revolves around prime numbers, there is a command line utility that can help you, the tool factor… In this article, we’ll quickly discuss things like why it exists, how it works, and how you can use it. Please note that all examples and instructions mentioned here have been tested on Ubuntu 16.04 LTS.

Linux factor command

The Linux factor command gives prime factors of a number. Here is the syntax for the tool:

factor [NUMBER]...

And here’s how the man page describes the utility:

Print the prime factors of each specified integer NUMBER.  If none are specified on the command 
line, read them from standard input.

Q1. How do I use the factor command?

Usage is pretty easy (you guessed it, through its syntax is described above). Just supply the number you want as an argument to the factor utility. For example, to find prime factors, say 20, use factor like this:

factor 20

Below is the result that was produced on the system:

[email protected]:~# factor 20                                                                                                                                    
20: 2 2 5

Of course, you can pass multiple numbers as an argument to the factor command in one go.

[email protected]:~#factor 20 30 40                                                                                                                               
20: 2 2 5                                                                                                                                                     
30: 2 3 5                                                                                                                                                     
40: 2 2 2 5

The tool does not have any other basic command line parameters other than the regular one. –Help and –Versionwhich almost every Linux command line tool has.

[email protected]:~# factor --help                                                                                                                                
Usage: factor [NUMBER]...                                                                                                                                     
  or:  factor OPTION                                                                                                                                          
Print the prime factors of each specified integer NUMBER.  If none                                                                                            
are specified on the command line, read them from standard input.                                                                                             
                                                                                                                                                              
      --help     display this help and exit                                                                                                                   
      --version  output version information and exit                                                                                                          
                                                                                                                                                              
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>                                                                                           
Full documentation at: <http://www.gnu.org/software/coreutils/factor>                                                                                         
or available locally via: info '(coreutils) factor invocation'

Q2. What algorithm does factor use?

According to the GNU documentation, the factor command uses the algorithm Pollard-Brent rho… Documentation (look here) also offers an efficient algorithm only for numbers with relatively small coefficients. Here’s an excerpt from the documentation:

From the documentation: Factoring large numbers is generally difficult. The Pollard-Brent rho algorithm used by factor is especially efficient for numbers with relatively small factorials. If you want to account for large numbers that don’t have small factorials (for example, a number that is the product of two large primes), other methods are much better. If factor is built without using GNU MP, only single-precision arithmetic is available, and therefore large numbers (usually 2 ^ {128} and above) will not be supported. Single precision code uses an algorithm that is designed to factor in a smaller number.

Q3. Why does factor exist?

Well, there is no official word about it, but experts assume someone (or a group) dealt with prime numbers and developed the tool some half a century ago. And the fact that it has since been included in Unix and Linux distributions can be attributed to the absence of any reason not to.

A discussion on this topic can be found here

Conclusion

Obviously, the factor command is a niche tool. Only those who use prime numbers / factors frequently might find it useful. But then again, there is absolutely no harm in learning about utility (especially when there is little to no learning curve associated with it).

You can access factor’s man page here

Sidebar