Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace DeletablePrimesCSharp
- {
- class DelPrimes
- {
- static void Main(string[] args)
- {
- List<int> Primes = Sieve(10000000);
- bool[] Table = new bool[Primes[Primes.Count - 1] + 1];
- double sum = 17;
- foreach (int prime in Primes)
- {
- Table[prime] = true;
- }
- for (int i = 4; i <= Primes.Count - 1; i++)
- {
- if (i > Primes.Count - 1)
- break;
- string intString = Convert.ToString(Primes[i]);
- Table[0] = false;
- for (int x = 0; x <= intString.Length - 1; x++)
- {
- string crnt = intString.Remove(x, 1);
- if (!(crnt.StartsWith("0")))
- {
- if (Table[int.Parse(crnt)] == true)
- {
- Table[0] = true;
- break;
- }
- }
- }
- if (Table[0] == false)
- {
- Table[Primes[i]] = false;
- Primes.RemoveAt(i);
- i--;
- }
- else
- {
- sum += Primes[i];
- }
- }
- Console.WriteLine(Primes.Count + System.Environment.NewLine + sum);
- Console.ReadKey();
- }
- static List<int> Sieve(int limit)
- {
- List<int> Primes = new List<int> { 2, 3, 5, 7 };
- int sqrt = 0;
- int crntNbr = 11;
- while (crntNbr < limit)
- {
- sqrt = (int)Math.Sqrt(crntNbr);
- foreach (int prime in Primes)
- {
- if (prime <= sqrt)
- {
- if (crntNbr % prime == 0)
- break;
- }
- else
- {
- Primes.Add(crntNbr);
- break;
- }
- }
- crntNbr += 2;
- }
- return Primes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment