Sorting prime function in ascending order

I created the void AllPrimes function to display all the primes numbers. However, I need it to display the Prime numbers in ascending order. Right now it displays them in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 void AllPrimes (size_t n, std::ostream& os = std::cout, bool ticker = 0) {
    if (n >= n+1)          
    {
      std::cerr << " ** Prime: argument too large for implementation. Execution terminating.\n";
      exit (EXIT_FAILURE);
    }
    if (n <= 1) {
        return;
    }

    data::BVector b(1+n); 
    Sieve(b, ticker);

    if (n%2 == 0) --n;     // make n odd
    while (n > 2)          
    {
      if (b[n]) {
        os << b[n] << " ";
      }
      n -= 2;
    }
    os << endl;
}
So make your while loop count up, rather than down.
How would I do that? I tried to make n +=2; but that will only give me 2
If you need all primes, note that this code omits the prime 2.
The last line should be os << "2\n";

That's how I get my 2. I'm supposed to mimic this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
size_t PrimeBelow (size_t n, bool ticker)
  // returns largest prime number <= n
  {
    if (n <= 1) return 0;
    if (n == 2) return 2;
    if (n >= n+1)          
    {
      std::cerr << " ** PrimeBelow: argument too large for implementation. Execution terminating.\n";
      exit (EXIT_FAILURE);
    }
    fsu::BitVector b(1+n); 
    Sieve(b, ticker);
    if (n%2 == 0) --n;     // make n odd
    while (n > 2)          
    {
      if (b[n])
        return n;
      n -= 2;
    }
    return 2;
  } 
Last edited on
closed account (E0p9LyTq)
I need it to display the Prime numbers in ascending order. Right now it displays them in descending order.

A crude change you could make is:

1. create a temporary, empty std::vector at the start of your function

2. store each prime in a std:vector as it is created instead of printing it out.

3. use std::sort from <algorithm> to get the primes in ascending order

4. modify your return type to std::vector, remove the std::ostream& parameter and at the end of your function return the temp std::vector.

5. print out your now ascending primes in your caller. :)
I'm not allowed to use <algorithm>
It's odd that you've done so much, yet can't get
1
2
3
4
5
6
7
size_t c = 0;
while ( c <= n ) {
  if (b[c]) {
    os << b[c] << " ";
  }
  c += 2;
}
salem, that would miss almost every prime.
I'm still having trouble with this. After implementing
1
2
3
4
5
6
7
size_t c = 0;
while ( c <= n ) {
  if (b[c]) {
    os << b[c] << " ";
  }
  c += 2;
}


I now end up with even numbers. For instance, when I use 12 I get 2, 4, 6, 8, 10.
closed account (E0p9LyTq)
I'm not allowed to use <algorithm>

Since your code creates primes in descending order, if you can use a std::vector as storage there is no need to sort the container. Simply start the output at the end of the vector and reverse loop through the elements.
Last edited on
> I now end up with even numbers.
Gee, start at 1 then if you want all the odd numbers.
If b[c] evaluates to true for any even value of c other than 2 then there's something wrong with your Sieve() routine.
Topic archived. No new replies allowed.