This site is supported by donations to The OEIS Foundation.

# Template:Prime factors (with multiplicity)

The {{prime factors (with multiplicity)}} (or {{mpf}} or {{factorization}}) arithmetic function template returns the prime factorization (prime factors (with multiplicity)) of a nonzero integer, otherwise returns an error message.

### Usage

{{prime factors (with multiplicity)|a nonzero integer|sep = map items separator (default &#32;*&#32;)|key/val_sep = key/value separator (default ^)}}

or

{{prime factors (with multiplicity)|a nonzero integer|map items separator (default &#32;*&#32;)|key/value separator (default ^)}}

or

{{mpf|a nonzero integer|sep = map items separator (default &#32;*&#32;)|key/val_sep = key/value separator (default ^)}}

or

{{mpf|a nonzero integer|map items separator (default &#32;*&#32;)|key/value separator (default ^)}}

where

• sep default &#32;*&#32; gives " * " (asterisk surrounded by spaces), and
• key/val_sep default is ^.

## Valid input

A nonzero integer less than 1031 2 = 1062961.

## Examples

To check the results, you may use

### Examples with valid input

Note that the prime factorization of integer units −1 and 1 gives the empty product of primes, hence the empty string is returned.

Code Result
{{factorization|210^2}} 2^2 * 3^2 * 5^2 * 7^2
{{mpf|210^2}} 2^2 * 3^2 * 5^2 * 7^2
{{mpf|210^2|sep = ;&#32;|key/val_sep = &#32;&rarr;&#32;}} 2 → 2; 3 → 2; 5 → 2; 7 → 2
{{mpf|210^2|sep = ,&#32;|key/val_sep = :&#32;}} 2: 2, 3: 2, 5: 2, 7: 2
{{mpf|210^2|sep = &#32;*&#32;|key/val_sep = ^}} 2^2 * 3^2 * 5^2 * 7^2
{{mpf|210^2|sep = &#32;+&#32;|key/val_sep = *}} 2*2 + 3*2 + 5*2 + 7*2
{{mpf|-28}} 2^2 * 7^1
{{mpf|-5}} 5^1
{{mpf|1}}
{{mpf|7}} 7^1
{{mpf|15}} 3^1 * 5^1
{{mpf|27}} 3^3
{{mpf|30}} 2^1 * 3^1 * 5^1
{{mpf|111}} 3^1 * 37^1
{{mpf|5^3 * 11^2}} 5^3 * 11^2
{{mpf|2^5 * 3^3 * 5}} 2^5 * 3^3 * 5^1
{{mpf|2^9 * 3^3}} 2^9 * 3^3
{{mpf|37^2 + 8 * 37^2}} 3^2 * 37^2
{{mpf|2^9 * (26 + 1)}} 2^9 * 3^3
{{mpf|89 * 113}} 89^1 * 113^1
{{mpf|79 * 79}} 79^2
{{mpf|210^2}} 2^2 * 3^2 * 5^2 * 7^2
{{mpf|233^2}} 233^2
{{mpf|10000}} 2^4 * 5^4
{{mpf|65535}} 3^1 * 5^1 * 17^1 * 257^1
{{mpf|65536}} 2^16
{{mpf|65537}} 65537^1
{{mpf|65539}} 65539^1
{{mpf|65540}} 2^2 * 5^1 * 29^1 * 113^1
{{mpf|65541}} 3^1 * 7^1 * 3121^1
{{mpf|65542}} 2^1 * 32771^1
{{mpf|65543}} 65543^1
{{mpf|65547}} 3^2 * 7283^1
{{mpf|65549}} 11^1 * 59^1 * 101^1
{{mpf|65551}} 65551^1
{{mpf|65553}} 3^1 * 21851^1
{{mpf|65557}} 65557^1
{{mpf|65559}} 3^1 * 13^1 * 41^2
{{mpf|65561}} 53^1 * 1237^1
{{mpf|65563}} 65563^1
{{mpf|65567}} 173^1 * 379^1
{{mpf|65569}} 7^1 * 17^1 * 19^1 * 29^1
{{mpf|65571}} 3^1 * 11^1 * 1987^1
{{mpf|65573}} 23^1 * 2851^1
{{mpf|65577}} 3^1 * 21859^1
{{mpf|65579}} 65579^1
{{mpf|265535}} 5^1 * 23^1 * 2309^1
{{mpf|265536}} 2^6 * 3^2 * 461^1
{{mpf|265537}} 131^1 * 2027^1
{{mpf|265539}} 3^1 * 88513^1
{{mpf|265540}} 2^2 * 5^1 * 11^1 * 17^1 * 71^1
{{mpf|265541}} 265541^1
{{mpf|265542}} 2^1 * 3^1 * 44257^1
{{mpf|265543}} 265543^1
{{mpf|265547}} 265547^1
{{mpf|265549}} 37^1 * 7177^1
{{mpf|265551}} 3^1 * 11^1 * 13^1 * 619^1
{{mpf|265553}} 29^1 * 9157^1
{{mpf|265557}} 3^1 * 17^1 * 41^1 * 127^1
{{mpf|265559}} 7^1 * 59^1 * 643^1
{{mpf|265561}} 265561^1
{{mpf|265563}} 3^2 * 19^1 * 1553^1
{{mpf|265567}} 265567^1
{{mpf|265569}} 3^1 * 88523^1
{{mpf|265571}} 265571^1
{{mpf|265573}} 7^1 * 11^1 * 3449^1
{{mpf|265577}} 13^1 * 31^1 * 659^1
{{mpf|265579}} 265579^1
{{mpf|257}} 257^1
{{mpf|97 * 211}} 97^1 * 211^1
{{mpf|216 * 211}} 2^3 * 3^3 * 211^1
{{mpf|1024 * 45}} 2^10 * 3^2 * 5^1
{{mpf|97 * 257}} 97^1 * 257^1
{{mpf|3^6 * 5^2}} 3^6 * 5^2
{{mpf|3 * 5^5}} 3^1 * 5^5
{{mpf|17^2 * 191}} 17^2 * 191^1
{{mpf|5 * 7 * 13 * 29}} 5^1 * 7^1 * 13^1 * 29^1
{{mpf|509^2}} 509^2
{{mpf|965535}} 3^1 * 5^1 * 59^1 * 1091^1
{{mpf|965536}} 2^5 * 11^1 * 13^1 * 211^1
{{mpf|965537}} 67^1 * 14411^1
{{mpf|965539}} 83^1 * 11633^1
{{mpf|965540}} 2^2 * 5^1 * 23^1 * 2099^1
{{mpf|965541}} 3^1 * 321847^1
{{mpf|965542}} 2^1 * 19^1 * 25409^1
{{mpf|965543}} 383^1 * 2521^1
{{mpf|965547}} 3^3 * 11^1 * 3251^1
{{mpf|965549}} 13^1 * 17^2 * 257^1
{{mpf|965551}} 965551^1
{{mpf|965553}} 3^1 * 321851^1
{{mpf|965557}} 31^1 * 31147^1
{{mpf|965559}} 3^1 * 7^1 * 45979^1
{{mpf|965561}} 19^1 * 89^1 * 571^1
{{mpf|965563}} 23^1 * 41981^1
{{mpf|965567}} 965567^1
{{mpf|965569}} 11^1 * 61^1 * 1439^1
{{mpf|965571}} 3^1 * 73^1 * 4409^1
{{mpf|965573}} 7^1 * 271^1 * 509^1
{{mpf|965577}} 3^1 * 367^1 * 877^1
{{mpf|965579}} 193^1 * 5003^1
{{mpf|997 * 1019}} 997^1 * 1019^1
{{mpf|1015943}} 997^1 * 1019^1

### Examples with invalid input

{{mpf|0}} Prime factors (with multiplicity) error: Argument must be a nonzero integer (undefined prime factorization)
{{mpf|1031^2}} Prime factors (with multiplicity) error: Argument must be a nonzero integer with absolute value < 1031 2 = 1062961

## Formatted numbers

This template requires unformatted numbers, it will not recognize formatted numbers, e.g. comma separated, which is by design since formatted numbers will break expression parsers. To remove the formatting from a number, you can wrap the number first in {{formatnum:number|R}}.[1]

code result
{{prime factors (with multiplicity)|1,000}} Prime factors (with multiplicity) error: Argument must be a nonzero integer
{{prime factors (with multiplicity)|{{formatnum:1,000|R}}}} 2^3 * 5^3

## Code

### {{Prime factors (with multiplicity)}} (or {{mpf}} or {{factorization}}) arithmetic function template

```
{{ifint| {{{1|NAN}}}
| {{#ifexpr: ( ( abs ({{{1}}}) ) > 1 ) and ( ( abs ({{{1}}}) ) < 1031^2 )
| {{~mpf
| {{{1}}}
| mpf_le_sqrt(n) = {{mpf le sqrt(n)
| {{{1}}}
| sep = *
| key/val_sep = ^
}}
| sep = {{{sep|{{{2| * }}}}}}
| key/val_sep = {{{key/val_sep|{{{3|^}}}}}}
}}
| {{#ifexpr: ({{{1}}}) = 0
| {{error| Prime factors (with multiplicity) error: Argument must be a nonzero integer }}
}}<!--
-->{{#ifexpr: abs ({{{1}}}) >= 1031^2
| {{error| Prime factors (with multiplicity) error: Argument must be a nonzero integer with absolute value < 1031{{^|2}} {{=}} {{#expr: 1031^2 }} }}
}}
}}
| {{error| Prime factors (with multiplicity) error: Argument must be a nonzero integer }}
}}

```

### {{~Prime factors (with multiplicity)}} (or {{~mpf}}) core function template

```
{{#replace:{{#replace:{{{mpf_le_sqrt(n)}}}|*|{{{sep| * }}}}}|^|{{{key/val_sep|^}}}}}<!--
-->{{#if: {{{mpf_le_sqrt(n)}}}
| {{#ifexpr: ( ( abs ({{{1}}}) ) / ( {{{mpf_le_sqrt(n)}}} ) ) > 1
| {{{sep| * }}}{{#expr: ( abs ({{{1}}}) ) / ( {{{mpf_le_sqrt(n)}}} ) }}{{{key/val_sep|^}}}1
| <!-- no prime factor greater than sqrt(n) needs to be added -->
}}
| {{#ifexpr: ( abs ({{{1}}}) ) > 1
| {{#expr: abs ({{{1}}}) }}{{{key/val_sep|^}}}1 <!-- A prime number! -->
| <!-- empty list: empty product of primes for the units (+/-)1, undefined prime factorization for 0 -->
}}
}}

```