Hướng dẫn how do you check if a number is a power of 10 in python? - làm thế nào để bạn kiểm tra xem một số có phải là lũy thừa của 10 trong python không?

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
0
0
1
0
1
0
1
1
0
1
0
2
1
0
1
0
0
1
0
1
0
4

1
0
1
0
5

1
0
1
0
6
1
0
1
0
7
1
0
1
0
8

1
0
1
0
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.
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
0
6
1
0
1
0
2
1
0
1
0
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.
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
0
6
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
1

1
0
1
0
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
4

1
0
1
0
6
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
9

#include 0

1
0
1
0
0 #include 2

1
0
1
0
5

1
0
1
0
6#include 5

1
0
1
0
6#include 7

1
0
1
0
6#include 9

1
0
1
0
6using1

1
0
1
0
6
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 using4

#include 0

Java

using6 using7 using8

1
0
1
0
6using6 namespace1 namespace2 namespace3

1
0
1
0
6
1
0
1
0
5

Các

Các

Các

Các

1
0
1
0
6#include 0

1
0
1
0
6using6 namespace1
1
0
1
0
11 isPower[
1
0
1
0
0
1
0
1
0
1____10
1
0
1
0
4

1
0
1
0
6
1
0
1
0
5

1
0
1
0
9
1
0
1
0
7
1
0
1
0
21std;0
1
0
1
0
23

1
0
1
0
24
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
0
26std;0std;5

1
0
1
0
9
1
0
1
0
0
1
0
1
0
31std;0
1
0
1
0
33

1
0
1
0
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.
8
1
0
1
0
36

1
0
1
0
24
1
0
1
0
38

1
0
1
0
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.
0
1
0
1
0
41

1
0
1
0
6#include 0

#include 0

Python3

1
0
1
0
45
1
0
1
0
46

1
0
1
0
6
1
0
1
0
7
1
0
1
0
49__150150150 std;0
1
0
1
0
53

1
0
1
0
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.
0
1
0
1
0
56
1
0
1
0
50
1
0
1
0
50 std;0
1
0
1
0
23

1
0
1
0
6
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
0
50 std;0

1
0
1
0
6
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
0
69

1
0
1
0
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
0
50
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
0
74
1
0
1
0
75

1
0
1
0
6
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
0
50
1
0
1
0
50
1
0
1
0
4

1
0
1
0
7
1
0
1
0
84namespace8namespace9std;0
1
0
1
0
88

1
0
1
0
6
1
0
1
0
90
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.
9std;0
1
0
1
0
23

1
0
1
0
94
1
0
1
0
95

1
0
1
0
6
1
0
1
0
90
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.
9std;4
1
0
1
0
23

1
0
1
0
7
1
0
1
0
84std;0namespace9bool0
1
0
1
0
88

1
0
1
0
6
1
0
1
0
90
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.
9std;0
1
0
1
0
23

1
0
1
0
94
1
0
1
0
95

1
0
1
0
6
1
0
1
0
90
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.
9std;4
1
0
1
0
23

1
0
1
0
7
1
0
1
0
84bool8namespace9isPower[0
1
0
1
0
88

1
0
1
0
6
1
0
1
0
90
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.
9std;0
1
0
1
0
23

1
0
1
0
94
1
0
1
0
95

1
0
1
0
6
1
0
1
0
90
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.
9std;4
1
0
1
0
23

1
0
1
0
7
1
0
1
0
84bool8namespace9
1
0
1
0
00
1
0
1
0
88

1
0
1
0
6
1
0
1
0
90
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.
9std;0
1
0
1
0
23

1
0
1
0
94
1
0
1
0
95

1
0
1
0
6
1
0
1
0
90
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.
9std;4
1
0
1
0
23

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

using7

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
0
5

1
0
1
0
6using6 namespace1 bool

1
0
1
0
6
1
0
1
0
5

1
0
1
0
9
1
0
1
0
7
1
0
1
0
8

1
0
1
0
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.
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
0
6
1
0
1
0
2
1
0
1
0
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.
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
0
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.
8
1
0
1
0
36

1
0
1
0
9
1
0
1
0
38

1
0
1
0
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.
0
1
0
1
0
41

1
0
1
0
6#include 0

1
0
1
0
45
1
0
1
0
46

1
0
1
0
6
1
0
1
0
5

1
0
1
0
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.
98

1
0
1
0
9
1
00

1
0
1
0
9
1
02

1
0
1
0
6
1
0
1
0
7
1
0
1
0
49__150150150 std;0
1
0
1
0
53

1
0
1
0
6#include 0

#include 0

1
0
1
0
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.
0
1
0
1
0
56
1
0
1
0
50
1
0
1
0
50 std;0
1
0
1
0
23

1
08

1
0
1
0
6
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
0
50 std;0

1
0
1
0
5

1
0
1
0
6
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
0
69

1
0
1
0
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
0
50
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
0
74
1
0
1
0
75

1
0
1
0
6
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
0
50
1
0
1
0
50
1
0
1
0
4

1
0
1
0
6
1
0
1
0
90
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.
9std;0
1
0
1
0
23

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

using7

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
0
6using6 namespace1 bool

1
0
1
0
9
1
0
1
0
7
1
0
1
0
8

1
0
1
0
9
1
0
1
0
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.
79

1
0
1
0
6using6 namespace1 namespace2
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

1
65

1
0
1
0
9
1
04

1
66

PHP

1
0
1
0
6
1
0
1
0
5

1
0
1
0
9
1
0
1
0
7
1
0
1
0
8

1
0
1
0
9
1
0
1
0
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.
79

1
0
1
0
9
1
79

1
0
1
0
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.
8
1
0
1
0
36

1
0
1
0
24
1
0
1
0
38

1
0
1
0
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.
0
1
0
1
0
41

1
0
1
0
6#include 0

1
0
1
0
45
1
0
1
0
46

1
94
1
95
1
92std;5

1
0
1
0
9
1
99
1
92std;5

1
0
1
0
9#include 03
1
92std;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
0
0
1
0
1
0
1
1
0
1
0
0
1
0
1
0
4

1
0
1
0
5

1
0
1
0
6#include 27 #include 28#include 222223#include 22222

1
0
1
0
6
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
0
0 #include 2

1
0
1
0
5

1
0
1
0
6#include 41

1
0
1
0
6
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 using4

#include 0

Java

using7

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
0
5

1
0
1
0
6namespace1
1
0
1
0
11 isPower[
1
0
1
0
0 #include 54

#include 55

1
0
1
0
0
1
0
1
0
4

1
0
1
0
6
1
0
1
0
5

1
0
1
0
9
1
0
1
0
0 #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
0
0#include 68

1
0
1
0
9#include 27 #include 71

#include 72#include 73

1
0
1
0
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.
0 #include 35

1
0
1
0
6#include 0

1
0
1
0
6using6 namespace1 namespace2 #include 83

1
0
1
0
6
1
0
1
0
5

1
0
1
0
9
1
0
1
0
7
1
0
1
0
84#include 89namespace9#include 91#include 92

1
0
1
0
24#include 94#include 95std;5

1
0
1
0
9
1
0
1
0
94

1
0
1
0
24#include 94using01std;5

1
0
1
0
6#include 0

#include 0

Python3

using06 using07

1
0
1
0
45 using09

1
0
1
0
6using11
1
0
1
0
50 using13using14using14 using16

1
0
1
0
6using18
1
0
1
0
50 using13using14 using16

1
0
1
0
6
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
0
7using27__150150150

1
0
1
0
7 using35
1
0
1
0
50
1
0
1
0
50using38
1
0
1
0
95

1
0
1
0
6
1
0
1
0
90
1
0
1
0
84#include 89namespace9#include 91using46

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

using7

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
0
5

1
0
1
0
6namespace1
1
0
1
0
11 isPower[
1
0
1
0
0 #include 54

1
0
1
0
5

#include 55

1
0
1
0
0
1
0
1
0
4

using65

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
0
0using68

1
0
1
0
9
1
0
1
0
0 #include 62____1010#include 64

using72using73

1
0
1
0
6
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
0
0 #include 2

1
0
1
0
5

1
0
1
0
6
1
0
1
0
7using84

1
0
1
0
9using86#include 95std;5

1
0
1
0
6
1
0
1
0
94

1
0
1
0
6
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 using4

#include 0

#include 0

Java

1
08

using7

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
0
5

1
0
1
0
6namespace1
1
0
1
0
11 isPower[
1
0
1
0
0 #include 54

#include 55

1
0
1
0
0
1
0
1
0
4

1
0
1
0
9
1
0
1
0
0 #include 62____1010#include 64

#include 0

1
0
1
0
9#include 27 #include 71

1
65

1
0
1
0
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.
0 #include 35

1
66

1
0
1
0
6using6 namespace1 namespace2 #include 83

1
0
1
0
5

#include 0

using65namespace38

using06 using07

using72#include 73

1
0
1
0
6
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
0
7using84

1
0
1
0
6namespace51#include 95std;5

1
0
1
0
94

1
0
1
0
6namespace51using01std;5

#include 06

1
0
1
0
0 #include 2 
 

1

1
0
1
0
6
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 using4Gyayak 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
 


Làm thế nào để bạn kiểm tra xem một số là một công suất 10?

Do đó, được thể hiện ở dạng dài, công suất 10 là số 1 theo sau bởi n số không, trong đó n là số mũ và lớn hơn 0; Ví dụ, 106 được viết 1.000.000. Khi N nhỏ hơn 0, công suất của 10 là vị trí N số 1 sau điểm thập phân; Ví dụ, 10−2 được viết 0,01.the number 1 followed by n zeros, where n is the exponent and is greater than 0; for example, 106 is written 1,000,000. When n is less than 0, the power of 10 is the number 1 n places after the decimal point; for example, 10−2 is written 0.01.

Làm thế nào để bạn kiểm tra xem một số là sức mạnh của một số trong Python?

Sau đây là bước chi tiết.1] Khởi tạo pow = x, i = 1 2] trong khi [pow

Bài Viết Liên Quan

Chủ Đề