Submit an Entry

To enter the challenge, you need to signup or login.

This Challenge's Best Entries [View All]

(View the Overall | Perl | PHP | Python | Ruby leaderboard.)

Rank User Size Language Score [?]
1st bearstearns 76 Perl 10,000 (v9)
2nd tybalt89 77 Perl 9,870 (v1)
3rd kounoike 77 Perl 9,870 (v41)
4th shinh 77 Perl 9,870 (v64)
5th arpad 78 Perl 9,743 (v26)
6th ySas 78 Perl 9,743 (v17)
7th primo 79 Perl 9,620 (v39)
8th o0lit3 79 Perl 9,620 (v15)
9th yvl 79 Perl 9,620 (v21)
10th terjek 80 Perl 9,500 (v12)

See who is active in this challenge →

Prime Factors

(Challenge added 1464 days ago.)

Work out the prime factors of a given number.

Discuss This Challenge →

The Problem

As you know, a prime number is any number with no divisors other than itself and 1. It is possible to express any number as a product of a unique series of prime numbers. Your code should, for a given number, output the primes whose product equal that number. For example, for the number 34, the prime factors are 2 and 17.

Note that the same factor may need to be used more than once. For the number 232, the factors are 2, 2, 2 and 29.

Your code needs to output to a specific format. For the above two examples, your code should output

34: 2 17
and
232: 2^3 29

That is, the number you are factoring followed by a colon (:) and a space, followed by the prime factors in ascending order separated by spaces.

In the cases where there are factors repeated, they should be represented as x^y where x is the factor and y is the number of times the factor is repeated.

More Information

  • Your code should be able to accept all numbers between 0 and 4,294,967,296 (2**32) inclusive.
  • The number will be given to your code on stdin, and your output, matching the format described above, on stdout.

Examples

Your code will be run seven times with various numbers. For the first four runs, the same numbers will be used. You can see these numbers and the expected output by following the links :

We will use random numbers as the seeds for the remaining three runs. Click the following links to see examples of these numbers. Reload the resultant page to get a new number.