@
Cheech0
There is a detailed explanation here:
http://math.stackexchange.com/questions/9259/find-the-sum-of-all-the-multiples-of-3-or-5-below-1000
From what I can gather brute force isn't awful, but I should be working on alternative methods that stimulate my mind. |
Hopefully we can all agree not to do naive solutions. Any logic or theory one can use to reduce the problem, is going to be an improvement.
Just on that, prime numbers are very near in the Euler landscape. All the time we see beginners using a naive solution of doing trial division - that is, for all the numbers in the set, seeing if
every number before it is a factor. So this is terrible when one has to come up with a list of all prime numbers up 2 million ! It's a lot easier to start out with all the numbers up to some limit, remove multiples of 2; the next number is 3, so remove multiples of that; next are 5, 7, 11, 13 and so on. There are other improvements, you can read about them on the wiki page. Wait for it ...... it's called Euler's algorithm ! Next is what container to store the numbers in? One can use an array (or some other container) of true false values, the subscript to the array is the number, set to true if it's a prime. The
std::bitset
is an array of bits, rather than integers - so this is smaller storage.
One other thing: Are you aware of the tutorial, reference material and articles on this site? There are links at the top left of the page. A lot of people seem to miss that somehow :+)
JLBorges wrote: |
---|
The hardest instances of these problems (for currently known techniques) are semi-primes, the product of two prime numbers. |
Ok, thanks, I will remember that :+). I should have said "a bad" rather than "the worst" scenario. By "large" I gather that could mean numbers with thousands of digits. So this is now very far from the example I gave earlier.