answersLogoWhite

0

#include<iostream> // std::cout and std::endl

#include<cmath> // std::sqrt

// returns the next Prime number after the given numberunsigned next_prime (unsigned num) {

if (num<2) return 2;

++num;

unsigned max_factor {std::sqrt{num) + 1U};

for (unsigned factor=2; factor<max_factor; ++factor)

if (num%factor==0) return next_prime (num);

return num;

}

// print prime numbers to 1000

int main()

{

unsigned num=0;

while ((num=next_prime(num))<1000) std::cout<<num<<std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?