Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def isperfectsquare(n):
- if int(math.sqrt(n) + 0.5) ** 2 == n:
- return True
- else:
- return False
- def compositions(n,k):
- if n < 0 or k < 0:
- return
- elif k == 0:
- if n == 0:
- yield []
- return
- elif k == 1:
- yield [n]
- return
- else:
- for i in range(0,n+1):
- for comp in compositions(n-i,k-1):
- yield [i] + comp
- def threesquares(n):
- ps = False
- result = False
- for c in list(compositions(n,3)):
- for x in c:
- if isperfectsquare(int(x)):
- ps = True
- else:
- ps = False
- break
- if ps:
- result = True
- break
- else:
- result = False
- print(result)
- threesquares(6)
- threesquares(188)
- threesquares(1000)
Advertisement
Add Comment
Please, Sign In to add comment