Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find All Prime Factors of a Number

Basic Acceptance 75.67% Points 10.00

You are given a positive integer n. Your task is to print all its prime factors in increasing order, including repeated occurrences.

A prime factor is a prime number that divides the given number exactly. If a prime factor appears multiple times in the factorization, each occurrence must be included in the output. If n = 1, the output should be empty because 1 has no prime factors.

Examples
Example 1

Input: n = 60
Output: 2 2 3 5
Explanation: The prime factorization of 60 is 2 × 2 × 3 × 5.

Example 2

Input: n = 13
Output: 13
Explanation: Since 13 is prime, its only prime factor is 13 itself.

Example 3

Input: n = 1
Output: (empty)
Explanation: The number 1 has no prime factors, so the output is empty.

Constraints
  • 1 = n = 10¹²
Companies
Microsoft Netflix
Topics
Data Structures Basics Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions