Why Errors in Prime Number Loop

This is a discussion on Why Errors in Prime Number Loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a program that finds a prime number less than 600 and tests to see if the reverse ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Why Errors in Prime Number Loop

    Hi, I'm writing a program that finds a prime number less than 600 and tests to see if the reverse is also prime (emirp). If so it should output the number. However, the program will not run. Can you please tell me where I am going wrong?

    Code:
    int reverseNumber(int sourcenum);
    
    int main ()
    {
    int number, counter, sum;
    
    while(counter<600)
    {
    	if (isPrime(number))
    	{
    		sum = reverseNumber(number);
    		if (isPrime(sum))
    		{
    			cout << sum;
    			return (0);
    		}
    	}
    	counter++;
    }
    return (0);
    }
    
    
    Logical isPrime(int integer)
    {
      for (int factor = 2; factor<integer; factor++) {
        if ((integer % factor) == 0) 
          return False;
      }
      return True;
    }
    
    int reverseNumber(int sourcenum)
    {
    	int temp = sourcenum;
    	int sum = 0;
    	while (temp)
    	{
    		sum*=10;
    		sum += temp%10;
    		temp/=10;
    	}
    	return sum;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well what is/are the error(s) you are getting ?

    Edit: You didn't initialize counter and number.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    You have used logical??but you haven't defined what's logical?? This may help you :-

    Code:
    #include <iostream.h>
    enum logical
    {
      false,
      true,
    };
    int reverse(int n)
    {
      int rev=0;
      while(n>0)
      {
    	rev=rev*10+n&#37;10;
    	n=n/10;
      }
      return rev;
    }
    logical isprime(int n)
    {
      int i;
      for(i=2;i<=n-1;i++)
      {
    	if(n%i==0)
    		return false;
      }
      return true;
    }
    
    int main(void)
    {
     clrscr();
     int n;
     cout<<"\n\nEnter The Value Of N :";
     cin>>n;
     for(int i=2;i<n;i++)
     {
    	if(isprime(i) && isprime(reverse(i)))
    		cout<<i<<"  ";
     }
    
     return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    enum logical
    {
      false,
      true,
    };
    Whats wrong with a bool?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    int reverseNumber(int sourcenum);
    logical isprime(int n);
    int main ()
    ...

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by cyberfish View Post
    Whats wrong with a bool?
    Nothing wrong! but since he has used logical. I've kept it logical!

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by chottachatri View Post
    Nothing wrong! but since he has used logical. I've kept it logical!
    That seems logical.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    It's like writing:
    Code:
    enum UnsignedInteger {
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
    };

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    if you really like the "logical":
    Code:
    typedef bool Logical;
    would do the same job.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Alright, I noticed that I haven't initialized counter, or number, but when I do the program doesn't function as I would like. Basically I believe that there is something wrong with the loop. I'll show the code I've fixed thus far:

    Code:
    #include <iostream>
    using namespace std;
    
    enum Logical {False, True};
    
    
    Logical isPrime(int integer);
    int reverseNumber(int sourcenum);
    
    int main ()
    {
    int number, counter=1, sum;
    
    while(counter<600)
    {
    	if (isPrime(number))
    	{
    		sum = reverseNumber(number);
    		if (isPrime(sum))
    		{
    			cout << sum;
    			return (0);
    		}
    	}
    	counter++;
    }
    return (0);
    }
    
    
    Logical isPrime(int integer)
    {
      for (int factor = 2; factor<integer; factor++) {
        if ((integer % factor) == 0) 
          return False;
      }
      return True;
    }
    
    int reverseNumber(int sourcenum)
    {
    	int temp = sourcenum;
    	int sum = 0;
    	while (temp)
    	{
    		sum*=10;
    		sum += temp%10;
    		temp/=10;
    	}
    	return sum;
    }

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, you are still using an uninitialized variable number, where you probably meant to use counter (lose number altogether, also note that 1 is not a prime).

    Then you have a return (0) in the middle of the code (terminates as soon as the first value is found).

    Thirdly, is there any reason not to use built-in bool which has values true and false?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Thanks for all the help I managed to get it working correctly. However, instead of outputting all the numbers in a list. I want to display it in a 10x10 grid. Any help or suggestions? Here's the code one last time...

    Code:
    #include <iostream>
    using namespace std;
    
    enum Logical {False, True};
    
    
    Logical isPrime(int integer);
    int reverseNumber(int sourcenum);
    
    int main ()
    {
    int number, counter=10, sum;
    
    while(counter<1742)
    {
    	if (isPrime(counter))
    	{
    		sum = reverseNumber(counter);
    		if (isPrime(sum))
    			cout << counter << endl;
    	}
    	counter++;
    }
    return (0);
    }
    
    
    Logical isPrime(int integer)
    {
      for (int factor = 2; factor<integer; factor++) {
        if ((integer % factor) == 0) 
          return False;
      }
      return True;
    }
    
    int reverseNumber(int sourcenum)
    {
    	int temp = sourcenum;
    	int sum = 0;
    	while (temp)
    	{
    		sum*=10;
    		sum += temp%10;
    		temp/=10;
    	}
    	return sum;
    }

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by cyberfish View Post
    It's like writing:
    Code:
    enum UnsignedInteger {
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
    };
    Ok but i m comfortable with enum only.!

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    And could you elaborate on how using bools is harder than actually creating an enum to simulate bools ? That makes no sense man.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    and could you elaborate how using bool is faster than enum?it depends on preference !man!
    Also i can't use bool as it's a new data type in C++ and as you might be knowing that i use Turbo C++ 3.0 compiler which is fairly old which doesn't support this data type. So i have to compulsorily use enum only!
    I hope i am clear

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  2. Prime number check
    By password in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2007, 10:30 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM
  5. prime # query
    By alokin in forum C Programming
    Replies: 8
    Last Post: 09-04-2002, 11:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21