Test Prime Numbers - Peter's Program

  • Thread starter Thread starter peterjaybee
  • Start date Start date
  • Tags Tags
    Prime Test
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to testing for prime numbers in C++. Participants are examining the code provided by a user named Peter and offering feedback on its functionality and structure, particularly focusing on the use of control statements and efficiency of the algorithm.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Peter's code seems to only work correctly for the input n = 3, raising concerns about the use of the goto statement and potential errors in the logic.
  • One participant emphasizes the importance of using curly braces with if statements to avoid ambiguity in code execution.
  • Another participant points out that the code will incorrectly identify numbers less than 2 as prime, suggesting that the initialization of the is_prime variable outside the loop may lead to persistent incorrect results after encountering a non-prime number.
  • There is a discussion about the differences in block control between C/C++ and Python, highlighting how indentation affects code execution in Python compared to C/C++.

Areas of Agreement / Disagreement

Participants express varying opinions on the efficiency and correctness of the code. There is no consensus on a single solution or approach, as multiple issues are identified and debated.

Contextual Notes

Some limitations include the handling of numbers less than 2, the potential misuse of the goto statement, and the need for proper block structuring in C++. The discussion does not resolve these issues definitively.

Who May Find This Useful

Individuals interested in programming, particularly those learning C++ and concepts related to control structures and algorithm efficiency, may find this discussion relevant.

peterjaybee
Messages
62
Reaction score
0
Hi,

I'm new to programming and have written the following code to test for prime-ness, but it doesn't seem to work except for n = 3.

I think it may have something to do with my goto statement. Can anyone see a way of avoiding this or any other errors with the program?

Code:
#include <iostream>
#include<math.h>

using namespace std;

int main() {
	int n, i, is_prime;
	
	//Assume # is prime until proven otherwise
	
	is_prime = true;
	loop:
	
	// get number
	
	cout << "Enter a number and press ENTER (0 to exit): ";
	cin >> n;
	
	if (n == 0) {
		
		return 0;
		
	}
	
	else {
	
		//test for prime-ness.
		
		i=2;
		
		while (i <= sqrt(static_cast<double>(n))) {
			
			if (n%i == 0)
			
				cout << i << " is a factor of " << n << endl;
				is_prime = false;
			
			i++;
		}
		
		if (is_prime)
			cout << "Number is Prime!" << endl;
			
		else 
			cout << "Number is not prime!" << endl;
			
	}
	
	goto loop;
}

Kind regards,

Peter
 
Technology news on Phys.org
Always use { } with if statements - then hunt down the lecturer/textbook writer that misses them out for single line if statements.

Then as a bonus appreciate the Python indentation rules
 
What are the python indentation rules
 
When you do
if ()
... this line
... another line

Which line does the if() statement apply to in c/c++?
In python, it's the indentation that controls the blocks, in c/c++ the indentation means nothing.
 
Last edited:
Well, it's not a terribly efficient way to check for being prime, but it looks like it should work (except for any number less than 2, which it will think is a prime number). A quick scan makes me think that one of your problems is that you set is_prime to true OUTSIDE of your loop. So it'll work the first time you run it, and will continue to work until you give it a non-prime number, in which case it will continually return false, no matter what.

But as mgb_phys points out, your bigger issue is that you should ALWAYS use { } around your code blocks, unless you've gained a knack for programming. As it stands, "is_prime = false" is performed EVERY time you're in your while loop, not just when n % i == 0. Hence, it works with 2 or 3, because it immediately falls out of the while loop (i = 2, and sqrt(3) and sqrt(2) are less than 2).

DaveE
 

Similar threads

Replies
12
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K