Microsoft Small Basic
RSS

Navigation





Quick Search
»
Advanced Search »

PoweredBy

Sum of natural numbers

RSS
Modified on 2011/11/14 09:03 by 99.255.114.181 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)
Vietnam Flights Vietnam Visa play and win Call Center Services, Pacquiao vs Marquez Tickets , Mayweather vs Ortiz phentermine, Essays, Pacquiao Marquez Tickets , Essay Writing, Mayweather vs Ortiz Online Live Streaming , Pacquiao vs Marquez 3 Tickets best weight loss

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