-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegers.py
30 lines (21 loc) · 1.24 KB
/
integers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Integers: Recreation one, level 5kyu.
# link: https://www.codewars.com/kata/55aa075506463dac6600010d
'''
Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!
Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.
The result will be an array of arrays or of tuples (in C an array of Pair) or a string, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.
#Examples:
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
'''
import numpy as np
def list_squared(m, n):
square_list = []
for x in np.arange(m,n+1):
int_list = np.arange(1,x+1)
div_list = int_list[x%int_list==0] # get an array of divisors
sq = sum((div_list)**2) # get the sum
root = np.sqrt(sq)
if int(root + 0.5) ** 2 == sq: # check if the number a perfect square
square_list.append([x,sq])
return square_list