Advertisement
Gatsky

Euler3

Sep 15th, 2019
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Euler3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine(GetFactors(600851475143).Where(x => IsPrime(x)).Max());
  14.         }
  15.         static bool IsPrime(int number)
  16.         {
  17.  
  18.             if (number == 1) return false;
  19.             if (number == 2) return true;
  20.  
  21.             var limit = Math.Ceiling(Math.Sqrt(number));
  22.  
  23.             for (int i = 2; i <= limit; ++i)
  24.             {
  25.                 if (number % i == 0) return false;
  26.             }
  27.             return true;
  28.         }
  29.  
  30.         static List<int> GetFactors(long number)
  31.         {
  32.             int currentFactor = 2;
  33.             List<int> result = new List<int>();
  34.             while (number > 1)
  35.             {
  36.                 if (number % currentFactor == 0)
  37.                 {
  38.                     result.Add(currentFactor);
  39.                     number /= currentFactor;
  40.                     currentFactor = 2;
  41.                 }
  42.                 else
  43.                     currentFactor++;
  44.             }
  45.             return result;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement