[Question]
  [
We had a [prime factorization challenge](https://codegolf.stackexchange.com/q/1979/42545) a while ago, but that challenge is nearly six years old and barely meets our current requirements, so I believe it's time for a new one.
## Challenge
Write a program or function that takes as input an integer greater than 1 and outputs or returns a list of its prime factors.
## Rules
* Input and output may be given by any standard method and in any standard format.
* Duplicate factors must be included in the output.
* The output may be in any order.
* The input will not be less than 2 or more than 231 - 1.
* Built-ins are allowed, but including a non-builtin solution is encouraged.
## Test cases
```
2 -> 2
3 -> 3
4 -> 2, 2
6 -> 2, 3
8 -> 2, 2, 2
12 -> 2, 2, 3
255 -> 3, 5, 17
256 -> 2, 2, 2, 2, 2, 2, 2, 2
1001 -> 7, 11, 13
223092870 -> 2, 3, 5, 7, 11, 13, 17, 19, 23
2147483646 -> 2, 3, 3, 7, 11, 31, 151, 331
2147483647 -> 2147483647
```
## Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so **the shortest code in bytes wins.**
  
[Answer]
# [Python 2](https://docs.python.org/2/), 55 bytes
```
f=lambda n,k=2:n/k*[0]and(f(n,k+1),[k]+f(n/k,k))[n%k<1]
```
[Try it online!](https://tio.run/nexus/python2#@59mm5OYm5SSqJCnk21rZJWnn60VbRCbmJeikaYBFNI21NSJzo7VBnL0s3WyNTWj81SzbQxj/xcUZeaVKKRpGJmaav4HAA "Python 2 – TIO Nexus")
[Answer]
# [Pyth](https://pyth.herokuapp.com/?code=P&input=2147483646&debug=0), 1 byte
```
P
```
I like Pyth's chances in this challenge.
[Answer]
## Python 2, 53 bytes
```
f=lambda n,i=2:n/i*[f]and[f(n,i+1),[i]+f(n/i)][n%i<1]
```
Tries each potential divisor `i` in turn. If `i` is a divisor, prepends it and restarts with `n/i`. Else, tries the next-highest divisor. Because divisors are checked in increasing order, only the prime ones are found.
As a program, for 55 bytes:
```
n=input();i=2
while~-n:
 if n%i:i+=1
 else:n/=i;print i
```
[Answer]
# Mathematica, ~~38~~ 30 bytes
Thanks @MartinEnder for 8 bytes!
```
Join@@Table@@@FactorInteger@#&
```
[Answer]
# [Haskell](https://www.haskell.org/), 48 bytes
```
(2%)
n%1=[]
n%m|mod m n<1=n:n%div m n|k<-n+1=k%m
```
[Try it online!](https://tio.run/##XY3BbsIwDIbvfgofqMREI9Vx2xREuPACHCdtHKpBt6pNmMa0C@zVlznVaNEkW/Zvf7/9Vp@7Y9@Hxj6HuU4ewCdkn/ZS3NWdDujQr8n6lU8O7VdU126t/IJsl7jg6tajxfeP1n/iDBukLCOAiwKNaoMaOBaGfFCpDMq/jqG6zeKY9KQYdFEMvhSLFMmILu/gfwHxZ1wbYUlS/Jqzpa5Mdns2HBr38abkUszCUm7yisu8nGAeYY6GIjZME2oGdFSgvsPPS9PXr@egHre73S8 "Haskell – Try It Online") Example usage: `(2%) 1001` yields `[7,11,13]`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 2 bytes
```
Æf
```
[Try it online!](https://tio.run/nexus/jelly#@3@4Le3/4fZHTWvc//830lEw1lEw0VEw01Gw0FEwBPKNTE1BBFDA0MDAEMg0MjawNLIwNwAyDU3MTSyMzUzMkNjmAA "Jelly – TIO Nexus")
[Answer]
# JavaScript (ES6), 44 bytes
```
f=(n,x=2)=>n-1?n%x?f(n,x+1):[x,...f(n/x)]:[]
```
Horribly inefficient due to the fact that it iterates from 2 up to every prime factor, including the last. You can cut the time complexity dramatically at the cost of 5 bytes:
```
f=(n,x=2)=>x*x>n?[n]:n%x?f(n,x+1):[x,...f(n/x,x)]
```
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), ~~37~~ 32 bytes
```
vs(...<..1I>(!@)s)%?w;O,s(No;^;<
```
[Try it online!](https://tio.run/nexus/cubix#@19WrKGnp2ejp2foaaeh6KBZrKlqX27tr1Os4ZdvHWdt8/@/kZGxgaWRhbkBAA "Cubix – TIO Nexus") or [Watch it in action](http://ethproductions.github.io/cubix/?code=dnMoLi4uPC4uMUk+KCFAKXMpJT93O08scyhObzteOzw=&input=MTI=&speed=4).
[Answer]
# [Haskell](https://www.haskell.org/), 47 bytes
Basically this checks all integers `2, 3, 4, 5, ...` for divisors of `n`. If we find one, we factor that out and repeat the process until we get to `1`.
```
f 1=[]
f n=(:)<*>f.div n$until((<1).mod n)(+1)2
```
[Try it online!](https://tio.run/##XY7BDoIwDIbvfYoeOIACoSswMMCLGA8kSFyEaRS9GJ8dNyJgTNas/9/v73aq7@dj141ji1TuD9CiLt2dV2yqNmzUE7Xz0IPqXLcgL@wvDWrP3ZInxr5WGku83pQe0MEWheAoF5mMAF4BCAwqFMD2Yogn5Rsj/XYM2exZm8SqGESSTDkfEx9JGp3@wH8HKIrIjqVhyZTJz1@ZH5sWLXO701RuwoalWMYZp3G6wrzAbAOJbZhWVE7ooiB4jx8 "Haskell – Try It Online")
[Answer]
# [Actually](https://github.com/Mego/Seriously), 6 bytes
```
w`in`M
```
[Try it online!](https://tio.run/nexus/actually#@1@ekJmX4Pv/vwUA "Actually – TIO Nexus")
Explanation:
```
w`in`M
w       factor into primes and exponents
 `in`M  repeat each prime # of times equal to exponent
```
[Answer]
# J, 2 bytes
```
q:
```
~~Body must be at least 30 characters.~~
[Answer]
# [MATL](https://github.com/lmendo/MATL), 2 bytes
```
Yf
```
[Try it online!](https://tio.run/nexus/matl#@x@Z9v@/kQkA "MATL – TIO Nexus")
Obligatory "boring built-in answer".
[Answer]
# Japt, 2 bytes
```
Uk
```
A built-in `k` used on the input `U`. Also refers to a country.
[Test it online!](http://ethproductions.github.io/japt/?v=master&code=VWs=&input=MTEwMTE=)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 1 byte
```
ǐ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLHkCIsIiIsIjIxNDc0ODM2NDYiXQ==)
Just the built-in. But that's boring, so
## [Vyxal](https://github.com/Vyxal/Vyxal), ~~16~~ 8 bytes
```
K~æ~Ǒ$ẋf
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCJLfsOmfseRJOG6i2YiLCIiLCIyMTQ3NDgzNjQ2Il0=)
## Explained
```
ʀ~æ~Ǒ$ẋf
ʀ~æ      # From the all numbers in the range [0, input], keep only those that are prime
   ~Ǒ    # Without popping anything, get the multiplicity of each prime divisor - this leaves the stack as [divisors, multiplicities]
     $ẋ  # Swap the two items and repeat each divisor multiplicity times
       f # Flatten and output that   
```
[Answer]
# [tone-deaf](https://github.com/kade-robertson/tone-deaf), 3 bytes
This language is quite young and not really ready for anything major yet, but it can do prime factorization:
```
A/D
```
This will wait for user input, and then output the list of prime factors.
[Answer]
# MATLAB, 6 bytes
I think this does not require any explanation.
```
factor
```
[Answer]
## Batch, 96 bytes
```
@set/an=%1,f=2,r=0
:l
@set/af+=!!r,r=n%%f
@if %r%==0 echo %f%&set/an/=f
@if %n% gtr 1 goto l
```
[Answer]
# [R](https://www.r-project.org/), ~~53~~ 52 bytes
```
p=function(n,i=2)if(n>1)c(i[m<-!n%%i],p(n/i^m,i+!m))
```
[Try it online!](https://tio.run/##dZHxCoIwEMb/7ykuIthoktvUGWQvEhVhEw5yytTnt1liOQnuYPvuft/dmO1ri6W@Ffe8rWyT9XVWdCZvsTLEMMwExYKYE6c5wXN5DNZmu8ULq4nZ47VkuFuXlM49iKCwgeAkVnNZfmTpydHYzcAHkqniM@mXWWJc/FZ9VMTxuAeDmAFXi3oyM/fCnxWG/NOunBd3uZgnZHgQqQq/j3kPnvqHHVwenPmC5ZGKUplEyS8sJ1gOBvFwkPwfqkZ0ukMGxR2fDTw6DW3logoeWtdgdd7Zxn38qn8B "R – Try It Online")
Recursive function: overflows the stack limit and errors on TIO for the last test case.
---
# [R](https://www.r-project.org/), ~~59~~ 58 bytes
```
function(n,i=2)while(n>1)if(n%%i)i=i+1 else{show(i);n=n/i}
```
[Try it online!](https://tio.run/##dY/taoMwFIb/9yoOlIJhlplEjWWktzJcG9dDs2QkEX@MXbvTVmyNDHLgfD3ve@L6b4df6r2pT8E6L/umNaeA1iQmRclId0GtEnOkBJvE7HZIUOILBaW9@vEX2yVI3ow0r/i7VEoYgS3sj2yzbPN7m0ftfNpOIQbKeRIz1YNZY5Q9T2OUFcV0RwpFClSs5uVCPHqxV5bR@7oYtOgQKz/GswOrRPb4zM143h9vGOIwiK9Ymou84mVePsN8hvkoUIwJp5vtP6yY2LkGCWerPBgbQDlnXQofbYAOtYZQXxXUoK35hDDIQW3O4O0t93vbhv4P "R – Try It Online")
Non-recursive function: does not error for large primes, but will still take a very long time to run (and so times-out without erroring on TIO for the last test case anyway).
[Answer]
# [CellTail](https://github.com/mousetail/CellTail/blob/main/README.md), 166 bytes
```
N,(u,u,_),N:1,(u,1),(0,2);(0,2),N,N:1,N,N;N,(u,v,0),N:1,(v,1),(u/v,2,(u/v)%2);N,(u,v,w),N:1,(u,v+1,u%(v+1)),N;a&(_,_,_),N,N:1,a,N;_,(v,1),_:1,v,N;N,u,N:1,(u,2,u%2),N;
```
[Try the demo](https://mousetail.github.io/CellTail/#ST1DIE47Ck89TjsKI009NDA7CgpOLCh1LHUsXyksTjoxLCh1LDEpLCgwLDIpOygwLDIpLE4sTjoxLE4sTjtOLCh1LHYsMCksTjoxLCh2LDEpLCh1L3YsMiwodS92KSUyKTtOLCh1LHYsdyksTjoxLCh1LHYrMSx1JSh2KzEpKSxOO2EmKF8sXyxfKSxOLE46MSxhLE47XywodiwxKSxfOjEsdixOO04sdSxOOjEsKHUsMix1JTIpLE47)
## Explanation:
Base case, when we reach the end switch to a recursion resistant tuple and pass a message right to break it. Then the message right sends a 1 to prevent recursion when the tuple has broken.
```
N,(u,u,_),N:1,(u,1),(0,2);(0,2),N,N:1,N,N;
```
We have found a factor, try again in the cell to the right with `(u/v)`
```
N,(u,v,0),N:1,(v,1),(u/v,2,(u/v)%2);
```
We have not found a factor, try another.
```
N,(u,v,w),N:1,(u,v+1,u%(v+1)),N;
```
Copy 3-tuples from left to current
```
a&(_,_,_),N,N:1,a,N;
```
Break recursion resistant tuples. Hopefully the next step the value from the right will throw a 1 to break the recursion.
```
_,(v,1),_:1,v,N;
```
Base case, start with trying 2 as a factor.
```
N,u,N:1,(u,2,u%2),N;
```
[Answer]
# [Bash](https://www.gnu.org/software/bash/) + coreutils, 19 bytes
```
factor|sed s/.*:.//
```
[Try it online!](https://tio.run/nexus/bash#@5@WmFySX1RTnJqiUKyvp2Wlp6///7@RqSkA "Bash – TIO Nexus")
[Answer]
## Pyke, 1 byte
```
P
```
[Try it here!](http://pyke.catbus.co.uk/?code=P&input=12)
Prime factors builtin.
[Answer]
# [Hexagony](https://github.com/m-ender/hexagony), 58 bytes
Not done golfing yet, but @MartinEnder should be able to destroy this anyway
Prints out factors space-separated, with a trailing space
Golfed:
```
2}\..}$?i6;>(<...=.\'/})."@...>%<..'':\}$"!>~\{=\)=\}&<.\\
```
Laid-out:
```
     2 } \ . .
    } $ ? i 6 ;
   > ( < . . . =
  . \ ' / } ) . "
 @ . . . > % < . .
  ' ' : \ } $ " !
   > ~ \ { = \ )
    = \ } & < .
     \ \ . . .
```
Explanation coming later.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 1 byte
```
Ò
```
[Try it online!](https://tio.run/nexus/05ab1e#@3940v//RoYm5iYWxmYmZgA "05AB1E – TIO Nexus")
[Answer]
# CJam, 2 bytes
```
mf
```
[cjam.aditsu.net/...](//cjam.aditsu.net#code=rimf%60&input=2147483646)
This is a function. Martin, it seems I was sleepy.
[Answer]
## C, 92 bytes
```
int p(int n){for(int i=2;i<n;i++)if(n%i==0)return printf("%d, ",i)+p(n/i);printf("%d\n",n);}
```
Ungolfed version:
```
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int prime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            printf("%d, ", i);
            return prime(number / i); //you can golf away a few bytes by returning the sum of your recursive function and the return of printf, which is an int
        }                             //this allow you to golf a few more bytes thanks to inline calls
    }
    printf("%d\n", number);
}
int main(int argc, char **argv) {
    prime(atoi(argv[1]));
}
```
[Answer]
# [Java (OpenJDK)](http://openjdk.java.net/), 259 bytes
```
import java.util.*;interface g{static void main(String[]z){int a=new Scanner(System.in).nextInt();int b=0;int[]l={};for(int i=2;i<=a;i++){for(;a%i<1;l[b-1]=i){l=Arrays.copyOf(l,b=l.length+1);a/=i;}}for(int i=0;i<b;i++)System.out.print(l[i]+(i<b-1?", ":""));}}
```
[Try it online!](https://tio.run/nexus/java-openjdk#RY5BT4QwEIX/SkNi0sputStZzZbGePTkgSPhMGDB2ZRCyuwqEn47gjHx9JL33nzzFmz7LhA7wxXkhdDJW42ebKihsqyZBgLCil07fGctoOcZBfRNXnyLae0xMN5@sqwC723g2TiQbSV6Ib39oldPXGw4Vpr7TfPCmWnWdRf45qI5aEwNaIxjMW2uhhtMlXZ5uVeFQTE58xICjIOsun58q7nblcZJZ31DH7ESGu4M6nn@J65/0vIX@Demu5Ds183EXY5FzNd4r56jHYtOUSTEerwsB5U8Jk8Px@T4Aw "Java (OpenJDK) – TIO Nexus")
[Answer]
# [PHP](https://php.net/), 51 bytes
```
for($i=2;1<$a=&$argn;)$a%$i?$i++:$a/=$i*print"$i ";
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwKWSWJSeZ6tkbGysZP0/Lb9IQyXT1sja0EYl0VYNLGetqZKoqpJpr5KprW2lkqhvq5KpVVCUmVeipJKpANTzHwA "PHP – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 51 bytes
```
i;f(n){for(i=2;i<=n;)n%i?i++:printf("%d ",i,n/=i);}
```
[Try it online!](https://tio.run/##XY7LCsIwEEXX5itKoDClLSZp@tBY/RapNMzCWGpdlX57TEwEcXXuXM4MM5R6GKxFNYLJ1vExA/ZC4ak3KjMpXjDPj9OMZhmBpreEFliYfY@Z2qwGVycmIyvZ/Sjl2VmuVmTnbzpMr@UJlLq4EeJ37lc08NnTILyhoQqQAU1AF8CjI@r6G6LAGeOxEhU7iK5lceSylV3VyOZvbv0X9g0 "C (gcc) – Try It Online")
[Answer]
# [Gol><>](https://github.com/Sp3000/Golfish), 19 bytes
```
IT1WP2K%ZB|:N,:M?t;
```
[Try it online!](https://tio.run/##XY1NCgIxDIX3PUU37io0Sf9mBhTciSguBMEDqAOCi3Hp3WtSnFaEhua9fC@5PR/XcbrnvD3B@Yi7xWXz7g@m369fQ0a9XGlUJB8pV5RhI3w7Umn2xAZsihR6X3JGe6Mhsg4/8N9TYC3IODILXJxHsh2maOdjZVGdy06ujsPMgosuUXChwVRhkoCXhqChsaBV5XrvAw "Gol><> – Try It Online")
## Explanation
```
IT                  < [n]     read input to n then mark cell (T)
  1WP     |         < [n m=2] from 2 onwards (m=1;while m:m++;)
     2K%ZB          < [n m+1] copy two elements, mod, break if n%m==0
           :N       < [n m] (< where n%m==0) print m with newline
             ,:M?t; < [n/m] (< new n)        divide, if not 1 redo from T else exit
```
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), 1 [byte](https://github.com/mudkip201/pyt/wiki/Codepage)
```
ḋ
```
Boring built-in answer
[Try it online!](https://tio.run/##K6gs@f//4Y7u//8NjY0B)
]