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.
Ask or answer a question about this exercise by clicking reply () below!
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
Learn more about how to use this guide.
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!
flag = True
cap = int(num**0.5)
for i in range(2,cap+1):
if num % i == 0:
flag = False
divisor_list = filter(lambda x:n%x==0,range(2,n+1))
prime_divisor_list = filter(is_prime, divisor_list)
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.
Nice one, @joshjones0410200536.
Took the tack of deconstruction for this one…
if x < 2: return False
for h in range(2, int(x ** 0.5) + 1):
if x % h == 0: return False
if is_prime(n): return n
factors = 
for x in range(2, n // 2):
while n % x == 0:
n /= x
Would sure love to know the two tests it is not passing, any ideas?
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
sopf(8) = 2 != 6).
To fix this in your code, just change the
while in line 11 to
if and you should be good.
And that is that. Great help! Thanks!