GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2007, 17:08
hiflya69 hiflya69 is offline
New Member
 
Join Date: Oct 2007
Posts: 17
hiflya69 is on a distinguished road

prime numbers


i gotta find the first 100 prime numbers including their reverse or emirps. im tryin to figure out how to come up with something that would test if a number is prime or not. there's no input, only output, so i'm gonna need a loop that keeps going till 100 are found.

since a prime is something that's only divisible by 1 and itself, what i thought was that i would define it as num%(any number from 2 to 9) != 0. i used a bunch of &&s to account for each, and wrote them all out. since a mod remainder of 0 would mean a clean division with nothing left over. is there a more effective way to find out primes?
the backwards thing i think i got figured out by using a loop, and then i'll test those with the same criteria as the primes.
  #2  
Old 18-Oct-2007, 19:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,270
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: prime numbers


Since primes (ex 2) are all odd, use a loop that starts at 3 and skips by 2.

Inside this loop would be another loop that runs from 3 to 1/2 of the number being tested (a rough ending for the max possible).

Also, search this site for prime...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 19-Oct-2007, 16:38
hiflya69 hiflya69 is offline
New Member
 
Join Date: Oct 2007
Posts: 17
hiflya69 is on a distinguished road

Re: prime numbers


hmmm... i see. so to find a prime you divide the number by half of the number and see if there's a remainder? i heard about using square roots too

but if they're odd then half of the number won't be an integer, which means it won't divide out without a remainder. well that's the point of a prime isn't it?

gettin kinda confused

anyway here's some of the code in the main function i got just for starters. i know it's not correct since it doesn't account for alot, like the first few primes, and i have to actually keep writing out numbers that it could possibly be divided by. i looked through a few other pages, but which is the most ideal for finding them?

CPP / C++ / C Code:
 int _tmain(int argc, _TCHAR* argv[])
{

	int num=2;
	int NumOfPrimes;
	

	for(NumOfPrimes=0; NumOfPrimes<100; num++)
	{
		int backwards= reverse(num);

		if (num %(num/2)!= 0 && num % (num/3)!= 0 && num % (num/5)!= 0)
			{

				if (backwards % (backwards/2)!= 0 && backwards % (backwards/3)!= 0 && backwards % (backwards/5)!= 0)
				{
				cout<<"  "<<num;
				NumOfPrimes++;
				}
			}
	}

	return 0;
}
  #4  
Old 19-Oct-2007, 20:10
hiflya69 hiflya69 is offline
New Member
 
Join Date: Oct 2007
Posts: 17
hiflya69 is on a distinguished road

Re: prime numbers


actually scratch that last code for now. ill just temporarily use some that i found by digging through these threads and applying it to my problem

CPP / C++ / C Code:
int reverse( int n )
{
    int result = 0;

	//Accounts for negative numbers

	bool isNegative = false;
    if( n < 0 )
    {
        isNegative = true;
        n = -n;
    }

	//Reverses number

    while( n > 0 )
    {
        result *= 10;
        result += n % 10;
        n /= 10;
    }

	if( isNegative)
    {
        result = -result;
    }

    return result;
}

int is_prime(int n) /*'input' is passed to function as 'n'*/
{
	int k, limit;

	int backwards= reverse(n);
	
	if (n==2) /*if 'input' = 2, then it's prime and 'true' is returned*/
		return 1;
	if (n%2==0 && backwards%2==0) /*if 'input' is evenly divisible by 2, then it's not prime and 'false' is returned*/
		return 0;
	limit = n/2;
	int limit2 = backwards/2;

	
	for (k=3; k <= limit; k += 2) /*checks to see if 'input' is evenly divided by an odd #*/
		if (n%k==0)
			return 0;
	for (k=3; k <= limit2; k += 2)
		if (backwards%k==0)
			return 0;
	return 1;
	
}

int main()
{
	int input, i=0;
	int NumOfPrimes=0;
	
	for (input = 0; i <= 100; ++input)
		if (is_prime(input)==1){     /*if a prime is found, increment a counter then print the counter and the prime number*/
			++i;
			cout<<setw(7)<<input<<" ";
			if (i==10 && i==20 && i==30 && i==40 && i==50 && i==60 && i==70 && i==80 && i==90)
				cout<<endl;
		}
	return 0;
}

on first glance it seems to work, but when you look, it doesn't have all of the right numbers and for some reason 32 and 1024 are in there... i don't get how.. doesn't make sense
  #5  
Old 19-Oct-2007, 21:04
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,270
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: prime numbers


Quote:
Originally Posted by hiflya69
hmmm... i see. so to find a prime you divide the number by half of the number and see if there's a remainder?
No, you divide the number by all the odd numbers from 3 up to 1/2 of the number.

Example:
63:
63/3 - remainder
63/5 - remainder
63/7 - no remainder -- not prime

29:
29/1 - remainder
29/3 - remainder
29/5 - remainder
29/7 - remainder
29/9 - remainder
29/11 - remainder
29/13 - remainder -- prime...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 19-Oct-2007, 21:42
hiflya69 hiflya69 is offline
New Member
 
Join Date: Oct 2007
Posts: 17
hiflya69 is on a distinguished road

Re: prime numbers


yeah thanks for clarifying that. i got it to work now
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table killer9012 C++ Forum 29 10-Oct-2006 22:15
Python script: Prime numbers crystalattice Python Forum 4 16-Apr-2006 09:19
Linear Search eccoflame C Programming Language 3 19-Apr-2005 09:36
prime numbers quasimof C++ Forum 1 01-Nov-2004 20:35
Help w/ prime # determination crystalattice C Programming Language 17 18-Apr-2004 22:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.