Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 14th, 2005, 01:48 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default prime numbers code error..

hi people,
i was trying to have fun with some c++ codes....it seems easy but ..i really got stuck because i ididnt get the logic of the primary number..or in better way....how to convert that logic in logical statement.!

here is the code



# include <iostream.h>
# include <conio.h>
# include <iomanip.h>

bool prime(int n);


int main()
{

    int count = 0;

    cout<<"the prime numbers from 1 to 10000 are :\n";

            for(int loop=2; loop<=10000; ++loop)
                if(*********) //make call to prime()
                {
                     ++count;
                     cout<<setw(6)<<loop;

                     if(count%10==0)
                         cout<<'\n';
                } // end if

                cout<<"\nthere were :"<<""<<count<<" "<<"prime numbers";
                return 0;

}


bool prime(int n)
{
    for(int i=2; i<=10000 ; i++)
        if(//test if n is divisible by i//)
            return false;

        else
            return true;
}

to test if n is divisible by n:

i have tried the following:
if(n%i!=0)
return fale
else return true;

i also have tried many logical statements but none of them work properly...so..i think i missing the logic of this..can anyone help me please?

thank you





Reply With Quote
  #2 (permalink)  
Old January 19th, 2005, 01:33 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

for prime numbers there are two general ways,
n is a prime number if it has just two factors.(1 and that number)
for example,
2 is a prime number because its factors are{1,2}
3 is a prime number because its factors are{1,3}
23 is a prime number because its factors are{1,23}
6 is not a prime number because its factors are{1,2,3,6}
1 is not a prime number because its factor is{1}
every even number except 2 is not a prime number because its factors are more than 2(for example for 2n we have at least 1,2,n,2n as its factors}
and its code is,
Code:
//Not tested
bool IsPrime(int n)
{
  int counter=0;
  for(int i=1; i<=n ; i++)
   if (n%i==0) counter++;
  if (counter==2) return true;
  return fasle;
}

but another way is,
for every number like n,it is a prime number if there is no factor from 2 to its root(n^1/2) for example,
12 is not a prime because between ranges from 2 to 3(3<=12^1/2) 2 and 3 are the factors for 12,
and its code is,
Code:
//Not tested
bool IsPrime(int n)
{
  //I'm not sure what is the right function for sqrt
  int root=(int)sqrt(n);
  for(int i=2; i<=root ; i++)
   if (n%i==0) return fasle;
  return true;
}
probably the second way is faster and better,
(if you just want to generate the prime numbers between two number there is a smart solution called Eratostenes Sieve,check out its link).

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Reporting With Line Numbers ef1196 Pro Visual Basic 2005 10 December 12th, 2006 03:28 PM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM
error in code bukky Classic ASP Databases 2 March 5th, 2004 11:29 AM
Error displayed with error trap code stoneman Access 4 February 28th, 2004 02:53 PM



All times are GMT -4. The time now is 09:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.