Skip to content

Commit aed04da

Browse files
committed
recursion
1 parent 51ede39 commit aed04da

File tree

7 files changed

+407
-51
lines changed

7 files changed

+407
-51
lines changed

python/PowerSum/.idea/PowerSum.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/PowerSum/.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/PowerSum/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/PowerSum/.idea/workspace.xml

Lines changed: 211 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/PowerSum/solver/PowerSum.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'''
2+
https://www.hackerrank.com/challenges/the-power-sum/problem
3+
'''
4+
5+
'''
6+
De quantas formas e possivel escrever um numero M
7+
na forma de soma de potencias N
8+
'''
9+
10+
11+
def top_down(base, exp, acc=1):
12+
X = acc ** exp
13+
if X > base:
14+
return 0
15+
elif X == base:
16+
return 1
17+
u = top_down(base, exp, acc + 1)
18+
v = top_down(base - X, exp, acc + 1)
19+
return u + v
20+
21+
22+
def another_top_down(base, exp, acc=1):
23+
x = base - (acc ** exp)
24+
if x < 0: # se acc ^ exp > que a base x < 0 portanto acc ^ exp nao faz parte da solucao
25+
return 0
26+
elif x == 0: # acc ^ x == base entao base - (acc ^ x) == 0 logo acc ^ x faz parte da solucao
27+
return 1
28+
u = another_top_down(base, exp, acc + 1) # sem adicionar acc ^ exp a solucao
29+
v = another_top_down(x, exp, acc + 1) # adicionando acc ^ exp a solucao
30+
return u + v
31+
32+
33+
def bottom_up_approach(base, exp):
34+
from math import sqrt
35+
acc = 1
36+
table = [0] * (base + 1)
37+
for i in range(2, int(sqrt(base) + 1)):
38+
x = acc ** i
39+
acc += 1
40+
41+
return table[base - 1]
42+
43+
44+
'''
45+
E se eu quisesse saber de quantas formas e possivel escrever
46+
um numero como potencias de 1 a P
47+
'''
48+
49+
50+
def run():
51+
n = int(input())
52+
p = int(input())
53+
print(another_top_down(n, p))
54+
55+
56+
def test():
57+
print(top_down(10, 2), another_top_down(10, 2))
58+
print(top_down(4, 2), another_top_down(4, 2))
59+
print(top_down(4, 1), another_top_down(4, 1))
60+
print(top_down(10, 1), another_top_down(10, 1))
61+
print(top_down(100, 3), another_top_down(100, 3))
62+
print(top_down(100, 2), another_top_down(100, 2))
63+
64+
65+
test()
66+
67+
if __name__ == '__main__':
68+
pass

python/PowerSum/solver/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)