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#, C, C++ and as an array of arrays in other languages.

Example:

```python
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. 

- In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.

Source: codewars | Difficulty: MEDIUM
Tags: Data structures

Mathematics

Starter code: def sum_for_list(lst):
	

Solutions:
def sum_for_list(lst):
	factors = {i for k in lst for i in xrange(2, abs(k) + 1) if not k % i}
	prime_factors = {i for i in factors if not [j for j in factors - {i} if not i % j]}
	return [[p, sum((e for e in lst if not e % p))] for p in sorted(prime_factors)]


from collections import defaultdict

def prime(x):
	for i in xrange(2, x):
		if x % i == 0:
			return False
	return True

def prime_dividers(a):
	return (x for x in xrange(2, abs(a) + 1) if a % x == 0 and prime(x))

def sum_for_list(lst):
	d = defaultdict(int)
	for a in lst:
		for div in prime_dividers(a):
			d[div] += a
	return sorted([list(item) for item in d.iteritems()])


from collections import defaultdict

def sum_for_list(lst):

	def factors(x):
		p_facs = []
		i = 2
		while x > 1 or x < -1:
			if x % i == 0:
				p_facs.append(i)
				x //= i
			else:
				i += 1
		return list(set(p_facs))
	fac_dict = defaultdict(int)
	for i in lst:
		for fac in factors(i):
			fac_dict[fac] += i
	return sorted([[k, v] for (k, v) in fac_dict.items()])


def sum_for_list(lst):

	def buscar_primxs(x):
		res = []
		x = abs(x)
		for i in range(2, int(x / 2) + 1):
			if x // i * i == x:
				if i == 2 or i == 3:
					res.append(i)
				else:
					c = 0
					for j in range(2, i):
						if i // j * j == i:
							c = c + 1
					if c == 0:
						res.append(i)
		if res == []:
			return [x]
		return res
	R = {}
	for x in lst:
		for elements in buscar_primxs(x):
			if elements in R:
				R[elements] += x
			else:
				R[elements] = x
	rta = []
	M = [x for x in R]
	M = sorted(M)
	for keys in M:
		rta.append([keys, R[keys]])
	return rta


from collections import defaultdict

def sum_for_list(lst):
	primes = [2]
	factors_dict = defaultdict(list)
	answer = []
	x = 3
	for num in range(max((abs(i) for i in lst))):
		if all((x % d for d in range(3, int(x ** 0.5) + 1, 2))):
			primes += [x]
		x += 2
	for k in primes:
		factors_dict[k]
	for prime in primes:
		for i in lst:
			if i % prime == 0:
				factors_dict[prime] += [i]
	for (k, v) in list(factors_dict.items()):
		if v:
			answer += [[k, sum(v)]]
	return answer


def sum_for_list(lst):
	r = []
	for i in lst:
		r.append(abs(i))
	k = []
	for i in r:
		l = []
		for j in range(2, i + 1):
			if i % j == 0:
				l.append(j)
		for x in l:
			g = []
			for y in range(1, x + 1):
				if x % y == 0:
					g.append(y)
			if g[0] == 1 and g[1] == x:
				k.append(x)
	s = set(k)
	p = []
	for i in s:
		o = [i]
		a = 0
		for j in lst:
			if j % i == 0:
				a = a + j
		o.append(a)
		p.append(o)
	p.sort()
	return p


from itertools import chain
from collections import defaultdict

def sum_for_list(lst):
	sums = defaultdict(int)
	for n in lst:
		m = n
		for p in chain([2], xrange(3, abs(n) + 1, 2)):
			if m % p == 0:
				sums[p] += n
			while m % p == 0:
				m //= p
	return map(list, sorted(sums.items()))


def factors(n):
	gaps = [1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6]
	(length, cycle) = (11, 3)
	(f, fs, nxt) = (2, [], 0)
	while f * f <= n:
		while n % f == 0:
			fs.append(f)
			n /= f
		f += gaps[nxt]
		nxt += 1
		if nxt == length:
			nxt = cycle
	if n > 1:
		fs.append(n)
	return fs

def sum_for_list(lst):
	d = {}
	fList = []
	for i in lst:
		f = factors(abs(i))
		f = list(set(f))
		if i < 0:
			print((i, f))
		for n in f:
			if n in d:
				d[n].append(i)
			else:
				d[n] = [i]
	r = []
	for i in sorted(d):
		r.append([int(i), sum(d[i])])
	return r


from functools import reduce

def sum_for_list(lst):
	factors = []
	for j in lst:
		for k in [i for i in range(2, abs(j) + 1) if j % i == 0]:
			if factors == [] or reduce(lambda x, y: x and y, [k % m != 0 for m in factors]):
				factors.append(k)
	return [[j, sum([l for l in lst if l % j == 0])] for j in sorted(factors)]


import subprocess
from itertools import chain

def prime_factors(n):
	out = subprocess.run(['factor', str(n)], stdout=subprocess.PIPE)
	out = str(out).split(':')[1].split('\\n')[0].split()
	return [int(s) for s in out]

def sum_for_list(L):
	P = list(chain((prime_factors(abs(n)) for n in L)))
	zP = list(zip(L, P))
	sP = sorted(set((p for l in P for p in l)))
	return [[p, sum((e[0] for e in zP if p in e[1]))] for p in sP]