欧拉计划 第7题
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?
class Program { private static List< long > primes = new List< long >(10000000); static void Main( string [] args) { primes.Add(2); for ( int i = 0; primes.Count < 10001; i++) { NextPrime(); } Console.WriteLine(primes.Last()); } private static long NextPrime() { long last = primes.Last(); long n = last + 1; while ( true ) { foreach ( var item in primes) { if (n % item == 0) { goto LoopOut; } } primes.Add(n); return n; LoopOut: n++; } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步