Task:
Find all integers between m and n (m and n integers with 1 <= m <= n) such that the sum of their squared divisors is itself a square.
We will return an array of subarrays or of tuples (in C an array of Pair) or a string. The subarrays (or tuples or Pairs) will have two elements: first the number the squared divisors of which is a square and then the sum of the squared divisors.
Example:
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
Solution:
def divisors(n):
divs = [1, n]
for i in range(2, int(n**0.5)+1):
if n % i == 0:
divs.extend([i, int(n/i)])
return set(divs)
def list_squared(m, n):
sq_list = []
for num in range(m, n):
_sum = sum(item**2 for item in divisors(num))
if (_sum ** 0.5).is_integer():
sq_list.append([num, _sum])
return sq_list
print(list_squared(42,250))
Integers: Recreation One Solutions | Codewars
No comments :
Post a Comment