Hướng dẫn how do you check if a number is a power in python? - làm thế nào để bạn kiểm tra xem một số có phải là lũy thừa 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;
Examples : 

Đầu vào: & nbsp; x = 10, y = 1Output: trorex^0 = 1 x = 10, y = 1
Output: True
x^0 = 1

Đầu vào: & nbsp; x = 10, y = 1000Output: truex^3 = 1  x = 10, y = 1000
Output: True
x^3 = 1

Đầu vào: & nbsp; x = 10, y = 1001Output: false 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(int x, long int #include 1

#include 2

#include 3#include 4 #include 5

#include 6#include 7 #include 8

#include 3long int using2 using3

#include 3using5 using6using2 using8

#include 6using2 namespace1

#include 3#include 7 using6using2 namespace6

namespace7

int namespace9

#include 2

#include 3std;2

#include 3std;4

#include 3std;6

#include 3std;8

#include 3#include 7 bool1

namespace7

Java

bool3 bool4 bool5

#include 3bool3 bool8 bool9 isPower(0

#include 3#include 2

Các

Các

Các

Các

#include 3namespace7

#include 3bool3 bool8 #include 08 isPower(int x, int #include 1

#include 3#include 2

#include 6#include 4 #include 18isPower(7#include 20

#include 21#include 7 #include 23isPower(7int2

#include 6int #include 28isPower(7#include 30

#include 6using5 #include 33

#include 21#include 35

#include 6#include 7 #include 38

#include 3namespace7

namespace7

Python3

#include 42 #include 43

#include 3#include 4 #include 46#include 47#include 47 isPower(7#include 50

#include 6#include 7 #include 53#include 47#include 47 isPower(7#include 20

#include 3using2 #include 47 isPower(7

#include 3using5 using6using2 #include 66

#include 6using2 #include 47 using2 #include 71 #include 72

#include 3#include 7 using6using2 #include 47#include 47 #include 1

#include 4#include 81isPower(5isPower(6isPower(7#include 85

#include 3#include 87using6isPower(7#include 20

#include 91#include 92

#include 3#include 87using6int1#include 20

#include 4#include 81isPower(7isPower(6int7#include 85

#include 3#include 87using6isPower(7#include 20

#include 91#include 92

#include 3#include 87using6int1#include 20

#include 4#include 81x, 5isPower(6x, 7#include 85

#include 3#include 87using6isPower(7#include 20

#include 91#include 92

#include 3#include 87using6int1#include 20

#include 4#include 81x, 5isPower(6long7#include 85

#include 3#include 87using6isPower(7#include 20

#include 91#include 92

#include 3#include 87using6int1#include 20

C#

using using53

bool4 using55

#include 2

#include 3bool3 bool8 bool using61int x, int #include 1

#include 3#include 2

#include 6#include 4 #include 5

#include 6#include 7 #include 8

#include 3long int using2 using3

#include 6using5 #include 33

#include 6#include 35

#include 6#include 7 #include 38

#include 3namespace7

#include 42 #include 43

#include 3#include 2

#include 6using95

#include 6using97

#include 6using99

#include 6namespace01

#include 3namespace7

namespace7

#include 3#include 4 #include 46#include 47#include 47 isPower(7#include 50

namespace05

#include 6#include 7 #include 53#include 47#include 47 isPower(7#include 20

#include 2

#include 3using2 #include 47 isPower(7

#include 3using5 using6using2 #include 66

#include 6using2 #include 47 using2 #include 71 #include 72

#include 3#include 7 using6using2 #include 47#include 47 #include 1

#include 3#include 87using6isPower(7#include 20

using using53

namespace7

bool4 using55

#include 3bool3 bool8 bool using61int x, int #include 1

#include 6#include 4 #include 5

#include 6int using76

namespace62

#include 3bool3 bool8 bool9 using91

namespace63

PHP

#include 3#include 2

#include 6#include 4 #include 5

#include 6int using76

#include 6namespace76

#include 6using5 #include 33

#include 21#include 35

#include 6#include 7 #include 38

#include 3namespace7

#include 6namespace88namespace89int2

namespace91namespace92namespace89int2

#include 6namespace96namespace89int2

#include 6std;00namespace89int2

std;03

Độ phức tạp về thời gian: O (logxy) Không gian phụ trợ: O (1) O(Logxy)
Auxiliary space: O(1)

Tối ưu hóa: & nbsp; chúng ta có thể tối ưu hóa giải pháp ở trên để làm việc trong O (nhật ký 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; 
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++

std;04

std;05

using namespace std;

bool isPower(int x, int #include 1

#include 2

#include 3int std;18std;19std;20____419std;22

#include 3std;24 std;25std;19std;20std;19std;29

#include 3#include 7 std;32

namespace7

int namespace9

#include 2

#include 3std;38

#include 3#include 7 bool1

namespace7

Java

bool4 using55

#include 2

#include 3bool8 #include 08 isPower(int std;51

std;52int #include 1

#include 3#include 2

std;62using6intstd;65

#include 6std;24 std;68

std;69std;70

#include 6#include 7 std;32

#include 3namespace7

#include 3bool3 bool8 bool9 std;80

#include 3#include 2

#include 6#include 4#include 81std;86isPower(6std;88__

#include 21std;91std;92int2

#include 6#include 91

#include 21std;91std;98int2

#include 3namespace7

namespace7

Python3

bool03 bool04

#include 42 bool06

#include 3bool08#include 47 bool10bool11bool11 bool13

#include 3bool15#include 47 bool10bool11 bool13

#include 3#include 7 isPower(7 #include 4bool24__

#include 4 bool32#include 47#include 47bool35#include 92

#include 3#include 87#include 81std;86isPower(6std;88bool43

C#

using using53

bool4 using55

#include 2

#include 3bool8 #include 08 isPower(int std;51

#include 2

std;52int #include 1

bool62using6intbool65

bool69bool70

#include 3#include 7 std;32

namespace7

int namespace9

#include 2

#include 3#include 4bool81

#include 6bool83std;92int2

#include 3#include 91

#include 6bool83std;98int2

namespace7

namespace7

#include 3#include 7 bool1

namespace05

Java

#include 2

bool4 using55

#include 3bool8 #include 08 isPower(int std;51

std;52int #include 1

namespace7

namespace62

#include 6std;24 std;68

namespace63

#include 6#include 7 std;32

#include 2

#include 3bool3 bool8 bool9 std;80

bool62isPower(35

#include 6#include 4#include 81std;86isPower(6std;88__

bool69std;70

#include 3#include 7 std;32

namespace7

#include 4bool81

#include 3isPower(48std;92int2

#include 91

#include 3isPower(48std;98int2

std;03

int namespace9: O(1)
Auxiliary space: O(1)

#include 3#include 7 bool1Gyayak 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 sức mạnh trong Python?

POW (số, số mũ) chức năng để tìm sức mạnh của số ...
Nhập toán. in (toán học. pow (4,2)) chạy. Nhập mô -đun toán học trong Python ..
DEF POWER (N, E): Res = 0. cho i trong phạm vi (e): res *= n. Trả lại res. In (POW (4,2)) chạy. ....
DEF POWER (N, E): Nếu E == 0: Trả về 1. Elif E == 1: Trả về n. Khác: return (n*power (n, e-1)).

Làm thế nào để bạn kiểm tra xem một số là một nguồn?

Tiếp tục chia số cho hai, tức là làm n = n/2 lặp lại cho đến khi n trở thành 1. trong bất kỳ lần lặp nào, nếu n%2 trở thành không khác và n không phải là 1 thì n không phải là sức mạnh của 2. Nếu n trở thành1 Sau đó, nó là một sức mạnh của 2. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

Làm thế nào để bạn kiểm tra xem một số có thể được thể hiện bằng quyền hạn?

Algorithm/Insights..
Khởi tạo yếu tố = 2 ..
Kiểm tra xem số có chia hết cho 'yếu tố' không.....
Sau bước 2, nếu chúng ta bị bỏ lại với số = 1, thì số có thể được biểu diễn dưới dạng sức mạnh của yếu tố, vì vậy hãy trả về đúng.....
Lặp lại các bước 2 và 3 cho đến khi hệ số

Làm thế nào để bạn kiểm tra xem một số có phải là sức mạnh của 3 trong Python không?

Khoa học dữ liệu thực tế sử dụng Python..
Chúng tôi sẽ sử dụng logarit để giải quyết điều này ..
Nếu [log10 (n) / log10 (3)] mod 1 == 0, thì nó sẽ là sức mạnh của ba, nếu không thì không ..