[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

26. Numeric operations

These programs do numerically-related operations.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

26.1 factor: Print prime factors

factor prints prime factors. Synopses:

 
factor [number]…
factor option

If no number is specified on the command line, factor reads numbers from standard input, delimited by newlines, tabs, or spaces.

The factor command supports only a small number of options:

`--help'

Print a short help on standard output, then exit without further processing.

`--version'

Print the program version on standard output, then exit without further processing.

Factoring the product of the eighth and ninth Mersenne primes takes about 30 milliseconds of CPU time on a 2.2 GHz Athlon.

 
M8=`echo 2^31-1|bc` ; M9=`echo 2^61-1|bc`
/usr/bin/time -f '%U' factor $(echo "$M8 * $M9" | bc)
4951760154835678088235319297: 2147483647 2305843009213693951
0.03

Similarly, factoring the eighth Fermat number 2^{256+1 takes about 20 seconds on the same machine.

Factoring large prime numbers is, in general, hard. The Pollard Rho algorithm used by factor is particularly effective for numbers with relatively small factors. If you wish to factor large numbers which do not have small factors (for example, numbers which are the product of two large primes), other methods are far better.

If factor is built without using GNU MP, only single-precision arithmetic is available, and so large numbers (typically 2^{64 and above) will not be supported. The single-precision code uses an algorithm which is designed for factoring smaller numbers.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

26.2 seq: Print numeric sequences

seq prints a sequence of numbers to standard output. Synopses:

 
seq [option]… last
seq [option]… first last
seq [option]… first increment last

seq prints the numbers from first to last by increment. By default, each number is printed on a separate line. When increment is not specified, it defaults to `1', even when first is larger than last. first also defaults to `1'. So seq 1 prints `1', but seq 0 and seq 10 5 produce no output. Floating-point numbers may be specified (using a period before any fractional digits).

The program accepts the following options. Also see Common options. Options must precede operands.

`-f format'
`--format=format'

Print all numbers using format. format must contain exactly one of the `printf'-style floating point conversion specifications `%a', `%e', `%f', `%g', `%A', `%E', `%F', `%G'. The `%' may be followed by zero or more flags taken from the set `-+#0 '', then an optional width containing one or more digits, then an optional precision consisting of a `.' followed by zero or more digits. format may also contain any number of `%%' conversion specifications. All conversion specifications have the same meaning as with `printf'.

The default format is derived from first, step, and last. If these all use a fixed point decimal representation, the default format is `%.pf', where p is the minimum precision that can represent the output numbers exactly. Otherwise, the default format is `%g'.

`-s string'
`--separator=string'

Separate numbers with string; default is a newline. The output always terminates with a newline.

`-w'
`--equal-width'

Print all numbers with the same width, by padding with leading zeros. first, step, and last should all use a fixed point decimal representation. (To have other kinds of padding, use `--format').

You can get finer-grained control over output with `-f':

 
$ seq -f '(%9.2E)' -9e5 1.1e6 1.3e6
(-9.00E+05)
( 2.00E+05)
( 1.30E+06)

If you want hexadecimal integer output, you can use printf to perform the conversion:

 
$ printf '%x\n' `seq 1048575 1024 1050623`
fffff
1003ff
1007ff

For very long lists of numbers, use xargs to avoid system limitations on the length of an argument list:

 
$ seq 1000000 | xargs printf '%x\n' | tail -n 3
f423e
f423f
f4240

To generate octal output, use the printf %o format instead of %x.

On most systems, seq can produce whole-number output for values up to at least 2^{53. Larger integers are approximated. The details differ depending on your floating-point implementation, but a common case is that seq works with integers through 2^{64, and larger integers may not be numerically correct:

 
$ seq 18446744073709551616 1 18446744073709551618
18446744073709551616
18446744073709551616
18446744073709551618

Be careful when using seq with outlandish values: otherwise you may see surprising results, as seq uses floating point internally. For example, on the x86 platform, where the internal representation uses a 64-bit fraction, the command:

 
seq 1 0.0000000000000000001 1.0000000000000000009

outputs 1.0000000000000000007 twice and skips 1.0000000000000000008.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on January, 20 2010 using texi2html 1.76.