Episode 27 – factor, primes, seq, and arithmetic

The last two episodes of Linux in the Shell discussed the use of bc as a command line scientific calculator and units for converting between different units. This episode will continue along the lines of mathematics by highlighting some other math related commands. First up is factors which, as the name implies, will output the prime factors of a given number. Prime factorization, or integer factorization, is the process of dividing a positive integer, that is greater than 1, into exact prime numbers. For instance, the following is a list of factors for positive integegers up to 20:

2 = 2
3 = 3
4 = 2 2
5 = 5
6 = 2 3
7 = 7
8 = 2 2 2
9 = 3 3
10 = 2 5
11 = 11
12 = 2 2 3
13 = 13
14 = 2 7
15 = 3 5
16 = 2 2 2 2
17 = 17
18 = 2 3 3
19 = 19
20 = 2 2 4

Basically, prime factoring involves dividing a number until all you have left is the prime factors whose product when multiplied together is the number you factored. The number 1 is not reported. The factor command, which is included in the coreutils package will factor any positive integer you provide:

factor 1382

Reports the following result:

1382: 2 691

There are only two options you can pass to factor and those are:

  • –version which will output the version of factor
  • –help which will output what factor does and these two options

The primes command takes a starting and optional stopping number and will output all the prime numbers from the start option to the stop option. If you do not provide a stop option it will continue until 4294967295 is reached or you kill the program with ctrl-c:

primes 500 1500

Primes does not have any options to pass other than the start and stop values. You must provide a start value of 0 or a positive integer. The stop value is optional but if used must be greater than the start value.

Chances are the primes command is not installed on your system by default. Primes is part of the BSD games package and should be an easy install via your package manager.
The seq command will output a complete list of floating point numbers given a starting and stopping number. You can optionally include an incremental value:

seq 5 .5 10

This example would start at the number 5 and show  numbers between 5 and 10 and increment each step by .5:


If a start number is not given seq defaults to 1. If you want to specify an increment value you must also specify a stop number. Otherwise it will treat the increment value as the stop number. If the stop number is greater than the start number the output will be null unless you provide a negative incremental value:

seq 10 1

Produces no output, but:

seq 10 -1 1

Produces a count down from 10 to 1 in 1 step decrements.

If you pass a value increments or decrements unevenly the sequence will end at the last possible number:

seq 1 .3 3

Produces the following output:


The seq command has a few options that it can take. The -s, or –separator=, will change the default separator between each number. The default is the newline (e.g.: \n):

seq -s, 1 10

Produces the output:


The -w, or –equal-width, will print all numbers with the same with padding with zeros where necessary:

seq -w 1 3 15



The -f, or –format=, option allows you to specify the printf-style floating point conversion specification to display the sequence in. The accepted values are:

  • %a – hexadecimal floating point, lowercase
  • %e – scientific notation, lowercase
  • %f – decimal floating point, lowercase
  • %g – Use shortest representation: %e or %f (this is default)
  • %A – hexadecimal floating point, uppercase
  • %E – scientific notation, uppercase
  • %F – decimal floating point, uppercase
  • %G – use shortest representation: %E or %F

To change the output sequence to hexadecimal floating point:

seq -f %a 1 6



For more information about the output formats for seq consult the info page.

Before closing this episode there is one more BSD Games program to cover: arithmetic. The arithmetic program is a mathematics quiz that will ask you a series of math problems. To proceeded to the next problem you have to answer the current problem correctly. The game will continue indefinitely printing your score after every 20 questions. The default operation of arithmetic will use the number 0 through 10 and only addition and subtraction. There are switches to change this.

The -o switch allows you to specify the operators which can be one, more, or all of the following:

  • + – addition
  • – – subtraction
  • / – division
  • * – multiplication


arithmetic -o +* – only addition and multiplication problems
arithmetic -o / – only division problems

The default range is 10 and this can be changed with the -r switch. Note that for addition and multiplication the range applies to the addends, terms or factors. For subtraction and division the range applies to divisor and quotient or the subtrahend and the difference.

arithmetic -r 1000

This sets the the terms range from 0 to 1000 for addition and the sutrahend and difference range from 0 to 1000 for subtraction.



If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much!

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published.