# Python Challenge - Sum of Prime Factors

This community-built FAQ covers the “Sum of Prime Factors” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the Python challenge Sum of Prime Factors

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def sum_of_prime_factors(n): def is_prime(num): flag = True cap = int(num**0.5) for i in range(2,cap+1): if num % i == 0: flag = False break return flag divisor_list = filter(lambda x:n%x==0,range(2,n+1)) prime_divisor_list = filter(is_prime, divisor_list) return sum(prime_divisor_list) print(sum_of_prime_factors(91))

This is the first thing I cam up with.

The is_prime function is simple. Mathematically, you only need to test for divisors up to the square root of a number to check if it is prime.

divisor_list uses filtering to make a list (really a filter object) of the non-trivial divisors.

prime_divisor_list then filters divisor_list through the is_prime function.

I’m sure there are better ways, but this works without going overboard with looping.

2 Likes

Nice one, @joshjones0410200536.

Took the tack of deconstruction for this one…

def sum_of_prime_factors(n): def is_prime(x): if x < 2: return False for h in range(2, int(x ** 0.5) + 1): if x % h == 0: return False return True if is_prime(n): return n factors = [] for x in range(2, n // 2): if is_prime(x): while n % x == 0: factors.append(x) n /= x print (factors) return sum(factors) print(sum_of_prime_factors(91)) print(sum_of_prime_factors(191)) print(sum_of_prime_factors(88)) print(sum_of_prime_factors(216))

Would sure love to know the two tests it is not passing, any ideas?

Hi,

Not sure if you’re still reading in this forum but I ran into they same problem with my code which only passed 3/5 tests at first. I compared the return values of my sum_of_prime_factors function to @joshjones0410200536 's and it seems the issue stems from ambiguous wording in the assignment. “`return the sum of all of its prime factors`” apparently means "add every prime factor to the sum exactly once, regardless of how many times it’s included in `n`" (e.g. `sopf(8) = 2 != 6`).

To fix this in your code, just change the `while` in line 11 to `if` and you should be good.

Cheers

1 Like

And that is that. Great help! Thanks!

1 Like