# Finding the Nth prime number

Page 1 of 1

## 3 Replies - 19174 Views - Last Post: 27 December 2009 - 03:37 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=147158&amp;s=569b90e3833d7cde1ef4cec7bd67e25f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lostttinc++

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 967
• 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

### #3 janotte

• code > sword

Reputation: 991
• 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.

### #4 lostttinc++

• New D.I.C Head

Reputation: 0
• 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;
}
```