-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPowerOfTwo.java
More file actions
41 lines (33 loc) · 862 Bytes
/
isPowerOfTwo.java
File metadata and controls
41 lines (33 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*231. Power of Two
* Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
*
* Input: n = 1
Output: true
Explanation: 20 = 1
*
*
* Input: n = 3
Output: false
*/
import java.util.Scanner;
import java.lang.Math;
public class isPowerOfTwo {
public static Scanner scan = new Scanner(System.in);
public static boolean solve(int num, int p){
double res = Math.pow(2, p);
//BC
if(res > num) return false;
//MC
if(res == num) return true;
//RC
return solve(num, p+1);
}
public static boolean solve(int num) {
return solve(num, 0);
}
public static void main(String[] args) {
int num = scan.nextInt();
System.out.println(solve(num));
}
}