Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:21:12+00:00 2026-05-27T03:21:12+00:00

Possible Duplicate: Prime Number Formula I am trying to write some code in c#

  • 0

Possible Duplicate:
Prime Number Formula

I am trying to write some code in c# that will give me the nth prime number but once my code gets past 121 as a prime number it starts giving me incorrect numbers back.

Now this could be that i have based my code off the wrong algorithm but i wanted to ask here and see if there is something i have done wrong.

the code ask for nth prime 10001 – output: 43751 (which i know is wrong)

Any where here is my code.

int[] p;
int x = 0;
p = new int[10002];
for (int i = 0; i < 1000000; i++)
if (i % 2 != 0)
{
    if (i % 3 != 0)
    {
        if (i % 5 != 0)
        {
            if (i % 7 != 0)
            {
                p[x] = i;
                x++;
                if (x == 10001)
                {
                    Console.WriteLine("{0}", i);
                    break;
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:21:13+00:00Added an answer on May 27, 2026 at 3:21 am

    That’s not how the Sieve of Eratosthenes works, you should read this section from the Wikipedia article :

    • Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
    • Initially, let p equal 2, the first prime number.
    • Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
    • Find the first number greater than p in the list that is not marked; let p now equal this number (which is the next prime).
    • If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.

    So basically what you think is the limit (121, from your comments) is just an example they used in that animated .gif.

    here’s a C# implementation of this method :

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sieve
    {
        class Program
        {
            static void Main(string[] args)
            {
                int maxprime = int.Parse(args[0]);
                ArrayList primelist = sieve(maxprime);
    
                foreach (int prime in primelist)
                    Console.WriteLine(prime);
    
                Console.WriteLine("Count = " + primelist.Count);
                Console.ReadLine();
            }
    
            static ArrayList sieve(int arg_max_prime)
            {
                BitArray al = new BitArray(arg_max_prime, true);
    
                int lastprime = 1;
                int lastprimeSquare = 1;
    
                while (lastprimeSquare <= arg_max_prime)
                {
                    lastprime++;
    
                    while (!(bool)al[lastprime])
                        lastprime++;
    
                    lastprimeSquare = lastprime * lastprime;
    
                    for (int i = lastprimeSquare; i < arg_max_prime; i += lastprime)
                        if (i > 0)
                            al[i] = false;
                }
    
                ArrayList sieve_2_return = new ArrayList();
    
                for (int i = 2; i < arg_max_prime; i++)
                    if (al[i])
                        sieve_2_return.Add(i);
    
                return sieve_2_return;
            }
        }
    }
    

    Credits go to Rosetta Code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to determine if a number is a prime with regex? This
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Is Mono ready for prime time? I have a few questions that
Possible Duplicate: How to determine if a number is a prime with regex? Here
Possible Duplicate: Why should hash functions use a prime number modulus? Why is it
Possible Duplicate: Good Primer for Python Slice Notation I've been reading over some sample
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Factor a large number efficiently with gmp I know I already posted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.