Hướng dẫn how do you check if a number is a power of another 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 một con trăn khác 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 <bits/stdc++.h>

using namespace std;

bool isPower(int x, long int #include <bits/stdc++.h>1

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3#include <bits/stdc++.h>4 #include <bits/stdc++.h>5

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>8

#include <bits/stdc++.h>3long int using2 using3

#include <bits/stdc++.h>3using5 using6using2 using8

#include <bits/stdc++.h>6using2 namespace1

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 using6using2 namespace6

namespace7

int namespace9

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3std;2

#include <bits/stdc++.h>3std;4

#include <bits/stdc++.h>3std;6

#include <bits/stdc++.h>3std;8

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 bool1

namespace7

Java

bool3 bool4 bool5

#include <bits/stdc++.h>3bool3 bool8 bool9 isPower(0

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

Các

Các

Các

Các

#include <bits/stdc++.h>3namespace7

#include <bits/stdc++.h>3bool3 bool8 #include <bits/stdc++.h>08 isPower(int x, int #include <bits/stdc++.h>1

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

#include <bits/stdc++.h>6#include <bits/stdc++.h>4 #include <bits/stdc++.h>18isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>21#include <bits/stdc++.h>7 #include <bits/stdc++.h>23isPower(7int2

#include <bits/stdc++.h>6int #include <bits/stdc++.h>28isPower(7#include <bits/stdc++.h>30

#include <bits/stdc++.h>6using5 #include <bits/stdc++.h>33

#include <bits/stdc++.h>21#include <bits/stdc++.h>35

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>38

#include <bits/stdc++.h>3namespace7

namespace7

Python3

#include <bits/stdc++.h>42 #include <bits/stdc++.h>43

#include <bits/stdc++.h>3#include <bits/stdc++.h>4 #include <bits/stdc++.h>46#include <bits/stdc++.h>47#include <bits/stdc++.h>47 isPower(7#include <bits/stdc++.h>50

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>53#include <bits/stdc++.h>47#include <bits/stdc++.h>47 isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>3using2 #include <bits/stdc++.h>47 isPower(7

#include <bits/stdc++.h>3using5 using6using2 #include <bits/stdc++.h>66

#include <bits/stdc++.h>6using2 #include <bits/stdc++.h>47 using2 #include <bits/stdc++.h>71 #include <bits/stdc++.h>72

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 using6using2 #include <bits/stdc++.h>47#include <bits/stdc++.h>47 #include <bits/stdc++.h>1

#include <bits/stdc++.h>4#include <bits/stdc++.h>81isPower(5isPower(6isPower(7#include <bits/stdc++.h>85

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>91#include <bits/stdc++.h>92

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6int1#include <bits/stdc++.h>20

#include <bits/stdc++.h>4#include <bits/stdc++.h>81isPower(7isPower(6int7#include <bits/stdc++.h>85

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>91#include <bits/stdc++.h>92

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6int1#include <bits/stdc++.h>20

#include <bits/stdc++.h>4#include <bits/stdc++.h>81x, 5isPower(6x, 7#include <bits/stdc++.h>85

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>91#include <bits/stdc++.h>92

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6int1#include <bits/stdc++.h>20

#include <bits/stdc++.h>4#include <bits/stdc++.h>81x, 5isPower(6long7#include <bits/stdc++.h>85

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>91#include <bits/stdc++.h>92

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6int1#include <bits/stdc++.h>20

C#

using using53

bool4 using55

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3bool3 bool8 bool using61int x, int #include <bits/stdc++.h>1

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

#include <bits/stdc++.h>6#include <bits/stdc++.h>4 #include <bits/stdc++.h>5

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>8

#include <bits/stdc++.h>3long int using2 using3

#include <bits/stdc++.h>6using5 #include <bits/stdc++.h>33

#include <bits/stdc++.h>6#include <bits/stdc++.h>35

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>38

#include <bits/stdc++.h>3namespace7

#include <bits/stdc++.h>42 #include <bits/stdc++.h>43

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

#include <bits/stdc++.h>6using95

#include <bits/stdc++.h>6using97

#include <bits/stdc++.h>6using99

#include <bits/stdc++.h>6namespace01

#include <bits/stdc++.h>3namespace7

namespace7

#include <bits/stdc++.h>3#include <bits/stdc++.h>4 #include <bits/stdc++.h>46#include <bits/stdc++.h>47#include <bits/stdc++.h>47 isPower(7#include <bits/stdc++.h>50

namespace05

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>53#include <bits/stdc++.h>47#include <bits/stdc++.h>47 isPower(7#include <bits/stdc++.h>20

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3using2 #include <bits/stdc++.h>47 isPower(7

#include <bits/stdc++.h>3using5 using6using2 #include <bits/stdc++.h>66

#include <bits/stdc++.h>6using2 #include <bits/stdc++.h>47 using2 #include <bits/stdc++.h>71 #include <bits/stdc++.h>72

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 using6using2 #include <bits/stdc++.h>47#include <bits/stdc++.h>47 #include <bits/stdc++.h>1

#include <bits/stdc++.h>3#include <bits/stdc++.h>87using6isPower(7#include <bits/stdc++.h>20

using using53

namespace7

bool4 using55

#include <bits/stdc++.h>3bool3 bool8 bool using61int x, int #include <bits/stdc++.h>1

#include <bits/stdc++.h>6#include <bits/stdc++.h>4 #include <bits/stdc++.h>5

#include <bits/stdc++.h>6int using76

namespace62

#include <bits/stdc++.h>3bool3 bool8 bool9 using91

namespace63

PHP

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

#include <bits/stdc++.h>6#include <bits/stdc++.h>4 #include <bits/stdc++.h>5

#include <bits/stdc++.h>6int using76

#include <bits/stdc++.h>6namespace76

#include <bits/stdc++.h>6using5 #include <bits/stdc++.h>33

#include <bits/stdc++.h>21#include <bits/stdc++.h>35

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 #include <bits/stdc++.h>38

#include <bits/stdc++.h>3namespace7

#include <bits/stdc++.h>6namespace88namespace89int2

namespace91namespace92namespace89int2

#include <bits/stdc++.h>6namespace96namespace89int2

#include <bits/stdc++.h>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 <bits/stdc++.h>1

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3int std;18std;19std;20____419std;22

#include <bits/stdc++.h>3std;24 std;25std;19std;20std;19std;29

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 std;32

namespace7

int namespace9

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3std;38

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 bool1

namespace7

Java

bool4 using55

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3bool8 #include <bits/stdc++.h>08 isPower(int std;51

std;52int #include <bits/stdc++.h>1

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

std;62using6intstd;65

#include <bits/stdc++.h>6std;24 std;68

std;69std;70

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 std;32

#include <bits/stdc++.h>3namespace7

#include <bits/stdc++.h>3bool3 bool8 bool9 std;80

#include <bits/stdc++.h>3#include <bits/stdc++.h>2

#include <bits/stdc++.h>6#include <bits/stdc++.h>4#include <bits/stdc++.h>81std;86isPower(6std;88__

#include <bits/stdc++.h>21std;91std;92int2

#include <bits/stdc++.h>6#include <bits/stdc++.h>91

#include <bits/stdc++.h>21std;91std;98int2

#include <bits/stdc++.h>3namespace7

namespace7

Python3

bool03 bool04

#include <bits/stdc++.h>42 bool06

#include <bits/stdc++.h>3bool08#include <bits/stdc++.h>47 bool10bool11bool11 bool13

#include <bits/stdc++.h>3bool15#include <bits/stdc++.h>47 bool10bool11 bool13

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 isPower(7 #include <bits/stdc++.h>4bool24__

#include <bits/stdc++.h>4 bool32#include <bits/stdc++.h>47#include <bits/stdc++.h>47bool35#include <bits/stdc++.h>92

#include <bits/stdc++.h>3#include <bits/stdc++.h>87#include <bits/stdc++.h>81std;86isPower(6std;88bool43

C#

using using53

bool4 using55

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3bool8 #include <bits/stdc++.h>08 isPower(int std;51

#include <bits/stdc++.h>2

std;52int #include <bits/stdc++.h>1

bool62using6intbool65

bool69bool70

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 std;32

namespace7

int namespace9

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3#include <bits/stdc++.h>4bool81

#include <bits/stdc++.h>6bool83std;92int2

#include <bits/stdc++.h>3#include <bits/stdc++.h>91

#include <bits/stdc++.h>6bool83std;98int2

namespace7

namespace7

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 bool1

namespace05

Java

#include <bits/stdc++.h>2

bool4 using55

#include <bits/stdc++.h>3bool8 #include <bits/stdc++.h>08 isPower(int std;51

std;52int #include <bits/stdc++.h>1

namespace7

namespace62

#include <bits/stdc++.h>6std;24 std;68

namespace63

#include <bits/stdc++.h>6#include <bits/stdc++.h>7 std;32

#include <bits/stdc++.h>2

#include <bits/stdc++.h>3bool3 bool8 bool9 std;80

bool62isPower(35

#include <bits/stdc++.h>6#include <bits/stdc++.h>4#include <bits/stdc++.h>81std;86isPower(6std;88__

bool69std;70

#include <bits/stdc++.h>3#include <bits/stdc++.h>7 std;32

namespace7

#include <bits/stdc++.h>4bool81

#include <bits/stdc++.h>3isPower(48std;92int2

#include <bits/stdc++.h>91

#include <bits/stdc++.h>3isPower(48std;98int2

std;03

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

#include <bits/stdc++.h>3#include <bits/stdc++.h>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 xem một số có phải là sức mạnh của một số python khác không?

Để kiểm tra xem một số nguyên đã cho là có sức mạnh của số nguyên đã cho không, hãy kiểm tra xem x> 0 && y % x == 0: y là sức mạnh lớn nhất của y có thể phù hợp với kiểu dữ liệu số nguyên.check if x > 0 && Y % x == 0 : Y is the largest power of y that can fit into an integer datatype.

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

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

Chủ đề