Hướng dẫn sum of power of digits in python wipro - tổng sức mạnh của các chữ số trong python wipro

Cải thiện bài viết

Lưu bài viết

Đưa ra một số, chúng ta cần tìm tổng của tất cả các chữ số của một số mà chúng ta nhận được sau khi nâng số lên một nguồn được chỉ định.
Examples: 
 

Input: number = 5, power = 4 
Output: 13
Explanation:
Raising 5 to the power 4 we get 625.
Now adding all the digits = 6 + 2 + 5


Input: number = 9, power = 5
Output: 27
Explanation:
Raising 9 to the power 5 we get 59049.
Now adding all the digits = 5 + 9 + 0 + 4 + 9

Cách tiếp cận cho Python được giải thích. Chúng tôi đã sử dụng hàm pow [] để tính toán cơ sở với giá trị công suất. Sau đó, chúng tôi đã trích xuất mọi chữ số dưới dạng chuỗi bằng phương thức str []. Vì chúng tôi có thể tính toán tổng các chuỗi, chúng tôi đã chuyển đổi mọi chữ số chuỗi trở lại số nguyên bằng phương thức int []. Cuối cùng, chúng tôi đã sử dụng hàm SUM [] để có được tổng của tất cả các chữ số. Giải pháp này sẽ trông rất đơn giản trong Python nhưng nó đã thắng rất ngắn trong các ngôn ngữ khác. Sau khi chạy cả hai mã, người ta có thể so sánh thời gian trôi qua và bộ nhớ được sử dụng trong cả ngôn ngữ đã cho, tức là, Python và Java. & NBSP; bên dưới là việc thực hiện ý tưởng trên: & nbsp;
Below is the implementation of above idea :
 

C++

#include

using namespace std;

int calculate[int n, int

13
1

13
2

13
3int
13
5

13
3int
13
8int#include0#include1#include2

13
3#include4 #include5

#include6int #include8

#include6using0

#include6using2

13
3using4

13
3using6 using7

using4

int namespace0

13
2

13
3int namespace4

13
3int namespace7

13
3namespace9

using4

Java

std;1 std;2 std;3

13
3std;5 int calculate[int n, int
13
1

13
3
13
2

#include6int int6int7int8

#include6int

13
8intcalculate[3

#include6#include4 calculate[6int7calculate[8

calculate[9int int1int22

calculate[9using0

calculate[9int7int2int8

#include6using4

#include6using6 using7

13
3using4

13
3std;1 std;5
13
00
13
01

13
3
13
2

#include6int

13
06
13
07int8

#include6int

13
11
13
122

#include6

13
15

13
3using4

using4

Python3

13
19
13
20

13
3using6
13
23__

13
34
13
35
13
07

13
37
13
35
13
12

13
40
13
41

C#

using

13
43

std;1 std;2

13
46

13
2

13
3std;5 int calculate[int n, int
13
1

13
3
13
2

#include6int int6int7int8

#include6int

13
8intcalculate[3

#include6#include4 calculate[6int7calculate[8

#include6

13
2

calculate[9int int1int22

calculate[9using0

calculate[9using2

#include6using4

#include6using6 using7

13
3using4

13
3using4

13
3
13
2

13
3std;1 std;5
13
00
13
01

#include6int

13
06
13
07int8

#include6

13
99

13
3using4

using4

#include6int
13
11
13
122

#include03

13
19
13
20

13
2

13
3using6
13
23__

13
34
13
35
13
07

13
37
13
35
13
12

13
3
13
2

13
40
13
41

using

13
43

std;1 std;2

13
46

13
3using4

#include6int

13
5

using4

#include6int

13
8int
13
65

#include6#include4

13
68

#include52#include53#include06#include07#include08#include57

#include58

calculate[9int #include8

#include59

13
3std;1 std;5
13
00
13
89

13
2

#include64

13
5

#include64#include67

13
3#include4 #include5

#include71#include8

#include6#include74

#include6#include76

13
3using4

13
3using6 using7

using4

#include64namespace4

#include64namespace7

13
3#include88

#include89

Output: 

13

int namespace0Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


Bài Viết Liên Quan

Chủ Đề