7 Replies - 1570 Views - Last Post: 10 September 2014 - 03:43 PM Rate Topic: -----

#1 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Eratos Prime Numbers

Posted 10 September 2014 - 01:27 PM

Here is my code
#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

void Eratos(int &length, vector< int > &);
ostream &operator<< (ostream &, const vector <int>&);
bool isPrime(int);
int main()
{
	int range;
	vector< int > v;
	cout << "Enter the upper range of number to search for prime numbers\n";
	cin >> range;
	if (range > 300 || range < 2)
	{
		cout << "Invalid range. Your range is not between 2 and 300\n";
	}
	else
	{
		Eratos(range, v);
	}
	return 0;
}


void Eratos(int &length, vector< int > &v)
{

	for (int i = 2; i < length; ++i)
	{
		v.push_back(i);
	}
	for (int i = 2; i < v.size(); ++i)
	{
		if (isPrime(i) == false)
		{
			v.erase(v.begin() + i);
		}
	}

	cout << "Prime numbers are:\n";
	cout << v;
}

ostream &operator <<(ostream& print, const vector<int> &v)
{
	int count = 1;
	for (unsigned int i = 0; i < v.size(); ++i)
	{
		print << setw(5) << v[i];
		if (count % 10 == 0)
		{
			cout << '\n';
		}
		count++;
	}
	print << '\n';
	return print;
}
bool isPrime(int i)
{
	for (int j = 2; j < i; ++j)
	{
		if (i % j == 0)
		{
			return false;
		}
	}
	return true;
}


The problem is my code takes away some prime but leaves other primes in and i cant seem to figure out why

like my output is
2 3 4 5 7 8 10 11 13 15
17 18 20 21 23 25 27 28 30 31
33 35 37 38 40 42 44 46 48 49
51 52 54 56 58 60 62 63 65 67
69 70 72 73 75 77 79 80 82 84
86 88 90 91 93 95 97 99

But the output should be
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97

Is This A Good Question/Topic? 0
  • +

Replies To: Eratos Prime Numbers

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Eratos Prime Numbers

Posted 10 September 2014 - 01:31 PM

I guess you need to understand what erase() actually does to the content and length of a vector.
http://www.cplusplus...r/vector/erase/

Print out the whole vector, after every erase() call.

You should be doing this as part of your general approach to learning how to do your own debugging.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Eratos Prime Numbers

Posted 10 September 2014 - 01:32 PM

Hints:
Prime numbers are prime because they have no factors other than themselves and 1. Any factor that is not prime can be broken into prime factors, so a number is prime if it has no prime factors. And if a number has prime factors, at least one of them is equal to or smaller than its square root.

This post has been edited by CTphpnwb: 10 September 2014 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: Eratos Prime Numbers

Posted 10 September 2014 - 02:31 PM

im not really getting much from that. I believe the problem is how the code is being read. i think the itsprime is skipping numbers but i cant figure out why
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Eratos Prime Numbers

Posted 10 September 2014 - 03:15 PM

Hi, your iterator number i does not correspond with the vector element, for two reasons 1) it starts at 2 and 2) you erase elements.

Eg. when i is 4 you erase begin() + i which is begin() + 4, and is the 5th element.

The 5th element is :-

2,3,4,5,6

6. So you don't delete the number 4 but the number 6.
Was This Post Helpful? 0
  • +
  • -

#6 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: Eratos Prime Numbers

Posted 10 September 2014 - 03:24 PM

how to i fix that?

*do
Was This Post Helpful? 0
  • +
  • -

#7 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Eratos Prime Numbers

Posted 10 September 2014 - 03:35 PM

It might be simpler just to push the primes to the back of the initially empty vector.

Are you creating a sieve?
Was This Post Helpful? 0
  • +
  • -

#8 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: Eratos Prime Numbers

Posted 10 September 2014 - 03:43 PM

yes its a Sieve of Eratosthenes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1