8 Replies - 395 Views - Last Post: 24 April 2013 - 06:29 PM Rate Topic: -----

#1 yLif429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 27-March 13

Prime number checker w/loop error & failed cout

Posted 19 April 2013 - 12:18 PM

Okay. Was gone there for a bit, but I'm back to the drawing board!

I am writing a program that checks for the inserted number's primeness.
If the number is prime, then it should echo, else, it should echo that it isn't.
If the number is prime, then it just skips to the end, without echoing.
If the number is not prime, it also just skips ahead, without echoing.
If the number is not prime, whilst being divisible by 3, then it spams that the number is prime.

I added a break, yet saw no desired results.

Without further ado, I give thee my code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int i, n;
	int is_prime = true;
	cout << "Enter a number to be tested for Optimus: \n";
	cin >> n;
	i = 2;

	while (i <= sqrt((double)n)) {
		if (n % i == 0) { 
			is_prime = false;
			i++;
		}
		if (is_prime)
			cout << "Your number is Optimus. Recieve 500 souls.\n";
		else
			cout << "Your number is not Optimus. Devour your lift pinky, but remember to do this only by wrapping the little piggie into a blanket and pouring insanely hot gravy onto it and sucking it clean. Then mash the bone down into a powder and snort it. Enjoy, and thankyou for visiting Casa De Blanca!\n";
	}
		system("PAUSE");
		return 0;
}



Going to work in about 90 minutes, or so. Hoping to continue this endeavor at a later hour(if not possibly before work). ;)

Is This A Good Question/Topic? 0
  • +

Replies To: Prime number checker w/loop error & failed cout

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Prime number checker w/loop error & failed cout

Posted 19 April 2013 - 01:08 PM

  • If the number is shown to be not prime then why do you continue with the while loop?
  • If the number has not yet been shown to be prime or not prime (still in the loop) then why do you output that it is or is not prime?

Was This Post Helpful? 1
  • +
  • -

#3 yLif429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 27-March 13

Re: Prime number checker w/loop error & failed cout

Posted 23 April 2013 - 05:08 PM

Okay, I found the issues.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int i, n;
	int is_prime = true;
	cout << "Enter a number and Press ENTER: \n";
	cin >> n;
	i = 2;
	while (i <= sqrt((double)n)) {
		if (n % i == 0) {
			is_prime = false;
			break;
		}
		i++;
	}
	if (is_prime)
		cout << "Number is prime. \n";
	else
		cout << "Number is not prime. \n";
	system("PAUSE");
	return 0;
}



Is there a way that I could make it only calculate the sqrt once, rather than multiple times?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Prime number checker w/loop error & failed cout

Posted 23 April 2013 - 06:30 PM

You could assign the square root to a variable and then use that in the loop condition.
Was This Post Helpful? 0
  • +
  • -

#5 yLif429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 27-March 13

Re: Prime number checker w/loop error & failed cout

Posted 23 April 2013 - 11:31 PM

Someone else said that, too, but I just don't understand how that would help.
Could you maybe show me? Or possibly elaborate? I am just trying to get a decent grasp on this stuff before I go to college in the fall. You have been helpful so far, and appreciate the approach of giving help, but not straight blurting out the answer. Helped me figure it out on my own, and I like that. Although, I don't understand ~this~ particular concept.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Prime number checker w/loop error & failed cout

Posted 24 April 2013 - 12:29 AM

Logically, having the sqrt within the loop condition will result in the sqrt being calculated every iteration (expensive). Since the sqrt(n) never changes, you should calculate it once above the loop and store it into a separate variable. Plus it looks like you know what your doing, performance wise.

int sqrt_n = sqrt(n);

while(i <= sqrt_n) {

}



That being said, compilers are so good these days that they might see that the condition argument sqrt(n) never changes, and might do this for you. An example of this is below.

i.e.
#include <stdio.h>
#include <math.h>

int main() {
   int x = 0;
   double y = 2000.0;

   while(x < sqrt(y))
      x++;

   return 0;
}



Compiling the above code using gcc (level 1 optimations) produces the assembly for the while loop below.


   while(x < sqrt(y))
 80483dc:	83 f8 2d             	cmp    eax,0x2d //<---- Computed sqrt(y) only once (0x2d ~ sqrt(2000)
 80483df:	75 f8                	jne    80483d9 <main+0x5> 
      x++;



As you can see, sqrt is not called every iteration; rather, it is calculated before hand in order to produce better performance.


What to take away from this, compilers are very good at what they do; however, they are not perfect. Yes premature optimizations are the root of all evil, but simple optimizations like this are perfectly reasonable.

This post has been edited by jjl: 24 April 2013 - 06:28 PM

Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Prime number checker w/loop error & failed cout

Posted 24 April 2013 - 05:35 AM

Just to be clear, I think jjl meant to write this as the first example in post #5:

int sqrt_n = sqrt(n);

while(i <= sqrt_n) {

}


Was This Post Helpful? 2
  • +
  • -

#8 yLif429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 27-March 13

Re: Prime number checker w/loop error & failed cout

Posted 24 April 2013 - 09:48 AM

Thanks! Will try, soon. Just didn't have time to keep messing with that chapter. Was trying to get ahead since my books show up soon.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Prime number checker w/loop error & failed cout

Posted 24 April 2013 - 06:29 PM

Quote

Just to be clear, I think jjl meant to write this as the first example in post #5:

Haha yeah, I should not post when I am that tired.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1