Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module TriangularNbr 'My solution to Project Euler Problem 12
- Sub Main()
- Console.WriteLine(TriangularIndex(500))
- End Sub
- Private Function TriangularIndex(ByVal limit As UInteger) As ULong
- Dim n As ULong = 1
- Dim lnum = NbrOfDivisors(n), rnum As ULong = NbrOfDivisors(n + 1)
- While lnum * rnum < limit
- n += 1 : lnum = rnum : rnum = NbrOfDivisors(n + 1)
- End While : Return (n * (n + 1)) / 2
- End Function
- Private Function NbrOfDivisors(ByVal n As UInteger) As ULong
- Dim count = 0, p As UInteger = 3
- NbrOfDivisors = 1
- If n Mod 2 = 0 Then n /= 2
- While n Mod 2 = 0 : count += 1 : n /= 2 : End While
- NbrOfDivisors = NbrOfDivisors * (count + 1)
- While Not n = 1
- count = 0
- While n Mod p = 0 : count += 1 : n /= p : End While
- NbrOfDivisors = NbrOfDivisors * (count + 1) : p += 2
- End While
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement