Task

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n.

Show output for n = 1000.

This is is the same as Project Euler problem 1.

Extra credit: do this efficiently for n = 1e20 or higher.




 
EnableExplicit
 
Procedure.q SumMultiples(Limit.q)
  If Limit < 0 : Limit = -Limit : EndIf; convert negative numbers to positive
  Protected.q i, sum = 0
  For i = 3 To Limit - 1
    If i % 3 = 0 Or i % 5 = 0
      sum + i
    EndIf
  Next  
  ProcedureReturn sum
EndProcedure
 
If OpenConsole()
  PrintN("Sum of numbers below 1000 which are multiples of 3 or 5 is : " + SumMultiples(1000))
  PrintN("")
  PrintN("Press any key to close the console")
  Repeat: Delay(10) : Until Inkey() <> ""
  CloseConsole()
EndIf
 