although this STILL does not work. at all. the code compiles with no errors or warnings in TDM-GCC using C++ 11 standards, but crashes immediately. |
Your array of primes is not initialised at any point (lines 15 and 29 cause all the logic to be missed, all the values a re garbage, not 0 or 1) So that is one big golden rule : "Always initialise your variable to something" :+)
If you want to use the largest possible numbers, consider
std::size_t
, it is usually the largest
unsigned type, on a 64 bit machine it can go up 2
64-1. Making a type unsigned doubles the extent of the numbers it can store.
I don't see where you are calculating primes. There are some clever ways of doing that. Have a look at the wiki page, there is
Euler's :+) method near the bottom. Just don't do trial division. Remember Google and wiki are your friends :+) There is a quick algorithm which involves
bitset
With your limit of
sqrt(Base)
, that is fine for working out primes. If one has removed all the multiples up to
sqrt(Base)
, then all the numbers left up to
Base
will be prime. But realise you are after
factors which are prime. For example 2 * 47 = 94, so there are factors that are bigger than sqrt(Base), if Base was 94 in this example.
The other big thing to remember about PE is that brute force is often not the answer, one has to come with a cunning plan. PE is all about clever algorithms.
In terms of publishing your working code, I wouldn't worry about that too much, especially for the low numbered problems, squillions of people have already completed them. Like any cheating, if one doesn't understand the early questions given the answer, then they won't have a hope with the later questions.
Good Luck !!