C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: a programme to display prime numbers Posted by Lundin on 15 Mar 2007 at 3:30 AM
This message was edited by Lundin at 2007-3-15 3:31:33

: : : :
: : : : bool isPrime(const int& x)
: : : : {
: : : :   if(x == 2)
: : : :     return true;
: : : :   else if(x <= 1)
: : : :     return false;
: : : :   
: : : :   else if(x % 2 == 0)
: : : :     return false;
: : : : 
: : : : 
: : : :   for (int i=3; i<x/2; i+=2)
: : : :   {
: : : :     if (x % i == 0) 
: : : :       return false;
: : : :   }
: : : : 
: : : :   return true;
: : : : }
: : : : 

: : : :
: : : : 1 is not a prime number. Plus, prime numbers are natural numbers, thus not anything like 0, -1, -2, -3...
: : :
: : : Thanks for your correction. Indeed, negatives are not prime, but it depends on your definition if one is prime. From the WikiPedia ([[http://en.wikipedia.org/wiki/1_%28number%29]]):
: : :
: : : 'One is currently considered neither a prime number, nor a composite number - although it used to be considered prime.'
: : :
: : : See ya,
: : : bilderbikkel
: : :
: : :
: :
: :
: :
: : In that case, x and i should be changed to unsigned int.
: :
: : Another comment: there is no point in passing x as reference. As a rule of thumb, pass primitive data types (int, char, float etc) by value and everything else by reference.
: :
:
: My comment is that the for loop the condition should be stored in a constant varible or something as every time it tests the condition it is dividing x by two. Where as, if it were to store the test condition in a const int or whatever it would eliminate this process mind you it is completely irrevelent really and it is somewhat easier to read without it
:


Yeah but it is a good point that I didn't think of. It will increase the efficiency of the code drastically. Here is a new version with correct data types and no reference.

bool isPrime(unsigned int x)
{
  if(x == 2)
    return true;
  else if(x < 2)
    return false;
  else if(x % 2 == 0)
    return false;


  unsigned int half_x = x / 2;
  unsigned int i;

  for(i=3; i<half_x; i+=2)
  {
    if (x % i == 0) 
      return false;
  }

  return true;
}



Thread Tree
wizad a programme to display prime numbers on 9 Mar 2007 at 10:17 AM
subirkumarsao Re: a programme to display prime numbers on 10 Mar 2007 at 2:22 AM
bilderbikkel Re: a programme to display prime numbers on 10 Mar 2007 at 4:19 AM
Lundin Re: a programme to display prime numbers on 12 Mar 2007 at 1:47 AM
Gregry2 Re: a programme to display prime numbers on 12 Mar 2007 at 5:41 AM
bilderbikkel Re: a programme to display prime numbers on 12 Mar 2007 at 6:31 AM
Lundin Re: a programme to display prime numbers on 12 Mar 2007 at 7:26 AM
bluj91 Re: a programme to display prime numbers on 13 Mar 2007 at 6:13 PM
Lundin Re: a programme to display prime numbers on 15 Mar 2007 at 3:30 AM
FDrache Re: a programme to display prime numbers on 15 Mar 2007 at 5:49 AM
Lundin Re: a programme to display prime numbers on 15 Mar 2007 at 7:40 AM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.