Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,111 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,726 people online right now. Registration is fast and FREE... Join Now!




primes

 
Reply to this topicStart new topic

primes

kp362001
25 Feb, 2007 - 06:15 AM
Post #1

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
Not to beat a dead horse but I've been working on this program and I can figure out what the problem is there are no errors but its not printing primes its printing all numbers from 700 - 998 ?

Here is my code
CODE

#include <stdio.h>

bool isPrime(int );



int main ()

{
bool prime;

printf("\nHere are the Primes: \n");

for(int i = 2; i < 1000; i++)
    
{
prime = isPrime(i);

if(prime)
printf("\n%3d",i);
}
return 0;
}


bool isPrime(int n  )

{

for(int j=2; j<=n/2; j++)    
      if(1 % j == 0)            
         break;        

return true;

}

User is offlineProfile CardPM
+Quote Post

horace
RE: Primes
25 Feb, 2007 - 06:25 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
should your statement
CODE

      if(1 % j == 0)            

be
CODE

      if(n % j == 0)            

also you always return true?

User is offlineProfile CardPM
+Quote Post

kp362001
RE: Primes
25 Feb, 2007 - 06:29 AM
Post #3

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
QUOTE(horace @ 25 Feb, 2007 - 07:25 AM) *

should your statement
CODE

      if(1 % j == 0)            

be
CODE

      if(n % j == 0)            

also you always return true?



I tried that and I get an error I changed my code a little but I think I'm still missing some primes ??

CODE

bool isPrime(int n  )
{
   int   k, limit;

   if (n == 2)
      return 1;
   if (n % 2 == 0)
      return 0;
   limit = n / 2;
   for (k = 3; k <=  limit; k += 2)
      if (n % k == 0)
         return 0;
   return 1;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Primes
25 Feb, 2007 - 06:39 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
your compiler gave you no errors? VC++ 5 hated that code. But I sorted that out. Well you have some logical trip-ups here and there.

in main: prime=isPrime(i) but isPrime(i) is a bool, and not only that, it always returns true so prime = true (-1 I assume) all the time.

This causes your next line to always be true, which ends up printing the numbers 2 though 999.


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Primes
25 Feb, 2007 - 07:03 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Many people find it easier to write out the algorithm on some paper before coding it...you just have to think about what a prime number is, and how one will identify it. It is a number that will be divisible by only itself and one. From that, one understands that if the condition if(number%factor==0) evaluates to true and the factor was not one or the number, the number is not prime. Take a look here at a basic implementation:
CODE

bool isPrime(int factor)
{
   bool retValue = true;
   for(int i=1;i<=factor;i++)
   {
      if(((factor%i)==0) && i!=1 && i!=factor)
         retValue = false;
   }
   return retValue;
}

You can see that the three conditions I mentioned are being tested for. Please not the code above is for demonstration purposes only, it can certainly be improved upon.
User is offlineProfile CardPM
+Quote Post

kp362001
RE: Primes
25 Feb, 2007 - 07:05 AM
Post #6

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
QUOTE(NickDMax @ 25 Feb, 2007 - 07:39 AM) *

your compiler gave you no errors? VC++ 5 hated that code. But I sorted that out. Well you have some logical trip-ups here and there.

in main: prime=isPrime(i) but isPrime(i) is a bool, and not only that, it always returns true so prime = true (-1 I assume) all the time.

This causes your next line to always be true, which ends up printing the numbers 2 though 999.



thats microsoft for ya, it doesn't like scanf though I need to put _s after it for it to take . My teacher will send me stuff that he says works yet I cant seem to get it to work with my complier(Visual Studio 5) ? That makes sense though I will look at my code and see if I can figure it out. . thanks
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Primes
25 Feb, 2007 - 07:22 AM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well I looked over your code and got isPrime working. I should note that this is not the best way to find all the primes in a range. Sieve methods work much better though they require more memory.

back to your isPrime: #1 it always returns true, I think you meant for it to return false if and when the break occured. A MUCH better way is to define a variable named "ret" and initalize it to true, then in the if statement you can assign it to false and then at the end return ret; The other thing this will help you deal with is what happens when n/2 = 2...

My version worked a little different but I think you should be able to work out the details from there.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:40PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month