Pella86

Untitled

Oct 3rd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3. def sum_squared(nlist, power):
  4.     mysum = 0
  5.     for x in nlist:
  6.         mysum += x ** power
  7.     return mysum
  8.    
  9.  
  10.  
  11. def power(x, n, counter_list , solutions_list):
  12.     counter = int(x ** (1/n))
  13.    
  14.     mysum = sum_squared(counter_list, n)
  15.    
  16.     if mysum == x:
  17.         solution_list = deepcopy(counter_list)
  18.         return solution_list
  19.     else:
  20.         old_counter = deepcopy(counter_list)
  21.        
  22.         i = counter
  23.  
  24.         while len(solutions_list) == 0 and i >= 0 and mysum <= x and len(counter_list) <= x:
  25.            
  26.             if i  == 0 or (counter_list and i not in counter_list and i > max(counter_list)):
  27.  
  28.                 counter_list.append(i)
  29.                
  30.                 solutions_list = power(x, n, counter_list, solutions_list)
  31.                
  32.                 counter_list = deepcopy(old_counter)
  33.  
  34.             i -= 1
  35.        
  36.         return solutions_list
  37.        
  38. for x in range(20):
  39.     for n in range(5):
  40.         print("x, n", x + 1, n + 1)
  41.         sl = power(x + 1, n + 1, [], [])
  42.         print(sl)
  43.         print(len(sl) - 1)
Advertisement
Add Comment
Please, Sign In to add comment