Microsoft Small Basic

Sum of natural numbers

Modified: 2012/01/09 17:20 by 118.71.68.60 - Categorized as: outsourcing
Problem 1
world literature literature buy essay buy essays lead generation
If 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)
visa, play and win Call Center Services, Pacquiao vs Marquez Tickets , Mayweather vs Ortiz phentermine, Essays, vietnam visa, vietnam tours, Pacquiao Marquez Tickets , Essay Writing, download free vector, vector, Mayweather vs Ortiz Online Live Streaming , Pacquiao vs Marquez 3 Tickets best weight loss om onny jasa seo murah kanvas lukis pasang iklan baris hot news toko bunga all round citra indah busana muslim trendy teak furniture jasa seo jasa seo murah teak furniture teak colonial furniture busana muslim trendy leader wongfinancial indonesia citra indah

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