Problem 1If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
maxNumber = 1000
sum = 0
For i = 1 To maxNumber - 1 ' below maxNumber
If Math.Remainder(i, 3) = 0 Or Math.Remainder(i, 5) = 0 Then
sum = sum + i
EndIf
EndFor
TextWindow.WriteLine("Sum = " + sum)
Here's a solution that a young Gauss may have used - no looping or conditions...just division, multiplication, addition and subtraction...
TextWindow.Write("Add up all the multiples of 3 and 5 below which number? :")
input = TextWindow.Read()
n3 = Math.Floor((input-1)/3)
n5 = Math.Floor((input-1)/5)
sum3 = 3 * n3 * (n3 + 1) / 2
sum5 = 5 * n5 * (n5 + 1) / 2
n15 = math.Floor((input-1)/15)
sum15 = 15 * n15 * (n15 + 1) / 2
answer = sum3 + sum5 - sum15
TextWindow.Clear()
TextWindow.WriteLine("The answer is :" + answer)