My vb.NET project input doesn't in my primepairs function . how can I fix them? - vb.net

Sub PrimePair()
Dim n As Integer
Dim count As Integer = 0
Console.WriteLine(count)
End Sub
Public Function PrimePairs(ByVal n As Integer, ByVal n2 As Long) As Integer
Dim count As Integer = 0
Console.ReadLine()
If n Mod 2 = 0 Then
For i = 1 To (n / 2) + 1
n2 = CLng(n - i)
If IsPrime(CLng(i)) And IsPrime(n2) = True Then
count += 1
End If
Next
Else
n2 = n - 2
If IsPrime(n2) = True Then
count = +1
End If
End If
Console.WriteLine(count)
Return n
End Function
End Module>
I can't run my code without sub. I created two functions, but the inputs I entered do not return in functions and do not print on the screen, I hope I can solve it, thanks for your attention. My project calculates how many different ways it prints the entered input value as a sum of prime numbers. About the Goldbach conjecture

Related

BC42105: Function '<procedurename>' doesn't return a value on all code paths

I tried all possible solutions and they didn't work. Any help would be appreciated.
Function '' doesn't return a value on all code paths is my error
Public Function Isprime(n2 As Long)
Dim n, i As Integer
Dim b As Boolean
Console.WriteLine("enter a no : \")
n = Console.ReadLine()
i = 2
b = True
While i < n
If n Mod i = 0 Then
b = False
End If
i = i + 1
End While
If b Then
Console.WriteLine("prime no")
Else
Console.WriteLine("not prime no\")
End If
Console.ReadLine()
End Function
Public Function PrimePairs(ByVal n As Long, ByVal n2 As Long) As Integer
Dim count As Integer = 0
Console.ReadLine()
If n Mod 2 = 0 Then
For i = 1 To (n / 2) + 1
n2 = n - i
If Isprime(i) And Isprime(n2) = True Then
count += 1
End If
Next
Else
n2 = n - 2
If Isprime(n2) = True Then
count = +1
End If
End If
Console.WriteLine(count)
Return n
End Function
End Module
You function does not return a value and is quite convoluted. It does I/O and does a prime test at the same time. Separate logic from I/O.
Public Function IsPrime(n As Long) As Boolean
n = Math.Abs(n) ' Allows to consider negative prime numbers
If n < 2 Then ' Disallows -1, 0, 1
Return False
End If
Dim i As Long
i = 2
While i < n ' Note that for n = 2 we don't enter the loop and thus return True.
If n Mod i = 0 Then
Return False
End If
i += 1
End While
Return True
End Function
Also, it is missing a return type. Always work with Option Explicit On for better code quality.
Note that you can return when you hit the first prime factor. There is no point in continuing the loop then.
But there are ways to optimize this. For example we could test the divisibility by 2 separately and then test only odd divisors and it is enough to test divisors up to the square root of n.
Public Function IsPrime(n As Long) As Boolean
n = Math.Abs(n)
If n = 2 Then
Return True
End If
If n < 2 Or n Mod 2 = 0 Then
Return False
End If
Dim i As Long = 3
Dim limit As Long = CLng(Math.Sqrt(n))
While i <= limit
If n Mod i = 0 Then
Return False
End If
i += 2
End While
Return True
End Function

Where is my error in this visual basic Bubblesort

I am currently making a highscore table - reading the times from a .csv file and sorting them from lowest to highest. The list only becomes partially sorted after the code runs.
All the data inputs correctly but when it goes to sort it sorts the data out incorrectly.
Private Sub BeginnerProcess(ByRef player() As pe_player, ByVal x As Integer)
Dim i As Integer
Dim j As Integer
Dim temp As Object
For i = x To 0 Step -1
For j = 0 To i - 1
If player(j).playerTime > player(j + 1).playerTime Then
temp = player(j)
player(j) = player(j + 1)
player(j + 1) = temp
End If
Next
Next
Dim k As Integer
For k = 1 To x
player(k).position = k
Next
End Sub
Here's the output
Leaderboard
Adapting the classic bubble-sort to your case, I think i should be something like the code below:
For i = 0 To x - 1
For j = i + 1 To x
If player(i).playerTime > player(j).playerTime Then
temp = player(i)
player(i) = player(j)
player(j) = temp
End If
Next
Next

Automatic Calculation with given numbers

I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)

array without any duplicate value

the code to generate no. of arrays from one is working..I'm try to make some change to it like below
Function myarray(ByVal arra1() As Integer, ByVal arran() As Integer, ByVal arrNumber As Integer) As Integer()
arran = arra1.Clone()
For i As Integer = 0 To arra1.Length - 1
If i = (arrNumber - 1) Then ' IF arrNumber is 1 then +1 to index 0, If it is 2 then +1 to index 1
arran(i) = arra1(i) + 1
'If there are two duplicate value make on of them zero at a time
For k = 0 To arran.Length - 1
For j = k + 1 To arran.Length - 1
If arran(k) = arran(j) Then
arran(k) = 0
End If
'make any value great than 11 zero
If arran(i) > 11 Then
arran(i) = 0
End If
Next
Next
Else
arran(i) = arra1(i)
End If
Next
'Print the array
For i = 0 To arran.Length - 1
Console.Write(arran(i) & " ")
Next
Console.WriteLine()
Return arran
End Function
what I really need is to decompose for example {1,4,5,5} to be {1,4,0,5} and then {1,4,5,0} the above code generate only {1,4,0,5}
I haven't tested this, but I believe the following code will do what you want. Based on your comments, I've changed the function to return all resulting arrays as an array of arrays, rather than requiring the index to change as an input and returning one array. I also ignored matches of 0, as the conditions you describe don't seem designed to handle them. Because of it's recursion, I think this approach will successfully handle input such as {3, 3, 3, 3}.
Public Function jaggedArray(ByVal inputArray() As Integer) As Integer()()
If inputArray Is Nothing Then
Return Nothing
Else
Dim resultArrays()(), i, j As Integer
Dim arrayMax As Integer = inputArray.GetUpperBound(0)
If arrayMax = 0 Then 'prevents errors later if only one number passed
ReDim resultArrays(0)
If inputArray(0) > 11 Then
resultArrays(0) = {1}
ElseIf inputArray(0) = 11 Then
resultArrays(0) = {0}
Else
resultArrays(0) = {inputArray(0) + 1}
End If
Return resultArrays
End If
For i = 0 To arrayMax
Dim tempArray() As Integer = inputArray.Clone
For j = 0 To arrayMax
If tempArray(j) > 11 Then
tempArray(j) = 0
End If
Next
If tempArray(i) = 11 Then
tempArray(i) = 0
Else
tempArray(i) += 1
End If
splitArray(resultArrays, tempArray)
Next
Return resultArrays
End If
End Function
Private Sub splitArray(ByRef arrayList()() As Integer, ByVal sourceArray() As Integer)
Dim x, y As Integer 'positions of matching numbers
If isValid(sourceArray, x, y) Then
If arrayList Is Nothing Then
ReDim arrayList(0)
Else
ReDim Preserve arrayList(arrayList.Length)
End If
arrayList(arrayList.GetUpperBound(0)) = sourceArray
Else
Dim xArray(), yArray() As Integer
xArray = sourceArray.Clone
xArray(x) = 0
splitArray(arrayList, xArray)
yArray = sourceArray.Clone
yArray(y) = 0
splitArray(arrayList, yArray)
End If
End Sub
Private Function isValid(ByRef testArray() As Integer, ByRef match1 As Integer, ByRef match2 As Integer) As Boolean
For i As Integer = 0 To testArray.GetUpperBound(0) - 1
If testArray(i) > 11 Then
testArray(i) = 0
End If
For j As Integer = i + 1 To testArray.GetUpperBound(0)
If testArray(j) > 11 Then
testArray(j) = 0
End If
If testArray(i) = testArray(j) AndAlso testArray(i) > 0 Then 'added second test to prevent infinite recursion
match1 = i
match2 = j
Return False
End If
Next
Next
match1 = -1
match2 = -1
Return True
End Function

Developing Fibonacci Series in VB

I am trying to write a code for Fibonacci series in VB, but some of the values in my series are incorrect. Can somebody help me with the code?
Below is what I have so far.
Private Function FibNumber(number As Integer) As Integer
If (number > 2) Then
FibNumber = (FibNumber(number - 2) + FibNumber(number - 1))
Else
FibNumber = 1
End If
End Function
Private Sub command1_click()
Dim x As Integer
x = Text1.Text
Call FibNumber(number)
End Sub
Well, I did a quick search and I came up with the following in the first couple of results:
Private Function FibNumber(number As Integer) As Integer
If (number > 2) Then
FibNumber = (FibNumber(number - 2) + FibNumber(number - 1))
Else
FibNumber = 1
End If
End Function
I know this is way old, but I think the issue could be with how compgeek is calling the function.
Instead of:
Call FibNumber(number)
It should be:
Call FibNumber(x)
My solution:
Private Function FibNumber(number As Integer) As Integer
If (number > 2) Then
FibNumber = (FibNumber(number - 2) + FibNumber(number - 1))
Else
FibNumber = 1
End If
End Function
Private Sub command1_click()
Dim x As Integer
x = Text1.Text
Call FibNumber(number)
End Sub
It's a Java function, and believe me; Fibonacci wont get much more faster or complex than
this particular version. It is optimized to operate at about 100 times faster than the original recursive one.
Tip: You might need to change maxN to extend parameter length!
For example if you want to input numbers between 0 and 199, you must increase the maxN to 200
static final int maxN = 72;
static long knownF[] = new long[maxN];
static long F(int i) {
if (knownF[i] != 0) {
return knownF[i];
}
long t = i;
if (i < 0) {
return 0;
}
if (i > 1) {
t = F(i - 1) + F(i - 2);
}
return knownF[i] = t;
}
Module Module1
Sub Main()
Console.WriteLine("The Fibonacci Series")
Console.WriteLine("Enter how many elements-")
Dim n As Integer = Console.ReadLine
If (n = 1) Then
Dim a As Integer = 1
Console.WriteLine("{0}", a)
Else
Dim a As Integer = 1
Dim b As Integer = 2
Console.WriteLine("{0}", a)
Console.WriteLine("{0}", b)
Dim i As Integer = 1
While (i < n - 1)
Dim c As Integer = a + b
Console.WriteLine(" {0}", c)
a = b
b = c
i = i + 1
End While
End If
Console.ReadKey()
End Sub
End Module