Blank output explanation?

I have to generate a specific prime but I'm getting blank outputs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main()
{
    int inputs, n;
    long int prime[200001];
    prime[0]=2;
    prime[1]=3;
    prime[2]=5;
    prime[3]=7;
    prime[4]=11;
    prime[5]=13; 
    int b=6;
            for (long int 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?
Hi,
Firstly :
for (long int a=14; a<3000000; a++)

So mind you explain what you are doing here? Why make your computer do three million of loops?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()
{
    int inputs, n;
    long int prime[200001];
    prime[0]=2;
    prime[1]=3;
    prime[2]=5;
    prime[3]=7;
    prime[4]=11;
    prime[5]=13; 
    int b=6;
            for (long int a=14; a<300 ; 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++;}
            }
        
    std::cout << "Here we are\n"; // <--
    
    cin>>inputs;
    for (int i=0; i<inputs; i++)
    {
        std::cout << "Here we are again\n"; // <--
        cin>>n;
        cout<<prime[n-1]<<" ";
    }
    return 0;
}


It helps if you have some cout statements as I've shown. And also try a < 300 to start off with.
Last edited on
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.


Last edited on
pretty sure there are better algorithm's out there... either make them or google them (first one preferred)
He's trying to create a sieve (of Eratosthenes).

The problem lies on line 16, which skips the sieve (the array of `prime[]`s) altogether.
Don't hard-code. Use a loop. (Yes, a loop in a loop.)

Better names for your variables will help:

    b 
 prime_count, nprimes, num_primes, ...
    a 
 potential_prime, n, x, ...

Now you only need to ask for the prime index:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
  constexpr long int max_N = 20000;

  // Ask user for prime index to calculate
  cout << "Nth prime calculator, (1 <= N <= " << max_N << ").\n"
          "N? ";
  long int N;
  cin >> N;

  // Some error checking
  if ((N < 1) || (N > max_N))
  {
    cout << "Invalid N, foo.\n";
    return 0;
  }

  // Here's our initial sieve
  long int prime[max_N+1] = { 2, 3, 5, 7, 11, 13 };
  long int 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";
}

Hope this helps.

[edit] Fixed typo
Last edited on
@ closed account 5a8Ym39o6 (975), the 200,000th prime number is 2.75 million which is why I am going through 3 million loops.
okay

here's a famous version of your problem from project euler

https://projecteuler.net/problem=7

and some of the solutions

https://www.google.com/search?q=project+euler+prime+number&oq=project+euler+prime+number&aqs=chrome..69i57.5330j0j1&sourceid=chrome&ie=UTF-8#q=project+euler+problem+7+solution+c%2B%2B

although your problem is solved for now but the point is that there are many solutions to a single problem, you have to make/pick the best one....

hope it helps
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;

public class Prime 
{
 public static void main(String[] args) 
  {
int[] PrimeArray;
PrimeArray= new int[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

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Oh don't worry I managed it using sqrt because there's a proven theorem that any composite number has factors of sqrt and lower. Final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;

public class Prime 
{
 public static void main(String[] args) 
  {
int[] PrimeArray;
PrimeArray= new int[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!
Topic archived. No new replies allowed.