# \#326 Power of Three ## *回傳是否為3的倍數* ## Log - build 20210614 by syhuang ## 奇葩解 - 直接用整數變數可以存放的最大3的倍數整數(即3^19), 來判斷該數是否為3的倍數(可以被整除就表示是3的倍數) ```javascript= var isPowerOfThree = function(n) { return n>0 && 1162261467%n==0 }; ``` ## 初見 - 邏輯實作:不斷除以3的最後結果是1就表示是3的倍數 ```javascript= var isPowerOfThree = function(n) { let t = n; for(let i=3; t%i==0 && t>=i; t/=i){} return t==1?true:false; }; ``` ## 備註 ## 參考 - [參考解](https://leetcode.com/problems/power-of-three/discuss/77856/1-line-java-solution-without-loop-recursion) ###### tags: `leetcode`, `leetcode-easy`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up