Skip to content

Commit c8f232f

Browse files
author
Ravi Kandasamy Sundaram
committed
Hacktoberfest: project euler solution 119
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: #2695
1 parent 3324bbb commit c8f232f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Diff for: project_euler/problem_119/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_119/sol1.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
"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 83 = 512. Another example of a number
8+
with this property is 614656 = 284. 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+
14+
import math
15+
16+
17+
def digit_sum(n: int) -> int:
18+
"""
19+
Returns the sum of the digits of the number
20+
"""
21+
sum = 0
22+
while n > 0:
23+
sum += n % 10
24+
n //= 10
25+
return sum
26+
27+
28+
def solution() -> int:
29+
"""
30+
Returns the value of a30
31+
"""
32+
powers = []
33+
for i in range(2, 100):
34+
for j in range(2, 100):
35+
n = int(math.pow(i, j))
36+
if i == digit_sum(n):
37+
powers.append(n)
38+
39+
powers.sort()
40+
return powers[29]
41+
42+
43+
if __name__ == "__main__":
44+
print(solution())

0 commit comments

Comments
 (0)