IT45200

Untitled

Feb 12th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import math
  2.  
  3. def isperfectsquare(n):
  4.  
  5.     if int(math.sqrt(n) + 0.5) ** 2 == n:
  6.         return True
  7.     else:
  8.         return False
  9.    
  10. def compositions(n,k):
  11.     if n < 0 or k < 0:
  12.         return
  13.     elif k == 0:
  14.         if n == 0:
  15.             yield []
  16.         return
  17.     elif k == 1:
  18.         yield [n]
  19.         return
  20.     else:
  21.         for i in range(0,n+1):
  22.             for comp in compositions(n-i,k-1):
  23.                 yield [i] + comp
  24.                
  25. def threesquares(n):
  26.     ps = False
  27.     result = False
  28.     for c in list(compositions(n,3)):
  29.         for x in c:
  30.             if isperfectsquare(int(x)):
  31.                 ps = True
  32.             else:
  33.                 ps = False
  34.                 break
  35.         if ps:
  36.             result = True
  37.             break
  38.         else:
  39.             result = False
  40.     print(result)
  41.              
  42. threesquares(6)
  43. threesquares(188)
  44. threesquares(1000)
Advertisement
Add Comment
Please, Sign In to add comment