The Prime Factorizer
MediumAcc. 88.4%
+30 XP 12
Basic Decomposition
Every integer can be uniquely represented as a product of prime numbers. Example: $12 = 2 imes 2 imes 3$.
The Assignment
Your function receives a parameter named num.
- Start with a
divisorat 2. - Use a
whileloop that continues as long asnumis greater than 1. - Inside the loop:
- If
numis divisible bydivisor:- Print the
divisor. - Update
numtonum / divisor.
- Print the
- Otherwise:
- Increment the
divisorto find the next candidate.
- Increment the
- If
01EXAMPLE 1
Input
num = 12Output
2
2
3Explanation: Finds all prime building blocks.
Constraints
- Print each factor on a new line.
- The loop must naturally skip non-prime divisors.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
num12
Expected Output
2 2 3
Click RUN to test your solution