Results 1 to 11 of 11

Thread: VB.NET Project Euler

  1. #1
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    VB.NET Project Euler

    Thought I'd take a stab at the Project Euler questions. If anyone has any suggestions on how to improve these I'm all ears.

    1. Add all the natural numbers below one thousand that are multiples of 3 or 5.

    vb.net Code:
    1. Private Function Problem1() As Integer
    2.         Return Enumerable.Range(1, 999) _
    3.             .Where(Function(x) {3, 5}.Any(Function(y) x Mod y = 0)) _
    4.             .Sum()
    5.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    2. Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.

    At this point I see that I'm going to need to make some reusable classes for generating numbers as I see Fibonacci and Primes mentioned several times in the first couple of questions.

    Here's my generator class.

    vb.net Code:
    1. Public MustInherit Class Yielder(Of T)
    2.     Implements IEnumerable(Of T)
    3.     Implements IEnumerator(Of T)
    4.  
    5.     Private _current As T
    6.  
    7.     Public Sub New()
    8.     End Sub
    9.  
    10.     Protected MustOverride Function [Yield]() As T
    11.  
    12.     Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
    13.         Return Me
    14.     End Function
    15.  
    16.     Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
    17.         Return Me
    18.     End Function
    19.  
    20.     Public ReadOnly Property Current As T Implements System.Collections.Generic.IEnumerator(Of T).Current
    21.         Get
    22.             Return _current
    23.         End Get
    24.     End Property
    25.  
    26.     Public ReadOnly Property Current1 As Object Implements System.Collections.IEnumerator.Current
    27.         Get
    28.             Return _current
    29.         End Get
    30.     End Property
    31.  
    32.     Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
    33.         _current = [Yield]()
    34.         Return True
    35.     End Function
    36.  
    37.     Public Sub Reset() Implements System.Collections.IEnumerator.Reset
    38.         Throw New NotImplementedException()
    39.     End Sub
    40.  
    41. #Region "IDisposable Support"
    42.     Private disposedValue As Boolean ' To detect redundant calls
    43.  
    44.     ' IDisposable
    45.     Protected Overridable Sub Dispose(disposing As Boolean)
    46.         If Not Me.disposedValue Then
    47.             If disposing Then
    48.  
    49.             End If
    50.  
    51.         End If
    52.         Me.disposedValue = True
    53.     End Sub
    54.  
    55.     Public Sub Dispose() Implements IDisposable.Dispose
    56.         Dispose(True)
    57.         GC.SuppressFinalize(Me)
    58.     End Sub
    59. #End Region
    60.  
    61. End Class

    My Fibonacci generator class

    vb.net Code:
    1. Public Class Fibonacci
    2.     Inherits Yielder(Of Long)
    3.  
    4.     Private a = 0
    5.     Private b = 1
    6.  
    7.     Protected Overrides Function [Yield]() As Long
    8.         b = a + b
    9.         a = b - a
    10.         Return b
    11.     End Function
    12.  
    13. End Class

    vb.net Code:
    1. Private Function Problem2() As Integer
    2.         Return New Fibonacci() _
    3.             .Where(Function(x) x Mod 2 = 0) _
    4.             .TakeWhile(Function(p) p < 4000000) _
    5.             .Aggregate(Function(sum, x) sum + x)
    6.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    3. What is the largest prime factor of the number 600851475143?

    Primes generator class

    vb.net Code:
    1. Public Class Primes
    2.     Inherits Yielder(Of Long)
    3.  
    4.     Dim primes As New List(Of Long)
    5.     Dim prime = 1
    6.  
    7.     Protected Overrides Function [Yield]() As Long
    8.         Do While prime <= Long.MaxValue
    9.             prime += 1
    10.             If prime = 2 Then
    11.                 primes.Add(prime)
    12.                 Return prime
    13.             End If
    14.             Dim IsPrime = Function(n) primes.TakeWhile(Function(p) p <= Math.Sqrt(n)).FirstOrDefault(Function(p) n Mod p = 0) = 0
    15.             If IsPrime(prime) Then
    16.                 primes.Add(prime)
    17.                 Return prime
    18.             End If
    19.         Loop
    20.         Return Nothing
    21.     End Function
    22. End Class

    vb.net Code:
    1. Private Function Problem3() As Integer
    2.         Const num = 600851475143
    3.         Return New Primes() _
    4.             .TakeWhile(Function(i) i < Math.Sqrt(num)) _
    5.             .Where(Function(i) num Mod i = 0) _
    6.             .Max()
    7.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    4. Find the largest palindrome made from the product of two 3-digit numbers.

    vb.net Code:
    1. Private Function Problem4() As Integer
    2.         Return (From x In Enumerable.Range(100, 899)
    3.                From y In Enumerable.Range(100, 899)
    4.                Let product = x * y
    5.                Where IsPalindrome(product)
    6.                Order By product Descending
    7.                Select product).First()
    8.     End Function

    vb.net Code:
    1. Public Function IsPalindrome(number As Integer) As Boolean
    2.         Dim digitString = number.ToString()
    3.         Return digitString.SequenceEqual(digitString.Reverse)
    4.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    5. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

    vb.net Code:
    1. Private Function Problem5() As Integer
    2.         Return LowestCommonMultiple(Enumerable.Range(1, 20).ToArray)
    3.     End Function
    4.  
    5.     Private Function GreatestCommonDivisor(x As Integer, y As Integer) As Integer
    6.         While y <> 0
    7.             Dim temp = y
    8.             y = x Mod y
    9.             x = temp
    10.         End While
    11.         Return x
    12.     End Function
    13.  
    14.     Private Function LowestCommonMultiple(x As Integer, y As Integer) As Integer
    15.         Return x / GreatestCommonDivisor(x, y) * y
    16.     End Function
    17.  
    18.     Private Function LowestCommonMultiple(divisors As Integer()) As Integer
    19.         Return divisors.Aggregate(1, Function(lcm, n) LowestCommonMultiple(lcm, n))
    20.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    6. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

    vb.net Code:
    1. Private Function Problem6() As Integer
    2.         Dim numbers = Enumerable.Range(1, 100)
    3.         Return Math.Pow(numbers.Sum(), 2) - numbers.Aggregate(0, Function(sum, n) sum + Math.Pow(n, 2))
    4.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    7. What is the 10001st prime number?

    Score! My Prime class can be reused!

    vb.net Code:
    1. Private Function Problem7() As Integer
    2.         Return New Primes().Take(10001).Last()
    3.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  8. #8
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    8. Find the greatest product of five consecutive digits in the 1000-digit number.

    vb.net Code:
    1. Private Function Problem8() As Integer
    2.         Dim chars As Char() = "73167176531330624919225119674426574742355349194934" & _
    3.                               "96983520312774506326239578318016984801869478851843" & _
    4.                               "85861560789112949495459501737958331952853208805511" & _
    5.                               "12540698747158523863050715693290963295227443043557" & _
    6.                               "66896648950445244523161731856403098711121722383113" & _
    7.                               "62229893423380308135336276614282806444486645238749" & _
    8.                               "30358907296290491560440772390713810515859307960866" & _
    9.                               "70172427121883998797908792274921901699720888093776" & _
    10.                               "65727333001053367881220235421809751254540594752243" & _
    11.                               "52584907711670556013604839586446706324415722155397" & _
    12.                               "53697817977846174064955149290862569321978468622482" & _
    13.                               "83972241375657056057490261407972968652414535100474" & _
    14.                               "82166370484403199890008895243450658541227588666881" & _
    15.                               "16427171479924442928230863465674813919123162824586" & _
    16.                               "17866458359124566529476545682848912883142607690042" & _
    17.                               "24219022671055626321111109370544217506941658960408" & _
    18.                               "07198403850962455444362981230987879927244284909188" & _
    19.                               "84580156166097919133875499200524063689912560717606" & _
    20.                               "05886116467109405077541002256983155200055935729725" & _
    21.                               "71636269561882670428252483600823257530420752963450"
    22.  
    23.         Dim digits = Array.ConvertAll(chars, Function(c) CInt(c.ToString()))
    24.  
    25.         Dim maxProduct = 0
    26.         'Dim theDigits As String = String.Empty
    27.  
    28.         For i = 0 To digits.Length - 5
    29.             Dim product = digits(i)
    30.             For j = i + 1 To i + 4
    31.                 product *= digits(j)
    32.             Next
    33.  
    34.             If product > maxProduct Then
    35.                 maxProduct = product
    36.                 'theDigits = String.Join("", chars(i), chars(i + 1), chars(i + 2), chars(i + 3), chars(i + 4))
    37.             End If
    38.         Next
    39.  
    40.         Return maxProduct
    41.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    9. There exists exactly one Pythagorean triplet for which, a + b + c = 1000. Find the product abc.

    vb.net Code:
    1. Private Function Problem9()
    2.         Return (From a In Enumerable.Range(1, 500)
    3.                From b In Enumerable.Range(1, 500)
    4.                Let c = 1000 - a - b
    5.                Where Math.Pow(a, 2) + Math.Pow(b, 2) = Math.Pow(c, 2)
    6.                Select Product = a * b * c).First()
    7.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  10. #10
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,195

    Re: VB.NET Project Euler

    10. Find the sum of all the primes below two million.

    vb.net Code:
    1. Private Function Problem10() As Long
    2.         Return New Primes() _
    3.             .TakeWhile(Function(x) x < 2000000) _
    4.             .Aggregate(Function(sum, x) sum + x)
    5.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  11. #11
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 03
    Location
    Greeneville, TN
    Posts
    274

    Re: VB.NET Project Euler

    Quote Originally Posted by MattP View Post
    Thought I'd take a stab at the Project Euler questions. If anyone has any suggestions on how to improve these I'm all ears.

    1. Add all the natural numbers below one thousand that are multiples of 3 or 5.

    vb.net Code:
    1. Private Function Problem1() As Integer
    2.         Return Enumerable.Range(1, 999) _
    3.             .Where(Function(x) {3, 5}.Any(Function(y) x Mod y = 0)) _
    4.             .Sum()
    5.     End Function

    Solve for 3n = 1000 to get the highest possible n.
    Solve for 5m = 1000 to get the highest possible m.
    solve for 15r = 1000 to get the highest possible r.

    Where n, m, and r are integers that are always floored. (IE: never round up)

    Do the following equation: ans = 3*(n(n+1))/2 + 5 * (m(m+1)/2) - 15 *(r(r+1)/2)


    Ans should contain the sum of every natural number that is multiples of 3 or 5.
    Last edited by Maven; Oct 24th, 2012 at 06:35 AM.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •