Created
February 16, 2014 07:35
-
-
Save musoftware/9030661 to your computer and use it in GitHub Desktop.
Smallest multiple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. | |
' What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | |
Module eular_5 | |
Function LCM(ByVal n1 As Long, ByVal n2 As Long) As Long | |
Dim tmp As Long, product As Long | |
product = n1 * n2 | |
Do | |
If n1 < n2 Then | |
tmp = n1 | |
n1 = n2 | |
n2 = tmp | |
End If | |
n1 = n1 Mod n2 | |
Loop While n1 | |
LCM = product \ n2 | |
End Function | |
Function evenly_divisible() As Integer | |
Dim target As Integer = 2 | |
For x As Integer = 20 To 2 Step -1 | |
target = LCM(target, x) | |
Next | |
Return target | |
End Function | |
Sub Main() | |
Console.WriteLine(evenly_divisible) | |
Console.ReadKey() | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment