// ----------------------------------------------------- // System Headers // ----------------------------------------------------- #include #include // ----------------------------------------------------- // Using Directives // ----------------------------------------------------- using std::vector; // ----------------------------------------------------- // setValues - Set 'values[current]' to false and then // continue onward recursively (moving // 'current' forward by 'step' each time) // until 'values' has been exhausted // ----------------------------------------------------- static void setValues(vector &values, unsigned step, unsigned current) { if(current < values.size()) { values[current] = false; setValues(values, current + step, step); } } // ----------------------------------------------------- // showValues - If 'values[number]' is true, display // 'number' then call setValues to nullify // all multiples of 'number' and then // 'showValues' again recursively until // 'values' has been exhausted // ----------------------------------------------------- static void showValues(std::vector &values, unsigned number = 2) { if(values[number]) std::cout << number << "\n"; setValues(values, number, 2 * number); if(++number < values.size()) showValues(values, number); } // ----------------------------------------------------- // main // ----------------------------------------------------- int main() { // ----------------------------------------------------- // Show all primes up to 'N' without using any loops, // division or modulus operations // ----------------------------------------------------- static const int N = 1000; vector values(N, true); showValues(values); return(0); }