Answer to Question #13766 in Visual Basic for with

Question #13766
Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers. Write a predicate to find the two prime numbers that sum up to a given even integer.
Example:
Input: 28
Output: 5,23
1
Expert's answer
2012-08-30T09:54:37-0400
Module Module1

Sub Main()
Dim lowerlimit As Integer
Dim upperlimit As Integer
Console.WriteLine("Input lower limit: ")
lowerlimit = Integer.Parse(Console.ReadLine())
Console.WriteLine("Input upper limit: ")
upperlimit = Integer.Parse(Console.ReadLine())
For i As Integer = lowerlimit To upperlimit
If i Mod 2 = 0 Then
Show(i)
End If
Next

Console.ReadLine()
End Sub

Private Sub Show(ByVal number As Integer)
Dim N As Integer = number
Dim isprime(N) As Boolean
For i As Integer = 2 To N - 1
isprime(i) = True
Next
For i As Integer = 2 To N
If (i * i < N) Then
If isprime(i) Then
For j As Integer = i To N
If i * j < N Then
isprime(i * j) = False
End If
Next
End If

End If
Next
Dim primes As Integer = 0

For i As Integer = 2 To N - 1
If isprime(i) Then
primes = primes + 1

End If
Next
Dim list(primes) As Integer
Dim nn As Integer = 0
For i As Integer = 0 To N - 1
If isprime(i) Then
nn = nn + 1
list(nn) = i
End If
Next

Dim left As Integer = 0
Dim right As Integer = primes - 1

While left <= right
If (list(left) + list(right) = N) Then
Exit While
ElseIf (list(left) + list(right) < N) Then
left = left + 1
Else
right = right - 1
End If

End While

If (list(left) + list(right) = N) Then
Console.WriteLine(N.ToString() + " = " + list(left).ToString() + " + " + list(right).ToString())
Else
Console.WriteLine(N.ToString() + " not expressible as sum of two primes")
End If
End Sub


End Module


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS