Ok, so I'm fairly new to c++ and this is my first time posting to this site. My problem is that my program needs to (1) find the first 100 prime numbers (not prime numbers between 1 and 100) and (2) find and print the 10th, 100th, 1000th,..., 1,000,000th primes without print the ones in between in this format:
*EDIT*
for some reason, this ^ isn't formatted the way it needs to be in the program so I'll just type it out...it is in columns with the width set to 9.
here is my code so far...i feel like I'm pretty close, but my math is off or something. Instead of returning primes, it returns 2 followed by all odd numbers up to 199 and the part 2 is completely off (because the function isPrime isn't working properly).
// Nate Hoover
// Primes
// Sources:
// This program will find the first 100 primes and the 10, 100, 10000, 100000, 1000000th primes
#include<iostream>
#include<iomanip>
usingnamespace std;
// function prototype
bool isPrime(int n); // Returns true if n is a prime, else false
int main() {
// declare variables
int count = 0, current_number = 2;
// welcome user
cout << "Welcome! The first 100 prime numbers are:" << endl;
// find the first 100 prime numbers
while (count < 100) {
if (isPrime(current_number)) { // call current number to the isPrime function
cout << current_number << endl; // print prime numbers to screen
count++; // add to the counter only if the number is prime
}
current_number++;
}
// blank line to seperate parts 1 and 2
cout << endl;
// initialize the columns
cout << setw(9) << left << "n:" << setw(9) << "nth prime:" << endl;
// set count back to 0
count = 0;
// find the primes at the nth position (multiples of 10)
while (count <= 1000000) {
if (isPrime(current_number)) { // call current number to the isPrime function
// switch loop to print nth prime
switch (count){
case 10:
cout << setw(9) << left << count << setw(9) << current_number << endl;
break;
case 100:
cout << setw(9) << left << count << setw(9) << current_number << endl;
break;
case 1000:
cout << setw(9) << left << count << setw(9) << current_number << endl;
break;
case 10000:
cout << setw(9) << left << count << setw(9) << current_number << endl;
break;
case 100000:
cout << setw(9) << left << count << setw(9) << current_number << endl;
break;
case 1000000:
cout << setw(9) << left << count << setw(9) << current_number << endl;
}
count++; // add to the counter only if the number is prime
}
current_number++;
}
// pause and exit
getchar();
getchar();
return 0;
}
// isPrime function to find if number is prime
bool isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
returnfalse; // tells the program if the number is not prime
}
else {
returntrue;
}
}
}
Your isPrime() function doesn't actually run the for loop more than once; for i = 2, either n % 2 == 0 and the program immediately returns false (i.e., not prime), which is fine, or n % 2 == 1 and the program immediately returns true (i.e., prime), without checking the other values of i.
Awesome! Thanks for your help guys. finally got it working. working on another program that is a little more advanced that I'm having trouble with...will post a topic soon if you think you can help. It involves math with large numbers (using arrays). I'll explain further in my next post...thanks again!