Project Euler #7: 10001st prime

  • + 0 comments

    Woah. That was easier than I thought. I don't know maybe it's because my solution is simple. C# solution

    1. I get the list of primes for the first 10000.
    2. Just put.
    public static void getPrimes(List<int> Primes)
        {
            for(int i = 3; Primes.Count <= 10000; i+=2)
            {
                bool isPrime = true;
                if(i == 3) {Primes.Add(3);}
                foreach(int prime in Primes)
                {
                    if( i % prime == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if(isPrime)
                {
                    Primes.Add(i);
                }
            }
        }
    
    static void Main(String[] args) {
            int t = Convert.ToInt32(Console.ReadLine());
            //get primes
            List<int> Primes = new List<int>();
            Primes.Add(2);
            //add the primes to the list
            myFunc.getPrimes(Primes);
            for(int a0 = 0; a0 < t; a0++){
                int n = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(Primes[n-1]);
            }
        }