Microsoft Small Basic

Smallest number divisible

Modified: 2008/12/26 15:56 by 24.218.11.239 - Categorized as: Samples
Problem 5

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 number that is evenly divisible by all of the numbers from 1 to 20?


a = 1
TextWindow.WriteLine("Press any button to go!")
TextWindow.PauseWithoutMessage()
TextWindow.WriteLine("Going.")
For i = 1 To 6000000000 Step a
  j = 0
  l = 0
  For j = 20 To 10 Step -1 'we dont need to test 1 - 9, since they all have multiples higher than 10 and less than 20, so if 18 has a multiple, 9 will have be a multiple, etc.
    k = Math.Remainder(i,j)
    If k = 0 Then
      l = l + 1
      If l > 0 And l > b then
        b = l
        a = i
      EndIf
      If l = 10 Then
        Goto End
      EndIf
    Else
      Goto BreakJ  
    EndIf
  EndFor
  BreakJ:
EndFor

End:
If l = 10 Then
  TextWindow.WriteLine("Number " + i + " is divisible by 1 to 20 with no remainder.")
  TextWindow.WriteLine("Done")
Else
  TextWindow.WriteLine("No number under " + i + " was found to have 20 divisors with 0 remainder.")
EndIf

The solution I've written above is incredibly efficient, and for me (Pacolaco), it took less than 1/15th a second to arrive at the answer when the program is run.

ScrewTurn Wiki version 2.0.35. Some of the icons created by FamFamFam.