3 Replies - 12204 Views - Last Post: 27 December 2009 - 03:37 AM Rate Topic: -----

#1 lostttinc++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 09

Finding the Nth prime number

Posted 27 December 2009 - 02:58 AM

else if (toupper(option)=='C')
		{
			int k; // to store the Nth number
			int number; // to store the final number
			int count; // loop counter
			int i; // natural numbers

			cout << "Please enter which abundant number you would like to find." << endl;
			cout << "	(i.e. A is the Nth abundant number, so you would enter 'N'" << endl;
			cin >> k;
				for (count=0; count<k; count++)
				{
					for (i=1; i<10000000; i++)
					{
						if (perfectTest(i)==1)
							number=i;
					}
				}
				cout << number << " is the " << k << "th abundant number." << endl << endl;
		}





int perfectTest(int n)
{
	int divisor;
	int sum=0;
	int status;
	
	for (divisor=1; divisor<n; divisor++)
	{
		if (n%divisor==0)
		{
			sum=sum+divisor;
		}
	}

	if (sum==n)
	{
		status = 0;
	}
	else if (sum<n)
	{
		status = -1;
	}
	else if (sum>n)
	{
		status = 1;
	}
	return status;
}



I just included the relevent parts of my code, please let me know if more of it is needed. When I run the program, nothing happens after I enter in a number. I think there might be something wrong with my loops, but can't really figure it out.

This post has been edited by lostttinc++: 27 December 2009 - 02:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the Nth prime number

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Finding the Nth prime number

Posted 27 December 2009 - 03:08 AM

for (i=1; i<10000000; i++)



This will be executed for 'k' times, which will take MINUTES, even on a dual-core machine. So your code works, but the loops are too big and you have to wait, wait and wait. Also, you should declare 'i' as a long integer when you are working with big numbers.

This post has been edited by sarmanu: 27 December 2009 - 03:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Finding the Nth prime number

Posted 27 December 2009 - 03:09 AM

When you have found the first value for "number" here:
number=i;
did you want the loop to end or to keep looping?

At the moment it just keeps looping.
Was This Post Helpful? 0
  • +
  • -

#4 lostttinc++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 09

Re: Finding the Nth prime number

Posted 27 December 2009 - 03:37 AM

I figured out what was wrong with my loops; I was making things much more complicated than they needed to be.

Here's my final code:

else if (toupper(option)=='C')
		{
			int k; // to store the Nth number
			int number=1; // to store the final number
			int perfectCount=0; //
			int i; // natural numbers

			cout << "Please enter which abundant number you would like to find." << endl;
			cout << "	(i.e. A is the Nth abundant number, so you would enter 'N'" << endl;
			cin >> k;
				
					for (i=1; perfectCount<k; i++)
					{
						if (perfectTest(i)==1)
						{
							number=i;
							perfectCount++;
						}
						

				}
				cout << number << " is the " << k << "th abundant number." << endl << endl;
		}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1