Given an integer n, return true if n is a power of three (that is, n equals 3 raised to some non-negative integer power). Return false for null, zero, or negative values.

Example 1:

Input: n = 27 Output: true Explanation: 27 equals 3 raised to the power 3, so it is a power of three.

Example 2:

Input: n = 1 Output: true Explanation: 1 equals 3 raised to the power 0, so it is a power of three.

Example 3:

Input: n = 10 Output: false Explanation: 10 cannot be expressed as 3 raised to any non-negative integer, so it is not a power of three.

Example 4:

Input: n = 0 Output: false Explanation: Zero is not a power of three.
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 August 2026

Contest runs August 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 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