# Python Challenge - Sum of Prime Factors

def sum_of_prime_factors(n): # Write your code here # empty list initialisation factors = [] # loop that finds all factors for i in range(2, n+1): if n % i == 0: factors.append(i) # to print the list of factors # print(factors) # prime factor list initialisation prime_factors = [] # check if factor is prime for factor in factors: counter = 0 for j in range(1, factor+1): if factor % j == 0: counter += 1 if counter == 2: prime_factors.append(factor) # print(prime_factors) if n < 2: return 0 else: return sum(prime_factors) print(sum_of_prime_factors(91))
def prime(n): is_prime = True if n == 0 or n == 1: return False if n == 2: return True for i in range(2, (n//2)+1): if n % i == 0: is_prime = False break return is_prime def sum_of_prime_factors(n): if n == 2: return n if n == 0 or n == 1: return None if prime(n): return n prime_factors = list() for i in range(n//2, 1, -1): if prime(i) and n % i == 0: prime_factors.append(i) return sum(prime_factors) print(sum_of_prime_factors(91))

def sum_of_prime_factors(n):
sum = 0
for num in range(2,n+1):
for i in range (2,num):
if num % i == 0:
break
else:
if n % num == 0:
sum+= num
return sum

print(sum_of_prime_factors(91))

def sum_of_prime_factors(n): if n < 2: return 0 array = [0 for _ in range(n+1)] for number in range(2, n+1): if array[number] == 0: array[number] = number for i in range(2, n//number + 1): array[i*number] = number + array[i] k = number*number while k <= n: array[k] = number k *= number # print(array) # Uncomment to check array for errors. return array[n] print(sum_of_prime_factors(91))

Chances are you did not pass this challenge since the result when 36 is the argument should be a return of 5, not 8.

The problem said that all test cases passed when I submitted my solution, but you’re right, 36 should return 5. I added another `for` loop within the `while` loop that takes care of the powers of the prime factors that should fix that issue:

def sum_of_prime_factors(n): if n < 2: return 0 array = [0 for _ in range(n+1)] for number in range(2, n+1): if array[number] == 0: array[number] = number for i in range(2, n//number + 1): array[i*number] = number + array[i] k = number*number while k <= n: for j in range(1, n//k + 1): array[j*k] -= number k *= number # print(array) # Uncomment to check array for errors. return array[n] print(sum_of_prime_factors(91))
1 Like

def prime_checker(num):
if num < 2:
return False
i = 2
while i < num:
if num % i == 0:
return False
i += 1
return True

def prime_list(num):
lst =
for i in range(1, num+1):
prime = prime_checker(i)
if prime == True:
lst.append(i)
return lst

def is_a_prime_factor(num):
factor_list =
lst = prime_list(num)
for i in lst:
if num % i == 0:
factor_list.append(i)
return factor_list

def sum_of_prime_factors(n):
return sum(is_a_prime_factor(n))

print(sum_of_prime_factors(91))

from functools import reduce def is_prime(n): if n == 1: return False return not any(n % y == 0 for y in range(2, n)) def factors(n): return [(x, y) for x in range(n + 1) for y in range(n + 1) if x * y == n] def product(lst): return reduce(lambda x, y: x * y, lst) def sum_of_prime_factors(number, primes=None): if primes is None: primes = [number, []] for x, y in factors(number): if is_prime(x) and is_prime(y): primes[1] += [x, y] return sum(set(primes[1])) elif is_prime(x): primes[1].append(x) return sum_of_prime_factors(y, primes=primes) elif is_prime(y): primes[1].append(y) return sum_of_prime_factors(x, primes=primes) else: if product(primes[1]) == primes[0]: return sum(set(primes[1])) for x, y in factors(number): if is_prime(x) and is_prime(y): primes[1] += [x, y] return sum(set(primes[1])) elif is_prime(x): primes[1].append(x) return sum_of_prime_factors(y, primes=primes) elif is_prime(y): primes[1].append(y) return sum_of_prime_factors(x, primes=primes)
def prime_finder(x,y): for i in range (2,x): if x % i == 0 : return 0 if y % x == 0 : return x else : return 0 def sum_of_prime_factors(n): total = 0 for i in range(2,n+1): total = total + prime_finder(i,n) return total print(sum_of_prime_factors(91))

Decided to work my way down through factors and prime # possibilities. Would love feedback!

def sum_of_prime_factors(n): prime_factors= [] for i in range(2, n+1): #testing all #s if n % i ==0: #ensuring a factor for j in range(2, int(i **0.5 + 1)): if i%j == 0: #ensuring prime break else: prime_factors.append(i) return sum(prime_factors ) print(sum_of_prime_factors(91))
def sum_of_prime_factors(n): # Write your code here if n < 2: return 0 prime_factors = [] for i in range(2, n + 1): if n%i == 0: while n%i == 0: n = n/i prime_factors.append(i) sum = 0 for x in prime_factors: sum += x return sum print(sum_of_prime_factors(91)) print(sum_of_prime_factors(219))
def sum_of_prime_factors(n): prims = [] #print('The "{}" number can be divided into primes.'.format(n)) for i in range(2, n + 1): counter = 0 for k in range(1, i + 1): if i % k == 0: counter += 1 if counter == 2: prims.append(i) new_list = [] for number in prims: if n % number == 0: new_list.append(number) for n in new_list: #print("The 'prime factors' are as follows: ".format(n), new_list) #return 'The sum of these factors are: {}'.format(sum(new_list)) return sum(new_list) print(sum_of_prime_factors(91))

def sum_of_prime_factors(n):
prims =
#print(‘The “{}” number can be divided into primes.’.format(n))
for i in range(2, n + 1):
counter = 0
for k in range(1, i + 1):
if i % k == 0:
counter += 1
if counter == 2:
prims.append(i)
new_list =
for number in prims:
if n % number == 0:
new_list.append(number)
for n in new_list:
#print("The ‘prime factors’ are as follows: ".format(n), new_list)
#return ‘The sum of these factors are: {}’.format(sum(new_list))
return sum(new_list)
print(sum_of_prime_factors(91))

def sum_of_prime_factors(n):

list_of_factors =
list_of_prime_factors =
for num in range(2, n+1):
if n % num == 0:
list_of_factors.append(num)
for factor in list_of_factors:
prime = True
for i in range(2, factor):
if factor%i == 0:
prime = False
break
if prime is True:
list_of_prime_factors.append(factor)
sum = 0
for i in list_of_prime_factors:
sum += i
return sum

print(sum_of_prime_factors(91))

def prime(n): b = [] for i in range(2, n): if n % i == 0: b.append(i) #print(b) return False return True #print(prime(889)) def sum_of_prime_factors(n): # Write your code here b = [] c = [] d = list(range(1,n+1)) l = 0 #while l <1: for i in d: if n % i == 0: b.append(i) else: c.append(i) b.remove(1) for i in b: if prime(i): #print(i) l+=i return l #print(list(range(1,9))) print(sum_of_prime_factors(17))
def get_primes(n): divisors = [] a = n if n < 0: a = a*-1 divisors.append(-1) for i in range(2,int(a/2 + 1)): if a % i == 0: while a % i == 0: a = a / i divisors.append(i) else: continue if len(divisors) < 2 and a != 1: divisors.append(a) else: pass return divisors def sum_of_prime_factors(a): list= get_primes(a) solution = 0 for i in list: solution += i return solution #Write your query: print(sum_of_prime_factors(91)) print(sum_of_prime_factors(1))

I might be getting hooked on python challenges.

Would love some feedback on this approach!

[codebyte language=python] from functools import reduce def is_prime(n): if n == 1: return False return not any(n % y == 0 for y in range(2, n)) def factors(n): return [(x, y) for x in range(n + 1) for y in range(n + 1) if x * y == n] def product(lst): return reduce(lambda x, y: x * y, lst) def sum_of_prime_factors(number, primes=None): if primes is None: primes = [number, []] for x, y in factors(number): if is_prime(x) and is_prime(y): primes[1] += [x, y] return sum(set(primes[1])) elif is_prime(x): primes[1].append(x) return sum_of_prime_factors(y, primes=primes) elif is_prime(y): primes[1].append(y) return sum_of_prime_factors(x, primes=primes) else: if product(primes[1]) == primes[0]: return sum(set(primes[1])) for x, y in factors(number): if is_prime(x) and is_prime(y): primes[1] += [x, y] return sum(set(primes[1])) elif is_prime(x): primes[1].append(x) return sum_of_prime_factors(y, primes=primes) elif is_prime(y): primes[1].append(y) return sum_of_prime_factors(x, primes=primes) [/codebyte]
def sum_of_prime_factors(n): # Write your code here if n <2: return 0 count = 1 z = list(range(n+1)) prime_factors = [] factor = [] while n != count: count += 1 if (n % count) == 0: factor.append(count) for i in factor: counter = 0 for v in range(2,i): if (i % v) == 0: counter += 1 if counter == 0: prime_factors.append(i) x =0 for i in prime_factors: x += i return x print(sum_of_prime_factors(91))

Pity we cannot test this since it is impossible to know where the indentations occur. Have you read the part in the new user notes about how to format code samples in posts?

Don’t get us wrong, we know where the indentations belong, but why force us to edit your code just to test it, on the assumption that is how the code is actually written (our edited version)? If you won’t format the code you post in the forums, then just don’t post code. You’re not likely to get many useful replies.

``````# If n is a prime number greater than 2
``````

Recursive approach based on the fact that the first and lowest factor found will necessarily be prime (otherwise we would have came across its lowest factors before in the iteration)

def sum_of_prime_factors(n,sum=0): # searching for the lowest factor of n, which is prime for i in range(2, (n//2) +1): if n%i == 0: sum += i n = int(n/i) # recursive call of the function with updated parameters return sum_of_prime_factors(n,sum) # base case reached, we simplified n to its last prime factor sum += n return sum print(sum_of_prime_factors(91))