944,158 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13264
  • C++ RSS
Oct 14th, 2004
0

prime numbers

Expand Post »
i know how to find prime numbers of several numbers, does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime

int mai()//asks for input
{
int num;
cout << "enter number:";
cin >> num;

call IsPrime;
cout if prime or not
}

bool IsPrime(int n)
// precondition: n >= 0
// postcondition: returns true if n is prime, else returns false
// returns false if precondition is violated
{
if (n < 2) // 1 and 0 aren't prime
{ return false; // treat negative #'s as not prime
}
else if (2 == n) // 2 is only even prime number
{ return true;
}
else if (n % 2 == 0) // even, can't be prime
{ return false;
}
else // number is odd
{ int limit = int(sqrt(n) + 1); // largest divisor to check
int divisor = 3; // initialize to smallest divisor

// invariant: n has no divisors in range [2..divisor)

while (divisor <= limit)
{ if (n % divisor == 0) // n is divisible, not prime
{ return false;
}
divisor += 2; // check next odd number
}
return true; // number must be prime
}
}
any suggestions would greatly be appreciated
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cblue is offline Offline
24 posts
since Oct 2004
Oct 14th, 2004
0

Re: prime numbers

Greetings cblue,

> does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime
Yes, I do know, its quite possible too.
int main() {
	int num;
	cout << "enter number:";
	cin >> num;

	if (isPrime(num))
		cout << "Number is prime" << endl;
	else
		cout << "Number is not prime" << endl;

	return 0;
}

// Add isPrime code here
How did this work? Simple, the if statement is used to express decisions. Formally the syntax is:
if (expression)
	statement1
else
	statement2
Where the else is optional. The expression is evaluated; if it is true (if has a non-zero value), statement1, is executed. If it is false, (if has value of zero) and if there is an else part, statement2 is executed instead.

Since an if simply tests the numeric value of an expression, certain coding shortcuts are possible:
if (expression)
Instead of:
if (expression != 0)
If you have further questions, please feel free to ask. Hope this helps.

- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Apr 10th, 2011
0
Re: prime numbers
not bad...
my problem is i have to output the prime factors of the following users input.
How it is being done.....?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jaguarXZ(357) is offline Offline
1 posts
since Apr 2011
Apr 11th, 2011
0
Re: prime numbers
Try writing some pseudo-code for an algorithm. When you run into trouble with your own implementation, let us know.
Reputation Points: 216
Solved Threads: 111
Practically a Posting Shark
pseudorandom21 is offline Offline
877 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cstring to int with characters
Next Thread in C++ Forum Timeline: Help with Airline Seating Assignment (Array)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC