Write a C++ program to print the first 10,000 prime numbers.
- User
Write a C++ program to print the first 10,000 prime numbers.
- Assistant
Sure! Here's a C++ program that prints the first 10,000 prime numbers:
#include <iostream> #include <vector> bool isPrime(int num) { if (num < 2) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } int main() { std::vector<int> primes; int count = 0; int num = 2; while (count < 10000) { if (isPrime(num)) { primes.push_back(num); count++; } num++; } for (int prime : primes) { std::cout << prime << " "; } return 0; }This program uses a helper function
isPrimeto check if a number is prime or not. It starts with an empty vectorprimesto store the prime numbers and a countercountto keep track of the number of prime numbers found. It then uses a while loop to iterate untilcountreaches 10,000. In each iteration, it checks if thenumis prime using theisPrimefunction, and if it is, it adds it to theprimesvector and increments thecountvariable. Finally, it prints all the prime numbers stored in theprimesvector.Note that finding the first 10,000 prime numbers can be a computationally expensive task, so the program might take some time to run.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.