Advertisement
Naim19149

Integers_Recreation_One

Dec 7th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. '''
  2. PROBLEM: [From Codewars: https://www.codewars.com/kata/55aa075506463dac6600010d/train/python]
  3.  
  4. Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42.
  5. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764.
  6. The sum of the squared divisors is 2500 which is 50 * 50, a square!
  7.  
  8. Given two integers m, n (1 <= m <= n) we want to find all integers
  9. between m and n whose sum of squared divisors is itself a square.
  10. 42 is such a number.
  11.  
  12. The result will be an array of arrays or of tuples (in C an array of Pair)
  13. or a string, each subarray having two elements, first the number
  14. whose squared divisors is a square and then the sum of the squared divisors.
  15.  
  16. #Examples:
  17.  
  18. list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
  19. list_squared(42, 250) --> [[42, 2500], [246, 84100]]
  20.  
  21. '''
  22.  
  23.  
  24. def list_squared(m, n):
  25.     output = []
  26.     for num in range(m,n+1):
  27.         summ = sum([i*i for i in range(1,num+1) if num%i==0])
  28.         if (summ**.5)-int(summ**.5) == 0:
  29.             output.append([num,summ])
  30.         else:
  31.             continue
  32.     return output
  33.            
  34.  
  35. print(list_squared(42,250))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement