TizzyT

Deletable Primes Calculator - TizzyT

Oct 2nd, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.61 KB | None | 0 0
  1. Module DelPrimes
  2.     Sub Main()
  3.         Dim Primes As List(Of Integer) = Sieve(10000000)
  4.         Dim table(Primes(Primes.Count - 1)) As Boolean
  5.         Dim sum As Double = 17
  6.         For Each prime As Integer In Primes : table(prime) = True : Next
  7.         For i = 4 To Primes.Count - 1
  8.             If i > Primes.Count - 1 Then Exit For
  9.             Dim intString As String = Primes(i)
  10.             table(0) = False
  11.             For x = 0 To intString.Length - 1
  12.                 Dim crnt As String = intString.Remove(x, 1)
  13.                 If Not crnt.StartsWith("0") Then
  14.                     If table(crnt) = True Then
  15.                         table(0) = True
  16.                         Exit For
  17.                     End If
  18.                 End If
  19.             Next
  20.             If table(0) = False Then
  21.                 table(Primes(i)) = False
  22.                 Primes.RemoveAt(i)
  23.                 i -= 1
  24.             Else
  25.                 sum += Primes(i)
  26.             End If
  27.         Next
  28.         Console.WriteLine(Primes.Count & vbCrLf & sum)
  29.         Console.ReadKey()
  30.     End Sub
  31.     Function Sieve(ByVal limit As Integer) As List(Of Integer)
  32.         Dim sqrt As Integer, CrntNbr = 11
  33.         Sieve = {2, 3, 5, 7}.ToList
  34.         While CrntNbr < limit
  35.             sqrt = Math.Sqrt(CrntNbr)
  36.             For Each prime In Sieve
  37.                 If prime <= sqrt Then
  38.                     If CrntNbr Mod prime = 0 Then Exit For
  39.                 Else
  40.                     Sieve.Add(CrntNbr)
  41.                     Exit For
  42.                 End If
  43.             Next
  44.             CrntNbr += 2
  45.         End While
  46.     End Function
  47. End Module
Advertisement
Add Comment
Please, Sign In to add comment