Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: nested for ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 10 Topic 45760 of 47032
Post > Topic >>

Re: nested for loop problem

by MiB <Michael.Boehnisch@[EMAIL PROTECTED] > May 6, 2008 at 09:12 AM

On 5 Mai, 20:42, notahipee <werldpe...@[EMAIL PROTECTED]
> wrote:
> Would someone be able to tell me why this isn't working. The nested
> for loops seem correctly coded to me. I would appreciate any input.

The algorithm you use for generating the primes is very inefficient.
However, bug fixing your code with minimal changes only, try this one.
It still misidentifies 0 and 1 as prime numbers (enter 0 as start
value...).

#include <iostream>

int main ()
{
	unsigned int a, b;

	std::cout << "Enter two integers ";
	std::cin >> a >> b;

	for ( unsigned int c = a; c <= b; c++ ) {
		bool isprime = true;
		for( unsigned int d = 2; d * d <= c; d++ ) {
			if ( c % d == 0 ) {
				isprime = false;
				break;
			}
		}
		if ( isprime ) std::cout << c << ' ';
	}
	std::cout << "are the prime numbers in the range " << a << " to " <<
b << std::endl;

	return 0;
}
 




 10 Posts in Topic:
nested for loop problem
notahipee <werldpeace@  2008-05-05 11:42:26 
Re: nested for loop problem
"Victor Bazarov"  2008-05-05 14:47:15 
Re: nested for loop problem
red floyd <no.spam@[EM  2008-05-05 11:50:30 
Re: nested for loop problem
Christopher <cpisz@[EM  2008-05-05 11:53:08 
Re: nested for loop problem
notahipee <werldpeace@  2008-05-05 11:56:57 
Re: nested for loop problem
notahipee <werldpeace@  2008-05-05 12:01:05 
Re: nested for loop problem
notahipee <werldpeace@  2008-05-05 12:05:13 
Re: nested for loop problem
notahipee <werldpeace@  2008-05-05 12:17:40 
Re: nested for loop problem
Puppet_Sock <puppet_so  2008-05-05 13:25:56 
Re: nested for loop problem
MiB <Michael.Boehnisch  2008-05-06 09:12:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:18:18 CDT 2008.