27
\$\begingroup\$

We had a prime factorization challenge 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 , so the shortest code in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ Would've been much better if you disallowed built-ins. \$\endgroup\$ – Buffer Over Read Dec 27 '16 at 0:25
  • 2
    \$\begingroup\$ @TheBitByte Challenges that disallow built-ins are generally looked down upon as Do X without Y challenges, especially since it's sometimes hard to tell whether a solution is technically a built-in. \$\endgroup\$ – ETHproductions Dec 27 '16 at 0:29
  • 1
    \$\begingroup\$ Well then, enjoy the influx of <5 byte solutions! As I write this, Pyth already does it in 1 byte. \$\endgroup\$ – Buffer Over Read Dec 27 '16 at 0:30
  • 2
    \$\begingroup\$ @TheBitByte Think of it as a language-by-language challenge, primarily. Try to beat Python's solution, or some other language without a builtin. \$\endgroup\$ – isaacg Dec 27 '16 at 2:30
  • 1
    \$\begingroup\$ @isaacg Well, language-by-language is a better way of looking at it, I agree. \$\endgroup\$ – Buffer Over Read Dec 27 '16 at 2:47

33 Answers 33

0
\$\begingroup\$

Gol><>, 19 bytes

IT1WP2K%ZB|:N,:M?t;

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
\$\endgroup\$
0
\$\begingroup\$

Pyt, 1 byte

Boring built-in answer

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Unfortunately, the non-built-in solution doesn't work on any number containing a duplicate factor (e.g. 8) \$\endgroup\$ – ETHproductions Feb 23 '18 at 5:13
  • \$\begingroup\$ Yep, you're right. Lemme delete it. \$\endgroup\$ – mudkip201 Feb 23 '18 at 13:32
0
\$\begingroup\$

APL(NARS), 1 char, 2 bytes

π

That is...

\$\endgroup\$
  • \$\begingroup\$ ... rather short? \$\endgroup\$ – Jonathan Frech Mar 18 at 9:47
  • \$\begingroup\$ @JonathanFrech yes it is short... Less than a character should be impossible... \$\endgroup\$ – RosLuP Mar 18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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