Project Euler第23题Java实现:接近正确解,求解题方向
Hey there! Let's dig into why your result (4190404) is off from the correct answer—first, know that it's super common to hit snags even with algorithms that worked for previous problems, so you're not alone here.
First, let's recap the key points of Problem 23 to ground our debugging: we need the sum of all positive integers ≤28123 that cannot be written as the sum of two abundant numbers. The correct result is 4179871, so your answer is higher than expected—this means some numbers that should be marked as expressible as two abundant sums are slipping through the cracks, getting included in your final sum.
Here are the most likely areas to check, based on your description:
1. Double-check your abundant number logic
Your divisor sum algorithm worked for Problem 21, but let's confirm it's behaving correctly for abundant numbers specifically:
- An abundant number is defined as a number where the sum of its proper divisors (excluding the number itself) is greater than the number. For example, 12's proper divisors sum to 1+2+3+4+6=16, which is >12—so 12 is abundant.
- Verify a few edge cases: is 1 correctly identified as non-abundant? Is 18 (sum=1+2+3+6+9=21>18) in your abundant list?
- Make sure you're generating all abundant numbers ≤28123—missing even one could leave gaps in your sum checks.
2. Fix the "sum of two abundants" marking logic
This is the most common pain point for this problem. Let's break down potential issues:
- Did you allow using the same abundant number twice? For example, 24=12+12—if your code only pairs distinct abundant numbers, you'll miss these cases, leaving numbers like 24 unmarked and included in your final sum (which would inflate your total).
- Is your boolean array sized correctly? You need an array of size 28124 (since we're checking numbers from 1 to 28123) initialized to
false. For every pair of abundant numbersaandbwherea + b ≤28123, setarray[a+b] = true. - Are you iterating through all possible pairs? A common optimization is to loop
afrom the start of your abundant list, then loopbstarting ata(to avoid redundant checks), but even if you loop all pairs, just make sure you don't skip any combinations that add up to ≤28123.
3. Verify your final summation step
- Ensure you're only summing numbers from 1 to 28123 where the boolean flag is
false(i.e., cannot be written as two abundant sums). It's easy to accidentally include numbers beyond 28123, or skip the lower end of the range.
Quick Debugging Tip
Test your code on a smaller range to isolate issues. For example, check numbers 1-30 manually:
- Abundant numbers here are 12,18,20,24
- Sums of two abundants: 24(12+12), 30(12+18)
- So the sum of non-expressible numbers 1-30 should be sum(1-23) + 25+26+27+28+29 = 411. Run your code on this small range and see if you get 411—if not, you'll know exactly where your logic is breaking.
Hope these pointers help you track down the bug! If you share more of your code, we can dive even deeper.
内容的提问来源于stack exchange,提问作者kiel dowdle

