How to find the first 10 prime Fibonacci numbers in VB.NET

1 Answer

0 votes
Imports System

Module PrimeFibonacci

    Function IsPrime(n As ULong) As Boolean
        If n < 2 Then Return False
        If n = 2 OrElse n = 3 Then Return True
        If n Mod 2 = 0 Then Return False

        Dim i As ULong = 3
        While i * i <= n
            If n Mod i = 0 Then Return False
            i += 2
        End While

        Return True
    End Function

    Function NextFib(state As ULong()) As ULong
        Dim f As ULong = state(0)
        Dim nextVal As ULong = state(0) + state(1)
        state(0) = state(1)
        state(1) = nextVal

        Return f
    End Function

    Sub Main()
        Dim fibState As ULong() = {1UL, 1UL}
        Dim count As Integer = 0

        While count < 10
            Dim f As ULong = NextFib(fibState)
            If IsPrime(f) Then
                Console.Write(f & " ")
                count += 1
            End If
        End While
    End Sub

End Module



' run:
'
' 2 3 5 13 89 233 1597 28657 514229 433494437 
'


 



answered 9 hours ago by avibootz
...