Advertisement
Gatsky

Euler37

Sep 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 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 ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine(Enumerable.Range(10, 10000000)
  14.                 .Where(x => IsPrime(x)).Where(x =>EveryStubIsPrime(GetListOfNumbers(x))).Sum());
  15.         }
  16.  
  17.  
  18.         static bool IsPrime(int number)
  19.         {
  20.  
  21.             if (number == 1) return false;
  22.             if (number == 2) return true;
  23.  
  24.             var limit = Math.Ceiling(Math.Sqrt(number));
  25.  
  26.             for (int i = 2; i <= limit; ++i)
  27.             {
  28.                 if (number % i == 0) return false;
  29.             }
  30.             return true;
  31.         }
  32.  
  33.         static int RemoveFirst(int n)
  34.         {
  35.             var stringN = n.ToString();
  36.             return Convert.ToInt32(stringN.Remove(0, 1));
  37.         }
  38.         static int RemoveLast(int n)
  39.         {
  40.             var stringN = n.ToString();
  41.             return Convert.ToInt32(stringN.Remove(stringN.Length-1, 1));
  42.         }
  43.  
  44.         static List<int> GetListOfNumbers(int n)
  45.         {
  46.             var listOfStubs = new List<int>() {n};
  47.             var nn = n; //for deleting letters from left
  48.             while ((1 < n.ToString().Length)&&(1 < nn.ToString().Length ))
  49.             {        
  50.                 n = RemoveLast(n);
  51.                 listOfStubs.Add(n);
  52.                 nn = RemoveFirst(nn);
  53.                 listOfStubs.Add(nn);
  54.             }
  55.             return listOfStubs;
  56.         }
  57.  
  58.         static bool EveryStubIsPrime(List<int> stub)
  59.         {
  60.             return stub.All(x => IsPrime(x));
  61.         }
  62.        
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement