Skip to content

Commit 2b5b2c6

Browse files
ksr1122Ravi Kandasamy Sundaram
and
Ravi Kandasamy Sundaram
authored
Added solution for Project Euler problem 119 (TheAlgorithms#2931)
Name: Digit power sum Problem Statement: The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284. We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum. You are given that a2 = 512 and a10 = 614656. Find a30 Reference: https://projecteuler.net/problem=119 reference: TheAlgorithms#2695 Co-authored-by: Ravi Kandasamy Sundaram <[email protected]>
1 parent 501a2ff commit 2b5b2c6

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

project_euler/problem_119/__init__.py

Whitespace-only changes.

project_euler/problem_119/sol1.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Problem 119: https://projecteuler.net/problem=119
3+
4+
Name: Digit power sum
5+
6+
The number 512 is interesting because it is equal to the sum of its digits
7+
raised to some power: 5 + 1 + 2 = 8, and 8^3 = 512. Another example of a number
8+
with this property is 614656 = 28^4. We shall define an to be the nth term of
9+
this sequence and insist that a number must contain at least two digits to have a sum.
10+
You are given that a2 = 512 and a10 = 614656. Find a30
11+
"""
12+
13+
import math
14+
15+
16+
def digit_sum(n: int) -> int:
17+
"""
18+
Returns the sum of the digits of the number.
19+
>>> digit_sum(123)
20+
6
21+
>>> digit_sum(456)
22+
15
23+
>>> digit_sum(78910)
24+
25
25+
"""
26+
return sum([int(digit) for digit in str(n)])
27+
28+
29+
def solution(n: int = 30) -> int:
30+
"""
31+
Returns the value of 30th digit power sum.
32+
>>> solution(2)
33+
512
34+
>>> solution(5)
35+
5832
36+
>>> solution(10)
37+
614656
38+
"""
39+
digit_to_powers = []
40+
for digit in range(2, 100):
41+
for power in range(2, 100):
42+
number = int(math.pow(digit, power))
43+
if digit == digit_sum(number):
44+
digit_to_powers.append(number)
45+
46+
digit_to_powers.sort()
47+
return digit_to_powers[n - 1]
48+
49+
50+
if __name__ == "__main__":
51+
print(solution())

0 commit comments

Comments
 (0)