Options: Save as PDF | Save attached file | Toggle line numbers
| Type: |
sub |
| Added By: |
Rembo |
| Short Description: |
|
| Notes: |
The problem description is included with the code. |
| Added: |
Mar 13 2009 at 1:56 PM |
| Related URLs |
http://projecteuler.net
|
Run the routine, the answer is printed in the debugging screen.
Sub Euler_0003()
'The prime factors of 13195 are 5, 7, 13 and 29.
'
'What is the largest prime factor of the number 600851475143 ?
Dim i As Long, i2 As Long
Dim arrNums() As Long
Dim sNum As String
Dim cPrimes As Collection
-
'Store enough prime numbers in a collection
i2 = Int(Sqr(Len("600851475143")) + 1)
For i = 1 To i2
sNum = sNum & "9"
Next i
Set cPrimes = New Collection
For i = 1 To CLng(sNum) Step 2
cPrimes.Add i, CStr(i)
Next i
For i = cPrimes.Count To 1 Step -1
If cPrimes(i) / 3 = Int(cPrimes(i) / 3) Then
cPrimes.Remove i
End If
Next i
For i = cPrimes.Count To 1 Step -1
If cPrimes(i) / 5 = Int(cPrimes(i) / 5) Then
cPrimes.Remove i
End If
Next i
-
For i = cPrimes.Count To 1 Step -1
If cPrimes(i) / 7 = Int(cPrimes(i) / 7) Then
cPrimes.Remove i
End If
Next i
'Remove squares of primes
For i = cPrimes.Count To 1 Step -1
If Sqr(cPrimes(i)) = Int(Sqr(cPrimes(i))) Then
cPrimes.Remove i
End If
Next i
For i = cPrimes.Count To 1 Step -1
If 600851475143# / cPrimes(i) = Int(600851475143# / cPrimes(i)) Then
Debug.Print cPrimes(i)
Exit Sub
End If
Next i
End Sub