Test Prime Numbers - Peter's Program

  • Thread starter peterjaybee
  • Start date
  • Tags
    Prime Test
In summary, Peter found that his code didn't seem to work properly and he was unsure as to why. He also found that his code was inefficient and needed to be improved.
  • #1
peterjaybee
62
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
  • #2
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
 
  • #3
What are the python indentation rules
 
  • #4
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:
  • #5
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
 

1. What is "Test Prime Numbers - Peter's Program"?

"Test Prime Numbers - Peter's Program" is a computer program designed to determine whether a given number is a prime number or not. It was created by Peter, a computer scientist, to help with his research on number theory.

2. How does "Test Prime Numbers - Peter's Program" work?

The program uses a mathematical algorithm to check if a given number has any factors other than 1 and itself. If it does not, then it is considered a prime number. The program displays the result of this calculation for any number that is entered into it.

3. Can "Test Prime Numbers - Peter's Program" test large numbers?

Yes, the program has been optimized to handle very large numbers. It uses efficient coding techniques to handle calculations for numbers with hundreds or even thousands of digits. However, the time taken to test these large numbers may vary depending on the processing power of the computer running the program.

4. Is "Test Prime Numbers - Peter's Program" accurate?

Yes, the program has been extensively tested and verified to accurately determine whether a number is prime or not. However, like any computer program, there is always a chance of errors, so it is always recommended to double check the results for important calculations.

5. Can "Test Prime Numbers - Peter's Program" be used for other purposes?

While the main purpose of the program is to test for prime numbers, it can also be used to check for factors of a number and to generate prime numbers within a given range. It can be a useful tool for number theory research and for solving mathematical problems involving prime numbers.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
958
  • Programming and Computer Science
Replies
22
Views
759
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
2
Views
873
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top