Rob
Rob

Reputation: 21

Project Euler 3

I have been trying to solve question 3 on project euler with the following vb code but I do not under stand why it is not working. Can someone please point me in the right direction?

Sub Main()
    Dim p As Int64 = 600851475143
    Dim y As Integer

    For i As Int64 = p / 2 To 1 Step -1
        If p Mod i = 0 Then
            y = 0
            For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
                If i Mod n = 0 Then
                    y = y + 1
                End If

            Next
            If y = 0 Then

                Console.WriteLine(i)
                Console.ReadLine()

            End If

        End If

    Next

End Sub

The question is "The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?"

Upvotes: 0

Views: 404

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
    If i Mod n = 0 Then

You start with n=1. Every number divides evenly by 1.

(so y = y+1 every time, and If y = 0 Then can never happen).

Upvotes: 3

Related Questions