Advertisement
Gatsky

Euler47

Sep 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 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 Эйлер47
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var numbersAndFactors = new Dictionary<int, List<int>>();
  14.             foreach (var e in Enumerable.Range(0, 200000))
  15.             {
  16.                 numbersAndFactors.Add(e, GetFactors(e));            
  17.             }  
  18.             var dictAfterSelection = numbersAndFactors.Where(x => ContainsFourPrimeFactors(x.Value));
  19.             var keysAfterSelection = dictAfterSelection.Select(x => x.Key).ToArray();
  20.            
  21.             for(int index = 0; index<keysAfterSelection.Length - 3; index++)
  22.             {
  23.                 if ((keysAfterSelection[index] +1 == keysAfterSelection[index + 1])
  24.                     && (keysAfterSelection[index + 1] +1== keysAfterSelection[index + 2])
  25.                     && (keysAfterSelection[index + 2] +1== keysAfterSelection[index + 3]))
  26.                     Console.WriteLine(keysAfterSelection[index]);
  27.             }
  28.         }
  29.        
  30.  
  31.         static List<int> GetFactors(int number)
  32.         {
  33.             int currentFactor = 2;
  34.             List<int> result = new List<int>();  
  35.             while (number > 1)
  36.             {
  37.                 if (number % currentFactor == 0)
  38.                 {
  39.                     result.Add(currentFactor);
  40.                     number /= currentFactor;
  41.                     currentFactor = 2;
  42.                 }
  43.                 else
  44.                     currentFactor++;
  45.             }
  46.             return result;
  47.         }
  48.  
  49.  
  50.         static bool ContainsFourPrimeFactors(List<int> factors)
  51.         {
  52.             var noRepetitionsFactors = factors.Distinct();
  53.             if (noRepetitionsFactors.Count() == 4) return true;
  54.             return false;
  55.         }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement