Cho hai số dương x và y, hãy kiểm tra xem y có phải là sức mạnh của x hay không.examples: & nbsp; & nbsp;
Examples :
Input: x = 10, y = 1 Output: True x^0 = 1 Input: x = 10, y = 1000 Output: True x^3 = 1 Input: x = 10, y = 1001 Output: False
Một giải pháp đơn giản là liên tục tính toán sức mạnh của x. Nếu một sức mạnh trở nên bằng y, thì y là một sức mạnh, thì không phải. & Nbsp; & nbsp;
C++
#include
using
namespace
std;
bool
isPower[
1 0 1 00
1 0 1 01
1 0 1 02
1 0 1 00
1 0 1 04
1 0 1 05
1 0 1 06
1 0 1 07
1 0 1 08
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.1
1 0 1 06
1 0 1 02
1 0 1 00
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.6
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
11
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
14
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
19
#include
0
1 0 1 00
#include
21 0 1 05
1 0 1 06
#include
51 0 1 06
#include
71 0 1 06
#include
91 0 1 06
using
11 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
using
4#include
0
Java
using
6 using
7 using
8
1 0 1 06
using
6 namespace
1 namespace
2 namespace
31 0 1 06
1 0 1 05
Các
Các
Các
Các
1 0 1 06
#include
01 0 1 06
using
6 namespace
1 1 0 1 011
isPower[
1 0 1 00
1 0 1 01____10
1 0 1 04
1 0 1 06
1 0 1 05
1 0 1 09
1 0 1 07
1 0 1 021
std;
01 0 1 023
1 0 1 024
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1 0 1 026
std;
0std;
51 0 1 09
1 0 1 00
1 0 1 031
std;
01 0 1 033
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1 0 1 036
1 0 1 024
1 0 1 038
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1 0 1 041
1 0 1 06
#include
0#include
0
Python3
1 0 1 045
1 0 1 046
1 0 1 06
1 0 1 07
1 0 1 049__150150150
std;
01 0 1 053
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1 0 1 056
1 0 1 050
1 0 1 050
std;
01 0 1 023
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
std;
01 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 069
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 074
1 0 1 075
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
1 0 1 050
1 0 1 04
1 0 1 07
1 0 1 084
namespace
8namespace
9std;
01 0 1 088
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
01 0 1 023
1 0 1 094
1 0 1 095
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
41 0 1 023
1 0 1 07
1 0 1 084
std;
0namespace
9bool
01 0 1 088
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
01 0 1 023
1 0 1 094
1 0 1 095
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
41 0 1 023
1 0 1 07
1 0 1 084
bool
8namespace
9isPower[
01 0 1 088
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
01 0 1 023
1 0 1 094
1 0 1 095
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
41 0 1 023
1 0 1 07
1 0 1 084
bool
8namespace
91 0 1 000
1 0 1 088
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
01 0 1 023
1 0 1 094
1 0 1 095
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
41 0 1 023
C#
using
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.56
using
7
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.58
1 0 1 05
1 0 1 06
using
6 namespace
1 bool
1 0 1 06
1 0 1 05
1 0 1 09
1 0 1 07
1 0 1 08
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.1
1 0 1 06
1 0 1 02
1 0 1 00
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.6
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1 0 1 036
1 0 1 09
1 0 1 038
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1 0 1 041
1 0 1 06
#include
01 0 1 045
1 0 1 046
1 0 1 06
1 0 1 05
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.98
1 0 1 09
100
1 0 1 09
102
1 0 1 06
1 0 1 07
1 0 1 049__150150150
std;
01 0 1 053
1 0 1 06
#include
0#include
0
1
0
1
0
91] Initialize pow = x, i = 1
2] while [pow < y]
{
pow = pow*pow
i *= 2
}
3] If pow == y
return true;
4] Else construct an array of powers
from x^i to x^[i/2]
5] Binary Search for y in array constructed
in step 4. If not found, return false.
Else return true.
0 1
0
1
0
561
0
1
0
501
0
1
0
50 std;
01
0
1
0
23
108
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
std;
01 0 1 05
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 069
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 074
1 0 1 075
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.5
1 0 1 050
1 0 1 050
1 0 1 04
1 0 1 06
1 0 1 090
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
std;
01 0 1 023
using
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.56
using
7
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.58
#include
0
1 0 1 06
using
6 namespace
1 bool
1 0 1 09
1 0 1 07
1 0 1 08
1 0 1 09
1 0 1 00
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.79
1 0 1 06
using
6 namespace
1 namespace
2 1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.94
165
1
0
1
0
91
04
166
PHP
1 0 1 06
1 0 1 05
1 0 1 09
1 0 1 07
1 0 1 08
1 0 1 09
1 0 1 00
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.79
1 0 1 09
179
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.8
1 0 1 036
1 0 1 024
1 0 1 038
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
1 0 1 041
1 0 1 06
#include
01 0 1 045
1 0 1 046
194
195
192
std;
51 0 1 09
199
192
std;
51 0 1 09
#include
03192
std;
5#include
06
Output:
1 0 1 0
Độ phức tạp thời gian của giải pháp trên là tối ưu hóa O [logxy]: & nbsp; chúng ta có thể tối ưu hóa giải pháp trên để làm việc trong o [log log y]. Ý tưởng là làm bình phương sức mạnh thay vì nhân nó với x, tức là so sánh y với x^2, x^4, x^8, vv. Nếu x trở nên bằng y, hãy trả về đúng. Nếu x trở thành nhiều hơn y, thì chúng ta sẽ tìm kiếm nhị phân cho sức mạnh của x giữa công suất trước và công suất hiện tại, tức là, giữa x^i và x^[i/2]. Theo dõi là bước chi tiết. & Nbsp;
Optimization:
We can optimize above solution to work in O[Log Log y]. The idea is to do squaring of power instead of multiplying it with x, i.e., compare y with x^2, x^4, x^8, …etc. If x becomes equal to y, return true.
If x becomes more than y, then we do binary search for power of x between previous power and current power, i.e., between x^i and x^[i/2].
Following are detailed step.
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.
Giải pháp thay thế: & nbsp; Ý tưởng là lấy nhật ký của y trong cơ sở x. Nếu nó hóa ra là một số nguyên, chúng ta sẽ trả về đúng. Khác sai. & Nbsp; & nbsp;
The idea is to take log of y in base x. If it turns out to be an integer, we return true. Else false.
C++
#include
07
#include
08
using
namespace
std;
bool
isPower[
1 0 1 00
1 0 1 01
1 0 1 00
1 0 1 04
1 0 1 05
‘
1 0 1 06
#include
27 #include
28#include
222223#include
222221 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
#include
35#include
0
1 0 1 00
#include
21 0 1 05
1 0 1 06
#include
411 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
using
4#include
0
Java
using
7
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.58
1 0 1 05
1 0 1 06
namespace
1 1 0 1 011
isPower[
1 0 1 00
#include
54#include
55
1 0 1 00
1 0 1 04
1 0 1 06
1 0 1 05
1 0 1 09
1 0 1 00
#include
62____1010#include
64#include
65
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1 0 1 00
#include
681 0 1 09
#include
27 #include
71#include
72#include
73
1 0 1 09
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
#include
351 0 1 06
#include
01 0 1 06
using
6 namespace
1 namespace
2 #include
831 0 1 06
1 0 1 05
1 0 1 09
1 0 1 07
1 0 1 084
#include
89namespace
9#include
91#include
921 0 1 024
#include
94#include
95std;
51 0 1 09
1 0 1 094
1 0 1 024
#include
94using
01std;
51 0 1 06
#include
0#include
0
Python3
using
06 using
07
1 0 1 045
using
091 0 1 06
using
111 0 1 050
using
13using
14using
14 using
161 0 1 06
using
181 0 1 050
using
13using
14 using
161 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
std;
0 1 0 1 07
using
27__1501501501 0 1 07
using
351 0 1 050
1 0 1 050
using
381 0 1 095
1 0 1 06
1 0 1 090
1 0 1 084
#include
89namespace
9#include
91using
46C#
using
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.56
using
7
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.58
1 0 1 05
1 0 1 06
namespace
1 1 0 1 011
isPower[
1 0 1 00
#include
541 0 1 05
#include
55
1 0 1 00
1 0 1 04
using
65
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.9
1 0 1 00
using
681 0 1 09
1 0 1 00
#include
62____1010#include
64using
72using
73
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
#include
35#include
0
1 0 1 00
#include
21 0 1 05
1 0 1 06
1 0 1 07
using
841 0 1 09
using
86#include
95std;
51 0 1 06
1 0 1 094
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
using
4#include
0
#include
0
Java
108
using
7
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.58
1 0 1 05
1 0 1 06
namespace
1 1 0 1 011
isPower[
1 0 1 00
#include
54#include
55
1 0 1 00
1 0 1 04
1 0 1 09
1 0 1 00
#include
62____1010#include
64#include
0
1 0 1 09
#include
27 #include
71165
1
0
1
0
91] Initialize pow = x, i = 1
2] while [pow < y]
{
pow = pow*pow
i *= 2
}
3] If pow == y
return true;
4] Else construct an array of powers
from x^i to x^[i/2]
5] Binary Search for y in array constructed
in step 4. If not found, return false.
Else return true.
0 #include
35
166
1 0 1 06
using
6 namespace
1 namespace
2 #include
831 0 1 05
#include
0
using
65namespace
38
using
06 using
07
using
72#include
73
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
#include
35#include
0
1 0 1 07
using
841 0 1 06
namespace
51#include
95std;
51 0 1 094
1 0 1 06
namespace
51using
01std;
5#include
06
1
0
1
0
0 #include
2
1
1 0 1 06
1] Initialize pow = x, i = 1 2] while [pow < y] { pow = pow*pow i *= 2 } 3] If pow == y return true; 4] Else construct an array of powers from x^i to x^[i/2] 5] Binary Search for y in array constructed in step 4. If not found, return false. Else return true.0
using
4Gyayak Jain for suggesting this solution.This article is contributed by Manish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above