DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Practise Problem, Pair of Prime Numbers (http://www.daniweb.com/forums/thread143933.html)

vidit_X Sep 4th, 2008 5:35 am
Practise Problem, Pair of Prime Numbers
 
Q.Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user. ( suggested by Aniseed ) (Intermediate)

#include<iostream.h>
#include<conio.h>
#include<math.h>

int prime(int);
void main()
{
 int i,j,c1,c2,num,f1,f2;
 clrscr();
 cout<<"Enter the number-";
 cin>>num;
 for(i=2,j=num-2;i<=num/2;i++,j--)
 {
  f1=prime(i);
  if(f1)
  {
    f2=prime(j);
        if(f2)
    cout<<"Pair:"<<i<<" + "<<j<<endl;       
  }
 } 
 getch();
}         
int prime(int a)
{
 int i;
 for(i=2;i<=sqrt(a);i++)
  if(a%i==0)
  return 0;
  else
  continue;       
 return 1;
}
Plz verify it. Any suggestions,comments or recommendations are welcomed.

Salem Sep 4th, 2008 1:03 pm
Re: Practise Problem, Pair of Prime Numbers
 
> #include<iostream.h>
This is old. The correct form is #include <iostream>
If you (or your compiler) don't know about namespaces, then it's time you looked into it, or upgraded your compiler. namespaces have been standard issue since 1998, so there's really no excuse any more.

> #include<conio.h>
This is non-standard. If you can write your code without it, do so.

> #include<math.h>
In a namespace enabled compiler, this would be #include <cmath>

> void main()
This is just wrong. main returns an int.

Your prime() function could be more efficient, especially for large numbers.
How many even primes are there?

hiraksarkardg Sep 4th, 2008 1:22 pm
Re: Practise Problem, Pair of Prime Numbers
 
Salem's post is just OK. Things that I more wanna say that if you write #include<iostream> then you must write "using namespace std;" next line & you can use #include<cmath> in place of #include<math.h>. That's all.

vidit_X Sep 4th, 2008 1:44 pm
Re: Practise Problem, Pair of Prime Numbers
 
I know about namespaces and the new standards i.e #include<iostream> , but ya my compiler "Turbo C++ v3.0" is old and dont understands the new formats like namespaces and the new style header files. An upgraded, according to the new standards, code will be

#include<iostream>
#include<cmath>
using namespace std;
bool isprime(int);
int main()
{
 int i,j,c1,c2,num;
 bool f1,f2;
 cout<<"Enter the number-";
 cin>>num;
 for(i=2,j=num-2;i<=num/2;i++,j--)
 {
  f1=isprime(i);
  if(f1)
  {
    f2=isprime(j);
        if(f2)
    cout<<"Pair:"<<i<<" + "<<j<<endl;       
  }
 } 
 return 0;
}         
bool isprime(int a)
{
 int i;
 for(i=2;i<=sqrt(a);i++)
  if(a%i==0)
  return false;
  else
  continue;       
 return true;
}
And thanks Salem and hiraksarkardg for your concern. :) Is conio.h not ISO C++ standard. Which funtion to be used in place of clrscr() and getch()?

Salem Sep 4th, 2008 2:08 pm
Re: Practise Problem, Pair of Prime Numbers
 
> Is conio.h not ISO C++ standard.
Correct.

> Which funtion to be used in place of clrscr() and getch()?
Such things only matter (to some extent) when you're running from the IDE.
Most programs have neither.
Consider 'dir', which neither clears the screen, nor waits for a key press.

vidit_X Sep 4th, 2008 2:36 pm
Re: Practise Problem, Pair of Prime Numbers
 
Why void main is deprecated in ISO standards? Where can i get a brief overview of the ISO C++ standards?

Salem Sep 4th, 2008 2:50 pm
Re: Practise Problem, Pair of Prime Numbers
 
You can't deprecate what was never standard to begin with.
void main has always been wrong, despite what many popular books and sloppy compilers say or do

vidit_X Sep 4th, 2008 3:18 pm
Re: Practise Problem, Pair of Prime Numbers
 
Thanks Salem for help, I think you really hate those who use "void main()" and you love "int main()" as your avtar and line under name says :D

vidit_X Sep 5th, 2008 11:43 am
Re: Practise Problem, Pair of Prime Numbers
 
Any other tips for efficiency?
There is only 1 even prime number i.e 2.

Lerner Sep 5th, 2008 12:52 pm
Re: Practise Problem, Pair of Prime Numbers
 
>>There is only 1 even prime number i.e 2

So, is there a simple way to determine if a number is even or odd? And, if any factor above 2 is even can the number be prime?

This might be a little more optimized:
bool isprime(int a)
{
  bool result = true;
  int i;

  //assume a > 0
  if(a % 2 == 0)
    result  = false;
  else 
  {
    //check for factors, i, that are odd and above 1
    for(i = 3; i <= sqrt(a); i += 2)
    { 
      if(a % i == 0)
      {
          result = false; 
          break;
        }
    }
    return result;
}


All times are GMT -4. The time now is 3:41 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC