Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Prime#s
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-08-2011, 02:31 PM
Newcomer
 
Join Date: Jun 2011
Posts: 1
Default Prime#s

So I just started C++ a few days ago, forgive me if I did anything royally stupid.
Basically trying to make a small program to give you a list of prime numbers up to a certain number with the Sieve of Eratosthenes algorithm. The problem is that instead of outputting each prime once, they each get spammed for a second.

Code:
// prim1to100.cpp : Defines the entry point for the console application.
// Sieve of Eratosthenes model

#include "stdafx.h"
#include "../../std_lib_facilities.h"

vector<int>not_prime;
vector<int>primes;
bool result;
void cross_out(int x)
{
	int y = x;
		for(int p = 0;p<100;++p) {			//Crosses out numbers and their multiples and adds them to the not prime vector
			y += x;
			not_prime.push_back(y);	
		}										
}

void find_primes(int prime)
{
	for(int timer = 0; timer < not_prime.size(); ++timer) {//looks for numbers that aren't on the not prime vector and adds them to prime
		sort(not_prime.begin(), not_prime.end());
		result = (binary_search(not_prime.begin(), not_prime.end(), prime));
		if(result == true);
		else primes.push_back(prime);		
	}
}
		

int main()
{
	cross_out(2);
	cross_out(3);
	cross_out(5);
	cross_out(7);
	for(int timer2 = 0; timer2 < 100; ++timer2) //Should find all numbers not in not_prime vector
		find_primes(timer2);
	cout << "All prime numbers to 10,000,000: \n";
		sort(primes.begin(), primes.end());
		primes.erase( unique ( primes.begin(), primes.end()));
		for (int timer3 = 0; timer3 < primes.size(); ++timer3) //Lists all elements of primes vector
			cout << primes[timer3] << endl;
	system("pause");
	return 0;
}//Fucked up somewhere, doesn't work.
Reply With Quote
  #2  
Old 06-12-2011, 05:51 PM
Guru
 
Join Date: Jan 2007
Location: /home/Canada
Posts: 1,768
Default Re: Prime#s

If you try to do some debugging and print out the content of "primes" right after your unique() function call you'll see it does have the primes already in order at the beginning of the vector.

It looks something like this: 01235711131719232931374143475359616771737983899700 0000000000...1111111111111...2222222222222222...33 33333333333... and so on.

You need to use resize() on your prime vector after the unique() call so that it only contains the number of elements that are unique which are at the beginning of the vector and it will end up looking like this: 012357111317192329313741434753596167717379838997.

I don't really want to give you the code (you are very close). Have a look here and see if you can figure it out, they're doing something very similar.
http://www.cplusplus.com/reference/algorithm/unique
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 04:34 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1