using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; //输出结果: //最大质数为:6857 //所话费的时间为:00:00:02.6578500 //The prime factors of 13195 are 5, 7, 13 and 29. //What is the largest prime factor of the number 600851475143 ? //寻找最大质因数 namespace Problem3 { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); long flag = 0; long target = 600851475143; for (long i = (int)Math.Sqrt(target); i > 0; i--) { if (IsPrime(target)) { flag = target; break; } if (target%i==0&&IsPrime(i)) { flag = i; break; } } Console.WriteLine("最大质数为:{0}", flag); sw.Stop(); Console.WriteLine("所话费的时间为:{0}",sw.Elapsed); } public static bool IsPrime(long l) { for (long i = 2; i < l; i++) { if (l % i == 0) { return false; } } return true; } } }