Silkroad Online Forums

A community forum for the free online game Silkroad Online. Discuss Silkroad Online, read up on guides, and build your character and skills.

Faq Search Members Chat  Register Profile Login

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Programming Question
PostPosted: Sat Apr 12, 2008 7:10 pm 
Common Member
User avatar
Offline

Joined: Nov 2007
Posts: 124
Location:
Sparta
ok so im currently taking a C++ class and its hard as hell and the professor want us to write a boolean function that returns whether a number is a prime number or not.

how would i go about that? is this how? :


Code:
bool IsNumPrime( int num)
{
if ( num = 2,3,5,7,11,13,17...)
   return true;
else
   return false;
}

_________________
Image


Top
 Profile  
 
 Post subject: Re: Programming Question
PostPosted: Sat Apr 12, 2008 7:43 pm 
Banned User
User avatar
Offline

Joined: Jul 2007
Posts: 2926
Location: Somewhereee
Wrong, wrong wrong, that'd never work, remember this scripting is done with algorithms
Here's a website that will help you http://pastebin.ca/982660
You only have to convert to C++, easy imo

_________________
<<Puff, bye>>


Top
 Profile  
 
 Post subject: Re: Programming Question
PostPosted: Sat Apr 12, 2008 7:44 pm 
Valued Member
User avatar
Offline

Joined: Jul 2006
Posts: 497
Location:
Alexander
You logic is not correct. I wrote a couple of lines, didnt have the time to test it but the logic is correct. Just play with it around a bit and debug the program to see how it works. Hope it helps.
Code:
  bool count = false;
   int num;
   cin >> num;

   
   for(int i=1;i<num/2; i++)
   {
      if (num%i!=0)
      {
         count=true;
      }
      else
         count=false;
   }
   cout<<"the number is :"<<count;

   return 0;

_________________
Server: Alexander

-=IMPERIAL FOREVER=-

[Quit]


Top
 Profile  
 
 Post subject: Re: Programming Question
PostPosted: Sat Apr 12, 2008 8:10 pm 
Banned User
User avatar
Offline

Joined: Jul 2007
Posts: 2926
Location: Somewhereee
As you SHOULD know, a prime number is a whole number
*That's larger than one
*That's divisible only by one and itself ('divisible' meaning: there's NO remainder after division)


Here, a simple but very brute force approach would be something like this:

Code:
bool NumIsPrimer(int num)
{
   if(num < 2) return false;
   int sqr = sqrt(num);
   int try_to_divide = 1;
   while(++try_to_divide < sqr)
   {
      if(num % try_to_divide == 0) return false;
   }
   return true;
}



The thing with what you coded is that it will only work in as far as you already supply it with prime #s.
The trick is to let the program calculate whether the numbers are prime or not.

_________________
<<Puff, bye>>


Top
 Profile  
 
 Post subject: Re: Programming Question
PostPosted: Sat Apr 12, 2008 8:33 pm 
Forum God
User avatar
Offline

Joined: Aug 2006
Posts: 8834
Location: Age of Wushu
Code:
bool isPrime(int num){
   if (num<=3) return true;

   int limit=sqrt(num);

   for (int i=2; i<=limit; i++){
      if (num%i==0) return true;
   }

   return false;
}


add also the header #include <math.h>

_________________
Playing Age of Wushu, dota IMBA


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group