Wrong answer on c# application, finding the n prime of a list of prime numbers.

Posted by user300484 on Stack Overflow See other posts from Stack Overflow or by user300484
Published on 2010-03-24T04:59:39Z Indexed on 2010/03/24 5:03 UTC
Read the original article Hit count: 285

Filed under:

Hello, I am new to C# and I am practicing by trying to solve this easy C# problem. This application will receive an "n" number. After receiving this number, the program has to show the n prime of the list of primes. For example, if the user enters "3", the program is supposed to display "5", because 5 is the third prime starting at 2. I koow that something is wrong with my code but I dont know where is the problem and how I can fix it. Can you please tell me? Thank you :P

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Determinar el n-esimo primo.");
            long n = Convert.ToInt64(Console.ReadLine()); // N lugar de primos
            long[] array = new long[n];
            long c=0;
            while (c >= 2) 
            { 
                if(siprimo(c++) == true)
                    for (long i = 0; i < n; i++)
                    {
                        array[i] = c;

                    }

            }


            Console.WriteLine(array[n - 1]);
            Console.ReadLine();


        }
        static private bool siprimo(long x)
        {
            bool sp = true;
            for (long k = 2; k <= x / 2; k++)
                if (x % k == 0)
                    sp = false;
            return sp;
        }

    }
}

© Stack Overflow or respective owner

Related posts about c#