So lately I got a little bored and stumbled upon Project Euler. It is basically a site with 250+ complex math/programming problems. It has been quite a bit of fun, even though I am only on the 3rd problem. I thought that i would post my VB(.NET 3.5) code that I used for the first 2 problems. If you don’t want to know the solutions, dont read further. Although, it is helpful to see the algorithm in other languages to make it work in your own code. For me, I think knowing what the final solution is would be best, so i can verify my algorithms worked before submitting. Anyway, below is problem 1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, ii, Multiples(0) As Integer
Dim X, Y As Double
X = 0
i = 0
Do Until X = 1000
If X / 3 = CInt(X / 3) Or X / 5 = CInt(X / 5) Then
ReDim Preserve Multiples(i)
Multiples(i) = X
Me.ListBox1.Items.Add(X)
i = i + 1
End If
X = X + 1
Loop
Y = 0
For ii = 0 To UBound(Multiples)
X = Multiples(ii)
Y = Y + X
Next
Me.Label1.Text = CStr(Y)
If Me.Label1.Text = "233168" Then
Me.Button2.Visible = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.WindowState = FormWindowState.Normal
Form2.Visible = True
Me.Visible = False
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
End Class
And for Problem 2:
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim X, Y, Z, Numbers(1), i, ii, Evens(0), p As Long
Dim EvenString As String
Dim Q As Double
X = 1
Y = 2
Z = 0
i = 2
p = 0
Numbers(0) = X
Numbers(1) = Y
EvenString = ""
Do Until Z >= 4000000
Z = X + Y
X = Y
Y = Z
If Z >= 4000000 Then
Exit Do
End If
ReDim Preserve Numbers(i)
Numbers(i) = Z
i = i + 1
Loop
For ii = 0 To UBound(Numbers)
Me.ListBox1.Items.Add(CStr(Numbers(ii)))
Next
For ii = 0 To UBound(Numbers)
X = Numbers(ii)
Q = X / 2
If Q = CInt(Q) Then
ReDim Preserve Evens(p)
Evens(p) = Numbers(ii)
Me.ListBox2.Items.Add(CStr(Numbers(ii)))
p = p + 1
End If
Next
X = 0
Y = 0
For ii = 0 To UBound(Evens)
X = Evens(ii)
Y = Y + X
Next
Me.Label5.Text = CStr(Y)
If Me.Label5.Text = "4613732" Then
Me.Button2.Visible = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form3.WindowState = FormWindowState.Normal
Form3.Visible = True
Me.Visible = False
End Sub
Private Sub Problem1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Problem1ToolStripMenuItem.Click
Form1.WindowState = FormWindowState.Normal
Me.Visible = False
Form1.Visible = True
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
Form1.Close()
End Sub
End Class
More to come… Enjoy!
