How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PureBasic programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PureBasic programming language

Source code in the purebasic programming language

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

  

You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Rust programming language
You may also check:How to resolve the algorithm Super-Poulet numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the PIR programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the M4 programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the QB64 programming language