C++ programming on prime numbers

Click For Summary

Discussion Overview

The discussion revolves around a C++ program designed to determine whether a given number is prime. Participants explore issues related to the program's logic, efficiency, and output for various inputs, including specific numbers like 9 and 5. The conversation includes technical explanations and suggestions for improvements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the program incorrectly identifies prime and composite numbers due to the placement of output statements within the loop.
  • Another participant suggests that the loop should run at least once and that the logic for determining primality needs refinement.
  • There is a proposal to limit the loop to check divisors only up to the square root of the number, rather than up to half of the number, to improve efficiency.
  • Concerns are raised about the performance of the program when checking large numbers, with a specific example given of 1,000,000,000,039.
  • Participants discuss the need for a more modern compiler, suggesting that the use of Turbo C++ may be outdated.
  • One participant mentions the potential use of inline functions to improve performance, though another argues it may not significantly enhance speed.

Areas of Agreement / Disagreement

Participants express differing views on the correctness and efficiency of the program. While some agree on the need for improvements, there is no consensus on the best approach to achieve this or on the effectiveness of certain proposed changes.

Contextual Notes

Participants highlight limitations in the original program's logic, including the handling of edge cases and the efficiency of the algorithm. There is also mention of the need for better coding practices and modern tools.

Who May Find This Useful

Individuals interested in C++ programming, algorithm optimization, and number theory may find this discussion relevant.

smart_worker
Messages
131
Reaction score
1
this is the program which i wrote:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void prime(int p)
{
if(p==0||p==1)
{
count<<"neither prime nor composite"<<endl;
getch();
exit(1);
}
for(int i=2;i<p/2;i++)
{
if(p%i==0)
{
count<<"composite"<<endl;
break;
}
else
count<<"prime"<<endl;
}
}
void main()
{
int n;
clrscr();
count<<"enter a number:"<<endl;
cin>>n;
prime(n);
getch();
}


it is not working for some numbers like 9,5,2,etc..

if i input 9
output is :

composite
prime

if i input 5
nothing is displayed


i debugged and no errors came
what exactly is wrong with this code?
 
Technology news on Phys.org
That output looks right for that code. Let's look at your two cases to see what the program actually does.

If p=9, p/2=4 (remember integer division). Due to the test in the for loop, you will test the values i=2 and i=3. In the case of i=2, 9%2=1 and it prints prime no matter what the later tests will give. For i=3, 9%3=0 and it prints composite. You probably don't want to print prime until you've verified that no numbers covered in the loop are factors of p.

If p=5, p/2=2. This means that the for loop won't run at all, since 2 isn't less than 2. You probably do want to run the loop at least once.
 
Hypersphere said:
That output looks right for that code. Let's look at your two cases to see what the program actually does.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void prime(int p)
{
int count=1;
if(p==0||p==1)
{
count<<"neither prime nor composite"<<endl;
getch();
exit(1);
}
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
count++;
}
if(count>=2)
count<<"not a PRIME NUMBER"<<endl;
else
count<<"PRIME NUMBER"<<endl;
}
void main()
{
int n;
clrscr();
count<<"enter a number:"<<endl;
cin>>n;
prime(n);
getch();
}


now is it correct?
o:)
 
Use [noparse]
Code:
[/noparse] tags:

Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void prime(int p)
{
   int count=1;

   if(p==0||p==1)
   {
      cout<<"neither prime nor composite"<<endl;
      getch();
      exit(1);
   }
   for(int i=2;i<=p/2;i++)
   {
      if (p%i==0)
         count++;
   }
   if(count>=2)
      cout<<"not a PRIME NUMBER"<<endl;
   else
      cout<<"PRIME NUMBER"<<endl;
}

void main()
{
   int n;

   clrscr();
   cout<<"enter a number:"<<endl;
   cin>>n;
   prime(n);
   getch();
}

For the code readability, correct formatting is paramount.
 
smart_worker said:
now is it correct?o:)
Strictly speaking, it's not correct. All it takes is one number to divide p with zero remainder and p is not prime. Your program will happen to have at least two divisors, but that's because your loop go to and including p/2. There's no reason to do that. Once you find one such divisor you know the number cannot be prime. Exit the loop immediately. There's no reason to go all the way to p/2. You only need to go up to and including sqrt(p). While the difference between p/2 and sqrt(p) is small for small numbers, it's not small at all for bit numbers. For example, a limit of p/2 means checking 500,000,000,019 numbers in the case p=1,000,000,000,039. A limit of sqrt(p) means you only need to check 1,000,000 numbers to see that 1,000,000,000,039 is indeed a prime. (And you can reduce this by a factor of two if you check 2 and then only check odd numbers).
 
D H said:
Strictly speaking, it's not correct. All it takes is one number to divide p with zero remainder and p is not prime. Your program will happen to have at least two divisors, but that's because your loop go to and including p/2. There's no reason to do that. Once you find one such divisor you know the number cannot be prime. Exit the loop immediately. There's no reason to go all the way to p/2. You only need to go up to and including sqrt(p). While the difference between p/2 and sqrt(p) is small for small numbers, it's not small at all for bit numbers. For example, a limit of p/2 means checking 500,000,000,019 numbers in the case p=1,000,000,000,039. A limit of sqrt(p) means you only need to check 1,000,000 numbers to see that 1,000,000,000,039 is indeed a prime. (And you can reduce this by a factor of two if you check 2 and then only check odd numbers).

anyways for small numbers<100 i am getting correct results

and for bit numbers as you said for 1,000,000,000,039 i have to use longint instead of int
 
The same argument applies to 2147483647 (which does fit within an int). Try this number. Your program takes an inordinate amount of time to say that this is a prime number. That is not acceptable. Ending your loop at 46340 rather than 1073741823 (in this case) would make your program 23,000 times faster.

BTW, you need to get a better compiler. Given your include files and your arcane use of void main, I suspect you are using Turbo C++. You should get a compiler that was written in this millennium. There are plenty of free compilers around. There is *no* reason to use Turbo C++ anymore.
 
Last edited:
D H said:
2147483647

it is a prime number

if my program is too slow i can make use of INLINE function.
 
smart_worker said:
if my program is too slow i can make use of INLINE function.

It won't change much.

The fastest operation is the one never performed. D H told you how to implement it in your case, but apparently your prefer to not listen to the good advice.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K