Throw std::logic_error ("should not happen") If (quo <= d) return true // I can quit now! rely on compiler realizing that simultaneous div and mod is one op You only need to test prime numbers up to √n, which is much fewer! So, note each found prime in a vector, and have isPrime consult the list thus far to do its job! for ( ⋯ When you test isPrime, you test every odd number less than n. Modulo is very slow and jams up the CPU so it is even slower than you would think! Minimizing the number of tests will give you big benefits. But you do want to keep a list of all the primes! See below. accumulate sums the values, but generating the list of primes might be a bit tricky. You do not have to write that loop at all, if you use std algorithms. You do not need to check that against true again. Performance suggestions: see next section And, good practice is to define these as constexpr. You can use separators in numbers for readability now. Put main last, so you don’t have to forward-declare your functions. You can, however, in a CPP file (not H file) or inside a function put individual using std::string etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |