10001st Prime
Problem 7 Euler Project
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int t = Convert.ToInt32(Console.ReadLine()); for(int a0 = 0; a0 < t; a0++){ int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(NthPrime(n)); } } static long NthPrime(int n) { long i = 2; long j = 1; long prime = 0; while(true) { if (j < n) { if (IsPrime(i)) { prime = i; j++; } i++; } else break; } return prime; } static bool IsPrime(long n) { if(n%2==0 ) return false; long l = 3; while (l < n) { if (n % l == 0) return false; l++; } return true; } }
Live Code on Ide one
Comments
Post a Comment