From 2355b98cb4f766c9a8b44fba06009c46a4d77dd0 Mon Sep 17 00:00:00 2001 From: JuliaVorotchenko Date: Fri, 29 May 2020 17:54:38 +0300 Subject: [PATCH] [Math] Add solution for Power of Four problem --- Math/PowerOfFour.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Math/PowerOfFour.swift diff --git a/Math/PowerOfFour.swift b/Math/PowerOfFour.swift new file mode 100644 index 00000000..697d833d --- /dev/null +++ b/Math/PowerOfFour.swift @@ -0,0 +1,12 @@ +/** + * Question Link: https://leetcode.com/problems/power-of-four/ + * Primary idea: Use log properties to solve the problem + * Time Complexity: O(1), Space Complexity: O(1) + * + */ + +class PowerOfFour { + func isPowerOfFour(_ num: Int) -> Bool { + return (log(Double(num))/log(4.0)).truncatingRemainder(dividingBy: 1) == 0 + } +}