#include <iostream>
usingnamespace std;
int main()
{
int inputs, n;
longint prime[200001];
prime[0]=2;
prime[1]=3;
prime[2]=5;
prime[3]=7;
prime[4]=11;
prime[5]=13;
int b=6;
for (longint a=14; a<3000000; a++)
{//divide by 2, 3, 5, 7, 11, and 13
if (a%2!=0 && a%3!=0 && a%5!=0 && a%7!=0 && a%11!=0 && a%13!=0)
{prime[b]=a;
b++;}
}
cin>>inputs;
for (int i=0; i<inputs; i++)
{
cin>>n;
cout<<prime[n-1]<<" ";
}
return 0;
}
And yes, "Cout<<prime[b]<<" "" up there does print out every prime if I put it within the loop, but as soon as I leave it out, it disappears. Would I have to use a function and return method to retain the full prime array?
Your prime number algorithm is faulty. You test only for divisibility by 2,3,5,7,11,13. Consider 289 (17*17). 289 is not prime, but your algorithm won't detect that it is not prime.
Lines 21-22: Not sure what the purpose of this cin and loop is for. Perhaps a while loop would be more appropriate here. i.e. Loop until the users enters a terminating value for n (0).
Lines 24-25: If the user enters 0 for n, you will get an out of bounds reference (prime[-1]).
You should also check that n is < b.
int main()
{
constexprlongint max_N = 20000;
// Ask user for prime index to calculate
cout << "Nth prime calculator, (1 <= N <= " << max_N << ").\n""N? ";
longint N;
cin >> N;
// Some error checking
if ((N < 1) || (N > max_N))
{
cout << "Invalid N, foo.\n";
return 0;
}
// Here's our initial sieve
longint prime[max_N+1] = { 2, 3, 5, 7, 11, 13 };
longint nprimes = 6;
// Sieve primes until we have nprimes == N+1:
/*( your for loops go here )*/
// Display the requested prime number
// (Remember that arrays are indexed 0..N-1.)
cout << "The " << N << "th prime number is " << prime[N-1] << ".\n";
}
sigh... it seems that doing it the hard way takes far too long (looping 3 million times probably crashed the program). The program using a surefire method also just blanks out in Java too, so I probably need to optimize my approach.
For those that are curious, here's my java version of this program:
import java.util.Scanner;
publicclass Prime
{
publicstaticvoid main(String[] args)
{
int[] PrimeArray;
PrimeArray= newint[200000];
int ArrayCount = 0;
//generate prime array
for (int i = 1; i<2800000; i++) //prime number 200,000 is 2.75 million
{boolean isPrimeNumber = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{isPrimeNumber = false;
break;}
}
if (isPrimeNumber && i!=1)
{PrimeArray[ArrayCount]=i;
ArrayCount++;}
}
//print out requested prime
Scanner sc = new Scanner(System.in);
int request = sc.nextInt();
for (int i=0; i< request; i++)
{
Scanner sc2= new Scanner(System.in);
//system.out.println("What prime number?")
int index = sc.nextInt();
System.out.println(PrimeArray[index]);
}
}
}
one thing that you can do is instead of j++ you use j+=2 (if you really want to use your code and not some other algorithms), needless to say you have to initialize j=3 so it will just check 3,5,7,9....................2999999
although you still have to loop it 1.5 millions time but its better than 3 millions :D
also like duoas said check out Sieve of Eratosthenes
import java.util.Scanner;
publicclass Prime
{
publicstaticvoid main(String[] args)
{
int[] PrimeArray;
PrimeArray= newint[200001];
int ArrayCount = 0;
//generate prime array
for (int i = 1; i<2800000; i++) //prime number 200,000 is 2.75 million
{boolean isPrimeNumber = true;
for (int j = 2; j <= Math.sqrt(i); j++) //j<i takes far too much time and this is proven to be true
{
if (i % j == 0)
{isPrimeNumber = false;
break;}
}
if (isPrimeNumber && i!=1 && ArrayCount<=200000)
{PrimeArray[ArrayCount]=i;
ArrayCount++;}
}
//print out requested prime
Scanner sc = new Scanner(System.in);
int request = sc.nextInt();
for (int i=0; i< request; i++)
{
Scanner sc2= new Scanner(System.in);
//system.out.println("What prime number?")
int index = sc.nextInt();
System.out.println(PrimeArray[index-1]+" ");
}
}
}
The c++ version of this is also similar so no worries about that :)
Thanks guys!