Programming Question

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
Kazaxat
Common Member
Posts: 124
Joined: Sun Nov 25, 2007 12:30 am
Quick Reply: Yes
Location: Sparta

Programming Question

Post by Kazaxat »

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: Select all

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

User avatar
Casey613
Addicted Member
Posts: 2926
Joined: Thu Jul 26, 2007 6:36 pm
Quick Reply: Yes
Location: Somewhereee

Re: Programming Question

Post by Casey613 »

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

User avatar
DotCom
Valued Member
Posts: 497
Joined: Thu Jul 20, 2006 1:44 am
Quick Reply: Yes
Location: Alexander

Re: Programming Question

Post by DotCom »

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: Select all

  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]

User avatar
Casey613
Addicted Member
Posts: 2926
Joined: Thu Jul 26, 2007 6:36 pm
Quick Reply: Yes
Location: Somewhereee

Re: Programming Question

Post by Casey613 »

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: Select all

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

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: Programming Question

Post by NuclearSilo »

Code: Select all

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

Post Reply

Return to “Off Topic Lounge”