[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These programs do numerically-related operations.
25.1 factor
: Print prime factorsShow factors of numbers. 25.2 seq
: Print numeric sequencesPrint sequences of numbers.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 only options are `--help' and `--version'. See section 2. Common options.
The algorithm it uses is not very sophisticated, so for some inputs
factor
runs for a long time. The hardest numbers to factor are
the products of large primes. Factoring the product of the two largest 32-bit
prime numbers takes over 10 minutes of CPU time on a 400MHz Pentium II.
$ p=`echo '4294967279 * 4294967291'|bc` $ factor $p 18446743979220271189: 4294967279 4294967291 |
In contrast, factor
factors the largest 64-bit number in just
over a tenth of a second:
$ factor `echo '2^64-1'|bc` 18446744073709551615: 3 5 17 257 641 65537 6700417 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
seq
: Print numeric sequences
seq
prints a sequence of numbers to standard output. Synopses:
seq [option]... [first [increment]] last... |
seq
prints the numbers from first to last by
increment. By default, first and increment are both 1,
and each number is printed on its own line. All numbers can be reals,
not just integers.
The program accepts the following options. Also see 2. Common options.
If you want to use seq
to print sequences of large integer values,
don't use the default `%g' format since it can result in
loss of precision:
$ seq 1000000 1000001 1e+06 1e+06 |
Instead, you can use the format, `%1.f', to print large decimal numbers with no exponent and no decimal point.
$ seq --format=%1.f 1000000 1000001 1000000 1000001 |
If you want hexadecimal output, you can use printf
to perform the conversion:
$ printf %x'\n' `seq -f %1.f 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 -f %1.f 1000000 | xargs printf %x'\n' | tail -n 3 f423e f423f f4240 |
To generate octal output, use the printf %o
format instead
of %x
. Note however that using printf works only for numbers
smaller than 2^32
:
$ printf "%x\n" `seq -f %1.f 4294967295 4294967296` ffffffff bash: printf: 4294967296: Numerical result out of range |
On most systems, seq can produce whole-number output for values up to
2^53
, so here's a more general approach to base conversion that
also happens to be more robust for such large numbers. It works by
using bc
and setting its output radix variable, obase,
to `16' in this case to produce hexadecimal output.
$ (echo obase=16; seq -f %1.f 4294967295 4294967296)|bc FFFFFFFF 100000000 |
Be careful when using seq
with a fractional increment,
otherwise you may see surprising results. Most people would expect to
see 0.3
printed as the last number in this example:
$ seq -s' ' 0 .1 .3 0 0.1 0.2 |
But that doesn't happen on most systems because seq
is
implemented using binary floating point arithmetic (via the C
double
type) -- which means some decimal numbers like .1
cannot be represented exactly. That in turn means some nonintuitive
conditions like .1 * 3 > .3
will end up being true.
To work around that in the above example, use a slightly larger number as the last value:
$ seq -s' ' 0 .1 .31 0 0.1 0.2 0.3 |
In general, when using an increment with a fractional part, where (last - first) / increment is (mathematically) a whole number, specify a slightly larger (or smaller, if increment is negative) value for last to ensure that last is the final value printed by seq.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |