[Question]
  [
In this task, you have to write a program, that computes the prime factors of a number. The input is a natural number 1 < n < 2^32. The output is a list of the prime factors of the number in the following format. Exponents must be omitted if they are 1. Only output prime numbers. (Assuming the input is 131784):
> 
> 131784 = 2^3 \* 3 \* 17^2 \* 19
> 
> 
> 
Using the same amount of whitespace is not required; whitespace may be inserted wherever appropriate. Your program should complete in less then 10 minutes for any input. The program with the shortest amount of characters wins.
  
[Answer]
# SageMath, 31 Bytes
```
N=input()
print N,"=",factor(N)
```
Test case:
`83891573479027823458394579234582347590825792034579235923475902312344444`
Outputs:
`83891573479027823458394579234582347590825792034579235923475902312344444 = 2^2 * 3^2 * 89395597 * 98966790508447596609239 * 263396636003096040031295425789508274613`
[Answer]
## Ruby 1.9, ~~74~~ 70 characters
```
#!ruby -plrmathn
$_+=?=+$_.to_i.prime_division.map{|a|a[0,a[1]]*?^}*?*
```
Edits:
* (74 -> 70) Just use the exponent as slice length instead of explicitly checking for `exponent > 1`
[Answer]
## Perl 5.10, 73 ~~88~~
```
perl -pe '$_=`factor $_`;s%( \d+)\K\1+%-1-length($&)/length$1%ge;y, -,*^,;s;\D+;=;'
```
Takes input number from standard input. Will compute factors for multiple inputs if provided.
Counted as a difference to `perl -e`. 5.10 is needed for the `\K` regex metacharacter.
[Answer]
# OCaml, 201 characters
A direct imperative translation of the best Python code:
```
let(%)s d=if!d>1then Printf.printf"%s%d"s!d
let f n=let x,d,e,s=ref n,ref 1,ref 0,ref"="in""%x;while!d<65536do
incr d;e:=0;while!x mod!d=0do x:=!x/ !d;incr e
done;if!e>0then(!s%d;"^"%e;s:="*")done;!s%x
```
For example,
```
# f 4294967292;;
4294967292=2^2*3^2*7*11*31*151*331- : unit = ()
```
(note that I've omitted outputting the final endline.) Just for fun, at 213 characters, a purely functional version, thoroughly obfuscated through liberal use of operators:
```
let(%)s d=if d>1then Printf.printf"%s%d"s d
let f x=let s=ref"="in""%x;let rec(@)x d=if d=65536then!s%x else
let rec(^)x e=if x/d*d<x then x,e else x/d^e+1in
let x,e=x^0in if e>0then(!s%d;"^"%e;s:="*");x@d+1in x@2
```
[Answer]
## Python, 140 135 133 chars
```
M=N=input()
s=''
f=1
while f<4**8:
 f+=1;e=0
 while N%f<1:e+=1;N/=f
 if e:s+='*%d'%f+'^%d'%e*(e>1)
print M,'=',(s+'*%d'%N*(N>1))[1:]
```
[Answer]
## J, 72
```
(":*/f),'=',([,'*',])/(":"0~.f),.(('^',":)`(''"0)@.(=&1))"0+/(=/~.)f=.q:161784
```
Typical J. Two characters to do most of the work, sixty characters to present it.
**Edit:** Fixed the character count.
[Answer]
## J, ~~53~~ 52 characters
This solution takes the `rplc` trick from the solution of [randomra](https://codegolf.stackexchange.com/a/45499/134) but comes up with some original ideas, too.
```
":,'=',(":@{.,'^','*',~":@#)/.~@q:}:@rplc'^1*';'*'"_
```
In non-tacit notation, this function becomes
```
f =: 3 : 0
(": y) , '=' , }: (g/.~ q: y) rplc '^1*' ; '*'
)
```
where `g` is defined as
```
g =: 3 : 0
": {. y) , '^' , (": # y) , '*'
)
```
* `q: y` is the vector of *prime factors* of `y`. For instance, `q: 60` yields `2 2 3 5`.
* `x u/. y` applies `u` to `y` *keyed* by `x`, that is, `u` is applied to vectors of elements of `y` for which the entries in `x` are equal. This is a bit complex to explain, but in the special case `y u/. y` or `u/.~ y`, `u` is applied to each vector of distinct elements in `y`, where each element is repeated for as often as it appears in `y`. For instance, `</.~ 1 2 1 2 3 1 2 2 3` yields
```
┌─────┬───────┬───┐
│1 1 1│2 2 2 2│3 3│
└─────┴───────┴───┘
```
* `# y` is the *tally* of `y`, that is, the number of items in`y`.
* `": y` *formats* `y` as a string.
* `x , y` *appends* `x` and `y`.
* `{. y` is the *head* `y`, that is, its first item.
* Thus, `(": {. y), '^' , (": # y) , '*'` formats a vector of *n* repetitions of a number *k* into a string of the form *k* ^ *n* \*. This phrase in tacit notation is `:@{.,'^','*',~":@#`, which we pass to the adverb `/.` described further above.
* `x rplc y` is the library function *replace characters.* `y` has the form `a ; b` and every instance of string `a` in `x` is replaced by `b`. `x` is ravelled (that is, reshaped such that it has rank 1) before operation takes place, which is used here. This code replaces `^1*` with `*` as to comply with the mandated output format.
* `}: y` is the *curtail* of `y`, that is, all but its last item. This is used to remove the trailing `*`.
[Answer]
# PHP, 112
```
echo$n=$_GET[0],'=';$c=0;for($i=2;;){if($n%$i<1){$c++;$n/=$i;}else{if($c){echo"$i^$c*";}$c=0;if(++$i>$n)break;}}
```
118
```
echo $n=$_GET[0],'=';for($i=2;;){if(!($n%$i)){++$a[$i];$n/=$i;}else{if($a[$i])echo "$i^$a[$i]*";$i++;if($i>$n)break;}}
```
[Answer]
**Python 119 Chars**
```
M=N=input()
i=1
s=""
while N>1:
 i+=1;c=0
 while N%i<1:c+=1;N/=i
 if c:s+=" * %d"%i+['','^%d'%c][c>1]
print M,'=',s[3:]
```
[Answer]
### JavaScript, ~~124~~ ~~122~~ 119
```
for(s='',i=2,o=p=prompt();i<o;i++){for(n=0;!(p%i);n++)p/=i;n?s+=i+(n-1?'^'+n:'')+'*':0}alert(s.substring(0,s.length-1))
```
[Answer]
# Perl, 78
```
use ntheory":all";say join" * ",map{(join"^",@$_)=~s/\^1$//r}factor_exp(shift)
```
It uses the s///r feature of Perl 5.14 to elide the ^1s. 81 characters to run in a loop:
```
perl -Mntheory=:all -nE 'chomp;say join" * ",map{(join"^",@$_)=~s/\^1$//r}factor_exp($_);'
```
[Answer]
## PHP, 236 characters
```
$f[$n=$c=$argv[1]]++;echo"$n=";while($c){$c=0;foreach($f as$k=>$n)for($r=~~($k/2);$r>1;$r--){if($k%$r==0){unset($f[$k]);$f[$r]++;$f[$k/$r]++;$c=1;break;}}}foreach($f as$k=>$n)if(--$n)$f[$k]="$k^".++$n;else$f[$k]=$k;echo implode("*",$f);
```
Output for 131784: [2^3\*3\*17^2\*19](http://codepad.viper-7.com/L7N3iK)
Completes all numbers within a few seconds while testing.
```
4294967296=2^32
Time: 0.000168
```
Input was never specified, so I chose to call it using command line arguments.
```
php factorize.php 4294967296
```
[Answer]
### Scala 374:
```
def f(i:Int,c:Int=2):List[Int]=if(i==c)List(i)else 
if(i%c==0)c::f(i/c,c)else f(i,c+1)
val r=f(readInt)
class A(val v:Int,val c:Int,val l:List[(Int,Int)])
def g(a:A,i:Int)=if(a.v==i)new A(a.v,a.c+1,a.l)else new A(i,1,(a.v,a.c)::a.l)
val a=(new A(r.head,1,Nil:List[(Int,Int)])/:(r.tail:+0))((a,i)=>g(a,i))
a.l.map(p=>if(p._2==1)p._1 else p._1+"^"+p._2).mkString("", "*", "")
```
ungolfed:
```
def factorize (i: Int, c: Int = 2) : List [Int] = {
  if (i == c) List (i) else 
    if (i % c == 0) c :: f (i/c, c) else 
      f (i, c+1)
}
val r = factorize (readInt)
class A (val value: Int, val count: Int, val list: List [(Int, Int)])
def g (a: A, i: Int) = 
  if (a.value == i) 
    new A (a.value, a.count + 1, a.list) else 
    new A (i, 1, (a.value, a.count) :: a.list)
val a = (new A (r.head, 1, Nil: List[(Int,Int)]) /: (r.tail :+ 0)) ((a, i) => g (a, i))
a.l.map (p => if (p._2 == 1) p._1 else
  p._1 + "^" + p._2).mkString ("", "*", "")
```
[Answer]
# J, 74 chars
```
f=.3 :0
(":y),'=',' '-.~('^1 ';'')rplc~}:,,&' *'"1(,'^'&,)&":/"{|:__ q:y
)
   f 131784
131784=2^3*3*17^2*19
```
64 chars with input in variable `x`:
```
   x=.131784
   (":x),'=',' '-.~('^1 ';'')rplc~}:,,&' *'"1(,'^'&,)&":/"{|:__ q:x
131784=2^3*3*17^2*19
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~28~~ ~~27~~ 26 bytes
-1 byte thanks to Shaggy
```
+'=+Uk ü ®ÊÉ?ZÌ+'^+Zl:ZÃq*
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Kyc9K1VrIPwgrsrJP1rMKydeK1psOlrDcSo&input=MTMxNzg0IA)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jellylanguage/wiki), 16 [bytes](https://github.com/DennisMitchell/jellylanguage/wiki/Code-page)
```
³”=³ÆFḟ€1j€”^j”*
```
One of my first Jelly answers, so can definitely be golfed (especially `³”=³`)..
[Try it online.](https://tio.run/##y0rNyan8///Q5kcNc20PbT7c5vZwx/xHTWsMs4AEUCwuC0ho/f//39DY0NzCBAA)
**Explanation:**
```
³                 # Push the first argument
 ”=               # Push string "="
   ³ÆF            # Get the prime factor-exponent pairs of the first argument
      ḟ€1         # Remove all 1s from each pair
         j€”^     # Join each pair by "^"
             j”*  # Join the pair of strings by "*"
                  # (implicitly join the entire 'stack' together)
                  # (which is output implicitly as result)
```
[Answer]
# Java 10, ~~109~~ 108 bytes (lambda function)
```
n->{var r=n+"=";for(int i=1,f;i++<n;r+=f<1?"":(f<2?i:i+"^"+f)+(n>1?"*":""))for(f=0;n%i<1;n/=i)f++;return r;}
```
[Try it online.](https://tio.run/##hVDbasMwDH3fVwjDwJ7XbLk0XeK4/YGtL3scG3huPNylSnGcwCj59sxd8zoKEkJHR@Ic7dWgFvvd96Qb1XXwoiyebgAs@toZpWvYnluAV@8sfoGmTRsKMhHQMWSIzitvNWwBQcKEi/VpUA6cRE4kEaZ1NFwDK@N7IyznFQrHpaniDSElNVWysaXl5INwwzjFdcDvSEkIY@dVIx8F3toqFvggLTOcC1f73iE4MU7iouDYfzZBwSxkaO0ODsEIvYh@ewfFZhc/na8PUdv76BhGvkGKkaZpmrI/R/8y8uUyza9w4jRePWVXSFlSZEW@Sorkmc1PHKdf)
# Java 6+, 181 bytes (full program)
```
class M{public static void main(String[]a){long n=new Long(a[0]),i=1,f;String r=n+"=";for(;i++<n;r+=f<1?"":(f<2?i:i+"^"+f)+(n>1?"*":""))for(f=0;n%i<1;n/=i)f++;System.out.print(r);}}
```
[Try it online.](https://tio.run/##JY3bCsIwEER/JSwIiestKihNoz@gTz6KQrykbK1bSaMi4rfHik8zHOYwpXu4fnm6pHSsXNOI9ft2P1R0FE10sY1HTSdxdcRyEwNxsd059a5qLgRbPj/Fqq3SbUc71SOre978ZyJYRrBgfB2kIcScTUDrc70EyKTPx0vKCGEP6BVKXrS8CxmAUj/D25HhDuXa8NCS8ohm82ri@Tqo73Fwax@iDMp8PiklPdGz@fQL)
-1 byte thanks to *@ceilingcat*.
**Explanation:**
```
n->{                // Method with integer parameter and String return-type
  var r=n+"=";      //  Result-String, starting at the input with an appended "="
  for(int i=1,f;i++<n;
                    //  Loop in the range [2, n]
      r+=           //    After every iteration: append the following to the result-String:
        f<1?        //     If the factor `f` is 0:
         ""         //      Append nothing
        :           //     Else:
         (f<2?      //      If the factor `f` is 1:
           i        //       Append the current prime `i`
          :         //      Else:
           i+"^"+f) //       Append the current prime `i` with it's factor `f`
         +(n>1?     //      And if we're not done yet:
            "*"     //       Also append a "*"
           :        //      Else:
            ""))    //       Append nothing more
    for(f=0;        //   Reset the factor `f` to 0
        n%i<1;      //   Loop as long as `n` is divisible by `i`
      n/=i)         //    Divide `n` by `i`
      f++;          //    Increase the factor `f` by 1
  return r;}        //  Return the result-String
```
[Answer]
# Powershell, ~~113~~ 97 bytes
Inspired by [Joey's](https://codegolf.stackexchange.com/users/15/joey) [answer](https://codegolf.stackexchange.com/a/680/80745). It's a slow but short.
```
param($x)(2..$x|%{for(;!($x%$_)){$_
$x/=$_}}|group|%{$_.Name+"^"+$_.Count-replace'\^1$'})-join'*'
```
Explained test script:
```
$f = {
param($x)               # let $x stores a input number > 0
(2..$x|%{               # loop from 2 to initial input number
    for(;!($x%$_)){     # loop while remainder is 0
        $_              # push a current value to a pipe
        $x/=$_          # let $x is $x/$_ (new $x uses in for condition only)
    }
}|group|%{              # group all values
    $_.Name+"^"+$_.Count-replace'\^1$'  # format and remove last ^1
})-join'*'              # make string with *
}
&$f 2
&$f 126
&$f 129
&$f 86240
#&$f 7775460
```
Output:
```
2
2*3^2*7
3*43
2^5*5*7^2*11
```
[Answer]
# APL(NARS), 66 chars, 132 bytes
```
{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
```
test and comment:
```
  f←{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
  f 131784
131784=2^3 * 3 * 17^2 * 19
  f 2
2=2
  f (2*32)
4294967296=2^32
{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
k←π⍵      find the factors with repetition of ⍵ and assign that array to k example for 12 k is 2 2 3
v←∪       gets from k unique elements and put them in array v
+/¨{k=⍵}¨ for each element of v count how many time it appear in k (it is array exponents)
v,¨       make array of couples from element of v (factors unique) and the array above (exponents unique)
∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨ pretty print the array of couples factor exponent as array chars
3↓                                 but not the first 3 chars
(⍕⍵),'='  but print first the argument and '=' in char format
```
if someone has many time with these primitives, know them very well them, for me it is possible that
the code is clearer of comments... so code more clear than comments, comments unuseful...
[Answer]
# [QBasic](https://en.wikipedia.org/wiki/QBasic), 156 characters
```
INPUT n#
?n#;"=";
f=2
1c=0
WHILE n#/f=INT(n#/f)
n#=n#/f
c=c+1
WEND
IF c THEN?f;
IF c>1THEN?"^";c;
IF c*(n#>1)THEN?"*";
f=f+1
IF f*f<=n#GOTO 1
IF n#>1THEN?n#
```
When run at [Archive.org](https://archive.org/details/msdos_qbasic_megapack), this takes about 2½ minutes to finish for an input of 4294967291, the largest prime in the specified range.
### Ungolfed, with some remarks
This is the version I used to get the timing information:
```
CLS
INPUT num#
PRINT num#; "=";
factor = 2
startTime# = TIMER
DO
    count = 0
    WHILE num# / factor = INT(num# / factor)
        num# = num# / factor
        count = count + 1
    WEND
    IF count > 0 THEN PRINT factor;
    IF count > 1 THEN PRINT "^"; count;
    IF count > 0 AND num# > 1 THEN PRINT "*";
    factor = factor + 1
LOOP WHILE factor * factor <= num#
IF n# > 1 THEN PRINT n# ELSE PRINT
time# = TIMER - startTime#
PRINT "Search took"; INT(time# / 60); "minutes and";
PRINT INT(time# mod 60); "seconds"
```
The program uses the standard approach of trial division of each factor between \$2\$ and \$\sqrt n\$. It prints a `*` after each successful factor if the remaining \$n\$ is still greater than 1. If the factor becomes greater than \$\sqrt n\$ while \$n > 1\$, then \$n\$ is prime and we output it after exiting the loop.
A few extra bytes had to be spent to cope with very large numbers:
QBasic's numbers are either single- or double-precision floating point under the hood, although it casts to integers for certain calculations. Integers greater than \$2^{24}\$ are not guaranteed to be accurately represented in single-precision floating point, so we have to use double-precision for \$n\$ (thus the double-precision sigil on the variable `num#`). However, since we're only checking factors up to \$\sqrt n < 2^{16}\$, we can use the default single precision for `factor`.
At first, I had `WHILE num# MOD factor = 0` for the inner loop. This worked until I tried numbers close to \$2^{32}\$, at which point it said "Overflow." It seems that the `MOD` operator casts its operands to integers--specifically, 32-bit *signed* integers. A value of \$2^{32}-1\$ can fit in a 32-bit *unsigned* integer, but not a signed one. So `MOD` wasn't going to work. Neither was my usual golf trick of checking divisibility with `a/b=a\b`, since the int-div operator `\` also casts to integers. Fortunately, I could still use floating point division and then truncate explicitly with the `INT` function (which seemingly does *not* cast to integer--??).
Given the issues with single- vs. double-precision, I assumed that squaring `factor` (single-precision) before comparing it with `num#` (double) wasn't going to work. But surprisingly, it did. My best guess after some experimentation is that if there's a double-precision value anywhere in an expression, QBasic casts *all* of the values to double before computing anything. This contrasts with C, [for example](https://tio.run/##jc@9CsIwEAfwPU9xKIW0amk3tX4svoGrS5ukbSAmmiYQEJ89phHRRXH5H8f9Du7IoiPE@ymXRFjKYDMYylXe7xCXBs41lziFG4JWqNqAgy0sy1WZFxVCcNHBtHiSUBiuttYs1F5ZQaFhkNCTnMyjfmY2Rlq9145cdoKFlhE@cCXXkLRxx2Xu0x2Ubb44TOMs/du7WZEX6S@9f2k8nhH5@KtmxmoJ4e@79w8), where implicit casting only occurs between the operands of a single operator.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~22~~ 20 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ÐfsÓ0Køε1K'^ý}'*ý'=ý
```
-2 bytes thanks to *@Emigna*.
[Try it online.](https://tio.run/##ASsA1P9vc2FiaWX//8OQZnPDkzBLw7jOtTFLJ17DvX0nKsO9Jz3Dvf//MTMxNzg0)
**Explanation:**
```
Ð                # Triplicate the (implicit) input-integer
 f               # Pop and push all prime factors (without counting duplicates)
  s              # Swap to take the input again
   Ó             # Get all prime exponents
    0K           # Remove all 0s from the exponents list
      ø          # Zip it with the prime factors, creating pairs
       ε         # Map each pair to:
        1K       #  Remove all 1s from the pair
        '^ý     '#  And then join by "^"
       }'*ý     '# After the map: join the string/integers by "*"
           '=ý  '# And join the stack by "=" (with the input we triplicated at the start)
                 # (after which the result is output implicitly)
```
[Answer]
# JavaScript, 107
```
n=prompt()
s=n+'='
c=0
for(i=2;;){if(n%i<1){c++
n/=i}else{if(c)s+=i+'^'+c+'*'
c=0
if(++i>n)break}}
alert(s)
```
120
```
n=prompt()
o={2:0}
for(i=2,c=n;i<=c;)!(c%i)?++o[i]?c/=i:0:o[++i]=0
s=n+'='
for(i in o)s+=o[i]?i+'^'+o[i]+'*':''
alert(s)
```
[Answer]
# [PHP](https://php.net/), 112 bytes
```
<?=$a=$argn,"=";for($i=2;1<$a;)$a%$i?$i++:$a/=$i+!++$r[$i];foreach($r as$k=>$v)echo$t?"*":!++$t,$v<2?$k:"$k^$v";
```
[Try it online!](https://tio.run/##HcrRCoIwFIfxe5@iDv9AmxC5O7fjHiQKDmJtCDqW7PWXdvfx44s@FuuijxUkfRYmrTWZyg27MoT/2hKTea@pRuDO3C3ENJALgkNQqofceI@zUkgPhOexTjL6GukkX8w8IDfT6Fdsjq7UH@PWItvOYe4J8wuZTCk/ "PHP – Try It Online")
[Answer]
# PHP, 93 bytes
```
<?=$n=$argn;for($i=2;$n>1;$k&&$p=print($p?"*":"=")."$i^$k",$i++)for($k=0;$n%$i<1;$n/=$i)$k++;
```
I could do 89 bytes with PHP 5.5 (or later), but that postdates the challenge by more than 2 years:
```
<?=$n=$argn;for($i=2;$n>1;$k&&$p=print"=*"[$p]."$i^$k",$i++)for($k=0;$n%$i<1;$n/=$i)$k++;
```
Run as pipe with `-nF` or [try them online](http://sandbox.onlinephpfunctions.com/code/853b0331a800861d752594542295f8fbed2d82a0).
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 24 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
+'=+Uk ü ËÎ+DÅÊÎç^+DÊÃq*
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Kyc9K1VrIPwgy84rRMXKzudeK0TKw3Eq&input=MTMxNzg0)
[Answer]
# [Go](https://go.dev), 199 bytes
```
type M=map[int]int
func f(n int)M{m:=make(M)
for x,i:=n,2;i<=n;i++{if func(p int)int{if p<4{return p}else{for i:=2;i*i<=p;i+=2{if p%i==0{return-1}}}
return p}(i)>0{for x%i==0{m[i]++;x/=i}}}
return m}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=TY9RaoQwEIbfc4qwUEiq26oVlHXTGwh9X_ZBlmQZdhODG8FFPElfpNBL9AI9Q2_TMUrrQ5jkm_8jM-8f53r8stXpUp0l1RUYAtrWjaNPG6Xd5rN1apv_fLu7lbQUurIHMO6Ih6jWnKhihuKDl73eYfciWcmJqhvahbATJkwK2AtTQBD0oOikMOsFPBOx-7RvpGsbQ-0grzfZTzKqKD6ialEViU8-gBDREt7GwzCQP5EBf4282c0pfYBjEBTds4BVUA_LOp2ffdqWcdqTtwanuRoWv8RZnoa41Hzj_L8VeRytUeZRtkLJRJI1iNLcM6yIl__Hca6_)
Returns a map where the keys are the bases, and the values are exponents. So `131784 => map[2:3 3:1 17:2 19:1]`
]