def problem(n):
myList = []
for i in range(2, n):
if n % i == 0:
myList.append(i)
return myList
with this code I was wondering how you would get the factors for example 12 to print out as[[6,2],[3,4]] something like this dosnt have to be in same order thanks.
This should work for you:
import math
def problem(n):
myList = []
for i in range(2, int(math.sqrt(n) + 1)):
if n % i == 0:
myList.append([i, int(n/i)])
return myList
To get the factor pair, this divides n by i, if i is a factor, which will by i's pair.
example:
print(problem(12)) #output: [[2, 6], [3, 4]]
Another way. Loop using range and check if is_integer
num = 12
set([tuple(sorted(j)) for j in [[i, int(num/i)] for i in range(2,num) if (num/i).is_integer()]]
)
#Output:
#{(2, 6), (3, 4)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With