Given a positive integer n, return a list of its prime factors in ascending order with repetition. For example, 12 = 2 x 2 x 3, so the result is [2, 2, 3]. For values of n less than or equal to 1, return an empty list.

Examples:

Input: n = 12 Output: [2, 2, 3] Explanation: 12 = 2 x 2 x 3 Input: n = 7 Output: [7] Explanation: 7 is itself prime Input: n = 1 Output: [] Explanation: 1 has no prime factors Input: n = 100 Output: [2, 2, 5, 5] Explanation: 100 = 2 x 2 x 5 x 5 Input: n = 30 Output: [2, 3, 5] Explanation: 30 = 2 x 3 x 5
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay October 2026 (Oct 16–31)Sponsored by FLXBL

Contest runs October 16 - 31. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 16 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In