0
\$\begingroup\$

Although there was a prime factors challenge posted ten years ago, it has tedious I/O and restricted time. In this challenge, your task is to write a program or function which takes an integer \$n \ge 2\$ as input, and returns its prime factors.

Task:

Input will consist of an integer, \$n \ge 2\$. Your program should theoretically work for arbitrarily large numbers, but can fail due to limits like integer overflow.

Your program should print/return the prime factors (in any order), in any reasonable representation. For example, 180 could be represented as [2, 2, 3, 3, 5], or "2 3 5 2 3", or through any other collection type or string representation.

Test cases:

2       [2]
3       [3]
6       [2, 3]
12      [2, 2, 3]
25      [5, 5]
180     [2, 2, 3, 3, 5]
181     [181]

Other:

Any trivial built-in answers should go in this community wiki.

This is , so shortest answer (in bytes) per language wins.

\$\endgroup\$
0
3
\$\begingroup\$

Javascript, 43 bytes

f=(n,i=2)=>n%i?n-1?f(n,i+1):"":i+" "+f(n/i)

Outputs a space separated list with a trailing space. It dies pretty quickly due to recursion limits, partially because it restarts at 2 every time it finds a new prime.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 16 bytes

ʀǎ:£$vǑ¥Zƛ÷w$ẋ;f

Try it Online at your own risk!

It is horrendously slow for large numbers, but it works

Explained

ʀǎ:£$vǑ¥Zƛ÷w$ẋ;f
ʀǎ                  # nth prime for each item in the range [0 ... input - 1]
  :£                # put that into the register while leaving it on the stack
    $vǑ             # get the divisbilty of each prime into the input
       ¥Z           # and zip that with the register
        ƛ     ;     # for each item in that zipped list: (each item is: [divisbilty, prime factor])
         ÷w         #      push divisbilty, [prime_factor]
           $ẋ       #      and repeat [prime_factor] divisibility times
               f    # flatten the unholy mess that results from doing the above
\$\endgroup\$
1
\$\begingroup\$

Trivial Answers (Community Wiki)

Edit: This challenge is closed. Please do not add solutions here; instead, consider creating a CW for trivial solutions on the dupe target, or just post your answer there as there are already many trivial solutions.

Vyxal, 1 byte

ǐ

Try it Online!

Jelly, 2 bytes

Æf

Try it online!

Factor + math.primes.factors, 7 bytes

factors

Try it online!

05AB1E, 1 byte

Ò

Try it online!

J, 2 bytes

q:

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Matlab, 17 bytes

factor(input(''))
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.