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

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

Related

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

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

is enumeration do faster

i want to konw which code is more faster.
compare this two code
Dim x As Boolean
Dim y As Boolean
If x = True Then
If y = True Then
Else
End If
Else
If y = True Then
Else
End If
End If
'=============================================
Enum xx
o = 0
x = 1
xy = 2
y = 3
End Enum
Sub Script_validate()
Dim nn As xx
If nn.o Then
ElseIf nn.x Then
ElseIf nn.xy Then
ElseIf nn.y Then
End If
End Sub
which code is faster and which code had low memory usage

How to count and output total number of loops in visual basic console application?

I am learning visual basic and wrote a simple visual basic console application to do "half or triple plus one" calculations and the console app works but I would also like to count and display the total number of loops run to get to the solution. Here is my code:
Sub Main()
Dim n As Double
Console.WriteLine("Enter a starting number.")
n = Console.ReadLine()
Do While n <> 1
Do While n > 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
If n = 1 Then
Console.WriteLine(
n = Console.ReadLine()
End If
Loop
Console.ReadLine()
End Sub
I can't seem to find anything that counts and displays the number of loops.
If I'm not misunderstanding you, The problem solves Introducing a new variable named j, and the final code results in
Sub Main()
Dim n As Double
Dim j as integer
Console.WriteLine("Enter a starting number.")
n = Console.ReadLine()
j = 0
Do While n <> 1
Do While n > 1
'Every Time i enter in the cicle, the "operation counter" increses by one
j = j + 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
If n = 1 Then
Console.WriteLine(
n = Console.ReadLine()
End If
Loop
Console.WriteLine("I make " & j & " operations to get here!")
Console.ReadLine()
End Sub
Sub Main()
Dim n As Double
Console.Write("Enter a starting number: ")
n = Convert.ToDouble(Console.ReadLine())
Console.WriteLine()
Dim loopsCounter As Integer = 0
Do While n > 0
Do While n > 1
loopsCounter += 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
Console.WriteLine()
Console.WriteLine(String.Format("{0} loops to result.", loopsCounter))
If n = 1 Then
Console.WriteLine()
Console.Write("Enter another number: ")
n = Convert.ToDouble(Console.ReadLine())
loopsCounter = 0
End If
Loop
Console.ReadLine()
End Sub

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