|
-
Sep 28th, 2012, 05:22 PM
#1
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:
Private Function Problem1() As Integer
Return Enumerable.Range(1, 999) _
.Where(Function(x) {3, 5}.Any(Function(y) x Mod y = 0)) _
.Sum()
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.
-
Sep 28th, 2012, 05:25 PM
#2
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:
Public MustInherit Class Yielder(Of T)
Implements IEnumerable(Of T)
Implements IEnumerator(Of T)
Private _current As T
Public Sub New()
End Sub
Protected MustOverride Function [Yield]() As T
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return Me
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return Me
End Function
Public ReadOnly Property Current As T Implements System.Collections.Generic.IEnumerator(Of T).Current
Get
Return _current
End Get
End Property
Public ReadOnly Property Current1 As Object Implements System.Collections.IEnumerator.Current
Get
Return _current
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
_current = [Yield]()
Return True
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Throw New NotImplementedException()
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
My Fibonacci generator class
vb.net Code:
Public Class Fibonacci
Inherits Yielder(Of Long)
Private a = 0
Private b = 1
Protected Overrides Function [Yield]() As Long
b = a + b
a = b - a
Return b
End Function
End Class
vb.net Code:
Private Function Problem2() As Integer
Return New Fibonacci() _
.Where(Function(x) x Mod 2 = 0) _
.TakeWhile(Function(p) p < 4000000) _
.Aggregate(Function(sum, x) sum + x)
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.
-
Sep 28th, 2012, 05:26 PM
#3
Re: VB.NET Project Euler
3. What is the largest prime factor of the number 600851475143?
Primes generator class
vb.net Code:
Public Class Primes
Inherits Yielder(Of Long)
Dim primes As New List(Of Long)
Dim prime = 1
Protected Overrides Function [Yield]() As Long
Do While prime <= Long.MaxValue
prime += 1
If prime = 2 Then
primes.Add(prime)
Return prime
End If
Dim IsPrime = Function(n) primes.TakeWhile(Function(p) p <= Math.Sqrt(n)).FirstOrDefault(Function(p) n Mod p = 0) = 0
If IsPrime(prime) Then
primes.Add(prime)
Return prime
End If
Loop
Return Nothing
End Function
End Class
vb.net Code:
Private Function Problem3() As Integer
Const num = 600851475143
Return New Primes() _
.TakeWhile(Function(i) i < Math.Sqrt(num)) _
.Where(Function(i) num Mod i = 0) _
.Max()
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.
-
Sep 28th, 2012, 05:27 PM
#4
Re: VB.NET Project Euler
4. Find the largest palindrome made from the product of two 3-digit numbers.
vb.net Code:
Private Function Problem4() As Integer
Return (From x In Enumerable.Range(100, 899)
From y In Enumerable.Range(100, 899)
Let product = x * y
Where IsPalindrome(product)
Order By product Descending
Select product).First()
End Function
vb.net Code:
Public Function IsPalindrome(number As Integer) As Boolean
Dim digitString = number.ToString()
Return digitString.SequenceEqual(digitString.Reverse)
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.
-
Sep 28th, 2012, 05:28 PM
#5
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:
Private Function Problem5() As Integer
Return LowestCommonMultiple(Enumerable.Range(1, 20).ToArray)
End Function
Private Function GreatestCommonDivisor(x As Integer, y As Integer) As Integer
While y <> 0
Dim temp = y
y = x Mod y
x = temp
End While
Return x
End Function
Private Function LowestCommonMultiple(x As Integer, y As Integer) As Integer
Return x / GreatestCommonDivisor(x, y) * y
End Function
Private Function LowestCommonMultiple(divisors As Integer()) As Integer
Return divisors.Aggregate(1, Function(lcm, n) LowestCommonMultiple(lcm, n))
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.
-
Sep 28th, 2012, 05:28 PM
#6
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:
Private Function Problem6() As Integer
Dim numbers = Enumerable.Range(1, 100)
Return Math.Pow(numbers.Sum(), 2) - numbers.Aggregate(0, Function(sum, n) sum + Math.Pow(n, 2))
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.
-
Sep 28th, 2012, 05:29 PM
#7
Re: VB.NET Project Euler
7. What is the 10001st prime number?
Score! My Prime class can be reused!
vb.net Code:
Private Function Problem7() As Integer
Return New Primes().Take(10001).Last()
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.
-
Sep 28th, 2012, 05:30 PM
#8
Re: VB.NET Project Euler
8. Find the greatest product of five consecutive digits in the 1000-digit number.
vb.net Code:
Private Function Problem8() As Integer
Dim chars As Char() = "73167176531330624919225119674426574742355349194934" & _
"96983520312774506326239578318016984801869478851843" & _
"85861560789112949495459501737958331952853208805511" & _
"12540698747158523863050715693290963295227443043557" & _
"66896648950445244523161731856403098711121722383113" & _
"62229893423380308135336276614282806444486645238749" & _
"30358907296290491560440772390713810515859307960866" & _
"70172427121883998797908792274921901699720888093776" & _
"65727333001053367881220235421809751254540594752243" & _
"52584907711670556013604839586446706324415722155397" & _
"53697817977846174064955149290862569321978468622482" & _
"83972241375657056057490261407972968652414535100474" & _
"82166370484403199890008895243450658541227588666881" & _
"16427171479924442928230863465674813919123162824586" & _
"17866458359124566529476545682848912883142607690042" & _
"24219022671055626321111109370544217506941658960408" & _
"07198403850962455444362981230987879927244284909188" & _
"84580156166097919133875499200524063689912560717606" & _
"05886116467109405077541002256983155200055935729725" & _
"71636269561882670428252483600823257530420752963450"
Dim digits = Array.ConvertAll(chars, Function(c) CInt(c.ToString()))
Dim maxProduct = 0
'Dim theDigits As String = String.Empty
For i = 0 To digits.Length - 5
Dim product = digits(i)
For j = i + 1 To i + 4
product *= digits(j)
Next
If product > maxProduct Then
maxProduct = product
'theDigits = String.Join("", chars(i), chars(i + 1), chars(i + 2), chars(i + 3), chars(i + 4))
End If
Next
Return maxProduct
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.
-
Sep 28th, 2012, 05:31 PM
#9
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:
Private Function Problem9()
Return (From a In Enumerable.Range(1, 500)
From b In Enumerable.Range(1, 500)
Let c = 1000 - a - b
Where Math.Pow(a, 2) + Math.Pow(b, 2) = Math.Pow(c, 2)
Select Product = a * b * c).First()
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.
-
Sep 28th, 2012, 05:31 PM
#10
Re: VB.NET Project Euler
10. Find the sum of all the primes below two million.
vb.net Code:
Private Function Problem10() As Long
Return New Primes() _
.TakeWhile(Function(x) x < 2000000) _
.Aggregate(Function(sum, x) sum + x)
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.
-
Oct 24th, 2012, 06:24 AM
#11
Hyperactive Member
Re: VB.NET Project Euler
 Originally Posted by MattP
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:
Private Function Problem1() As Integer
Return Enumerable.Range(1, 999) _
.Where(Function(x) {3, 5}.Any(Function(y) x Mod y = 0)) _
.Sum()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|