# Sum by Factors

From Codewars.com:

Given an array of positive or negative integers

`I= [i1,..,in]`

you have to produce a sorted array P of the form

`[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]`

P will be sorted by increasing order of the prime numbers. The final result has to be given as a string in Java, C# or C++ and as an array of arrays in other languages.

Example:

`I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]`

[2, 3, 5] is the list of all prime factors of the elements of I, hence the result.

Notes:It can happen that a sum is 0 if some numbers are negative!

Example: I = [15, 30, -45] 5 divides 15, 30 and (-45) so 5 appears in the result, the sum of the numbers for which 5 is a factor is 0 so we have [5, 0] in the result amongst others.

This was a bit of a doozy

def sum_for_list(lst):

# largest = find largest abs value in lst

def simple_abs(num):

return -num if num < 0 else num

abs_value_lst = []

for i in lst:

value = simple_abs(i)

abs_value_lst.append(value)

largest = max(abs_value_lst)

# p = [generate prime numbers somehow], from 2 to largest

p = []

count = 2

while count < largest:

isprime = True

for x in range(2, int((count/2) + 1)):

if count % x == 0:

isprime = False

break

if isprime:

p.append(count)

count += 1

# sum_list = factor, sum

sum_list = []

sum = 0

counter = 0

#loop through prime array

for j in p:

sum = 0

counter = 0

for i in lst:

# loop through lst

#if not lst[i] % p: (is divisible)

if not i % j:

sum += i

counter += 1

# sum += lst[i]

#sum_list.append([p, sum])

if counter:

sum_list.append([j, sum])

#return list

return sum_list