Problem 112984 · medium · Phase 01 Prerequisites & Setup

Prime Factorisation

functions · primes · lists

Write prime_factors(n) that returns the prime factors of n as a list in non-decreasing order, repeating a factor as many times as it divides n. prime_factors(1) is [].

Examples

Input:  n = 12
Output: [2, 2, 3]

Input:  n = 13
Output: [13]

Input:  n = 360
Output: [2, 2, 2, 3, 3, 5]

Constraints

  • 1 <= n <= 10**12
  • Your function must be fast even when n is a large prime.

Goals

  • Divide out each factor repeatedly
  • Handle the leftover large prime after the loop
Starting Python…