What's Here?
- Members: 119,059
- Replies: 436,213
- Topics: 67,433
- Snippets: 2,417
- Tutorials: 641
- Total Online: 1,471
- Members: 70
- Guests: 1,401
Who's Online?
|
Welcome to Dream.In.Code |
|
|
Getting C# Help is Easy!
Join 119,059 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,471 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
|
Get a prime number by the position it is in, in the prime number sequence.
|
Submitted By: RodgerB
|
|
|
Rating:
|
|
Views: 410 |
Language: C#
|
|
Last Modified: May 11, 2008 |
Instructions:
1) Copy and paste the functions into a class.
2) Read how to call them. |
Snippet
// The function we will be using to get the prime
// by its index in the prime number sequence.
public static int getPrime(int index)
{
int i = 2, counter = 0, val = 0;
while (counter != index)
{
if (isPrime(i))
{
val = i;
counter++;
}
i++;
}
return val;
}
// Function to check if a number is prime.
private static bool isPrime(int n)
{
for (int i = 2; i < n; i++)
if ((n % i) == 0)
return false;
return true;
}
// Example Usage:
MessageBox.Show(getPrime(10).ToString());
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|