zoukankan      html  css  js  c++  java
  • 欧拉计划 第3题

    The prime factors of 13195 are 5, 7, 13 and 29.

    What is the largest prime factor of the number 600851475143 ?

    class Program
        {
            private static List<long> primes = new List<long>(10000);
    
            static void Main(string[] args)
            {
                /*
                 * The prime factors of 13195 are 5, 7, 13 and 29.
                 * What is the largest prime factor of the number 600851475143 ?
                 */
                const long number = 600851475143;
                primes.Add(2);
                long max = 2;
                long x = number;
                for (long i = 2; i <= x; i = NextPrime())
                {
    			    if(number % i == 0)
    			    {
    			        x /= i;
    			        max = i;
    			    }
    			}
                Console.WriteLine(max);
    
            }
    
            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++;
                }
            }
        }
    

  • 相关阅读:
    git简单使用
    简单Spring和mybatis整合配置文件
    ASP.NET程序开发范例宝典
    C# DataSet和DataTable详解
    AOP
    匿名内部类
    数据库事务
    mybatis
    线程池
    单例模式
  • 原文地址:https://www.cnblogs.com/kiminozo/p/2156289.html
Copyright © 2011-2022 走看看