How to resolve the algorithm Successive prime differences step by step in the Visual Basic .NET programming language
How to resolve the algorithm Successive prime differences step by step in the Visual Basic .NET programming language
Table of Contents
Problem Statement
The series of increasing prime numbers begins: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... The task applies a filter to the series returning groups of successive primes, (s'primes), that differ from the next by a given value or values. Example 1: Specifying that the difference between s'primes be 2 leads to the groups: (Known as Twin primes or Prime pairs) Example 2: Specifying more than one difference between s'primes leads to groups of size one greater than the number of differences. Differences of 2, 4 leads to the groups: In the first group 7 is two more than 5 and 11 is four more than 7; as well as 5, 7, and 11 being successive primes. Differences are checked in the order of the values given, (differences of 4, 2 would give different groups entirely). Note: Generation of a list of primes is a secondary aspect of the task. Use of a built in function, well known library, or importing/use of prime generators from other Rosetta Code tasks is encouraged.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Successive prime differences step by step in the Visual Basic .NET programming language
Source code in the visual programming language
Imports System.Text
Module Module1
Function Sieve(limit As Integer) As Integer()
Dim primes As New List(Of Integer) From {2}
Dim c(limit + 1) As Boolean REM composite = true
REM no need to process even numbers > 2
Dim p = 3
While True
Dim p2 = p * p
If p2 > limit Then
Exit While
End If
For i = p2 To limit Step 2 * p
c(i) = True
Next
Do
p += 2
Loop While c(p)
End While
For i = 3 To limit Step 2
If Not c(i) Then
primes.Add(i)
End If
Next
Return primes.ToArray
End Function
Function SuccessivePrimes(primes() As Integer, diffs() As Integer) As List(Of List(Of Integer))
Dim results As New List(Of List(Of Integer))
Dim dl = diffs.Length
Dim i = 0
While i < primes.Length - dl
Dim group(dl) As Integer
group(0) = primes(i)
Dim j = i
While j < i + dl
If primes(j + 1) - primes(j) <> diffs(j - i) Then
GoTo outer REM continue the outermost loop
End If
group(j - i + 1) = primes(j + 1)
j += 1
End While
results.Add(group.ToList)
outer:
i += 1
End While
Return results
End Function
Function CollectionToString(Of T)(c As IEnumerable(Of T)) As String
Dim builder As New StringBuilder
builder.Append("[")
Dim it = c.GetEnumerator()
If it.MoveNext() Then
builder.Append(it.Current)
End If
While it.MoveNext()
builder.Append(", ")
builder.Append(it.Current)
End While
builder.Append("]")
Return builder.ToString
End Function
Sub Main()
Dim primes = Sieve(999999)
Dim diffsList = {({2}), ({1}), ({2, 2}), ({2, 4}), ({4, 2}), ({6, 4, 2})}
Console.WriteLine("For primes less than 1,000,000:-")
Console.WriteLine()
For Each diffs In diffsList
Console.WriteLine(" For differences of {0} ->", CollectionToString(diffs))
Dim sp = SuccessivePrimes(primes, diffs)
If sp.Count = 0 Then
Console.WriteLine(" No groups found")
Continue For
End If
Console.WriteLine(" First group = {0}", CollectionToString(sp(0)))
Console.WriteLine(" Last group = {0}", CollectionToString(sp(sp.Count - 1)))
Console.WriteLine(" Number found = {0}", sp.Count)
Console.WriteLine()
Next
End Sub
End Module
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Chapel programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the PostScript programming language
You may also check:How to resolve the algorithm Count in octal step by step in the OCaml programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm File input/output step by step in the PARI/GP programming language