-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerSumRecursionV2.py
51 lines (43 loc) · 1.88 KB
/
PowerSumRecursionV2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 4 11:51:17 2018
@author: Ilja
"""
#import math
#import sys
import math
def powerSumRec(X, N, lowest=1, curIntAmount=1, maxIntAmount=math.inf, inPowSum=0, preLast=0, curLast=1, answer=0):
print()
print('input parameters:')
print('lowest=',lowest,'curIntAmount=',curIntAmount,'maxIntAmount=',maxIntAmount,'inPowSum=',inPowSum,'preLast=',preLast,'curLast=',curLast,'answer=',answer)
input()
if lowest**N > X:
print('lowest**N > X')
print('answer=',answer)
return answer
if lowest**N == X:
return answer + 1
else:
print(' inPowSum + (curLast)**N=',inPowSum + (curLast)**N)
if (inPowSum + (curLast)**N >= X):
print(' inPowSum + (curLast)**N >= X')
if inPowSum + (curLast)**N == X:
answer += 1
maxIntAmount = curIntAmount - 1
if (curIntAmount > 2):
return powerSumRec(X, N, lowest, curIntAmount-1, maxIntAmount, inPowSum - preLast**N, max(lowest,preLast-1), preLast+1, answer)
else:
return powerSumRec(X, N, lowest=lowest+1, curLast=lowest+1, answer=answer)
elif (inPowSum + curLast**N < X) and (curIntAmount < maxIntAmount):
print(' (inPowSum + curLast**N < X) and (curIntAmount < maxIntAmount)')
return powerSumRec(X, N, lowest, curIntAmount+1, maxIntAmount, inPowSum + curLast**N, curLast, curLast+1, answer)
elif (inPowSum + curLast**N < X) and (curIntAmount == maxIntAmount):
print(' (inPowSum + curLast**N < X) and (curIntAmount == maxIntAmount)')
return powerSumRec(X, N, lowest, curIntAmount, maxIntAmount, inPowSum, preLast, curLast+1, answer)
if __name__ == "__main__":
# X = int(input().strip())
# N = int(input().strip())
X=100
N=2
result = powerSumRec(X, N)
print(result)