Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(Enumerable.Range(10, 10000000)
- .Where(x => IsPrime(x)).Where(x =>EveryStubIsPrime(GetListOfNumbers(x))).Sum());
- }
- static bool IsPrime(int number)
- {
- if (number == 1) return false;
- if (number == 2) return true;
- var limit = Math.Ceiling(Math.Sqrt(number));
- for (int i = 2; i <= limit; ++i)
- {
- if (number % i == 0) return false;
- }
- return true;
- }
- static int RemoveFirst(int n)
- {
- var stringN = n.ToString();
- return Convert.ToInt32(stringN.Remove(0, 1));
- }
- static int RemoveLast(int n)
- {
- var stringN = n.ToString();
- return Convert.ToInt32(stringN.Remove(stringN.Length-1, 1));
- }
- static List<int> GetListOfNumbers(int n)
- {
- var listOfStubs = new List<int>() {n};
- var nn = n; //for deleting letters from left
- while ((1 < n.ToString().Length)&&(1 < nn.ToString().Length ))
- {
- n = RemoveLast(n);
- listOfStubs.Add(n);
- nn = RemoveFirst(nn);
- listOfStubs.Add(nn);
- }
- return listOfStubs;
- }
- static bool EveryStubIsPrime(List<int> stub)
- {
- return stub.All(x => IsPrime(x));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement