How to resolve the algorithm Sum multiples of 3 and 5 step by step in the BCPL 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 BCPL 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 BCPL programming language
Source code in the bcpl programming language
GET "libhdr"
LET sumdiv(n, d) = VALOF {
LET m = n/d
RESULTIS m*(m + 1)/2 * d
}
LET sum3or5(n) = sumdiv(n, 3) + sumdiv(n, 5) - sumdiv(n, 15)
LET start() = VALOF {
LET sum = 0
LET n = 1
FOR k = 1 TO 999 DO
IF k MOD 3 = 0 | k MOD 5 = 0 THEN sum +:= k
writef("The sum of the multiples of 3 and 5 < 1000 is %d *n", sum)
writef("Next, the awesome power of inclusion/exclusion...*n");
FOR i = 1 TO 10 {
writef("%11d %d *n", n, sum3or5(n - 1))
n *:= 10
}
RESULTIS 0
}
You may also check:How to resolve the algorithm 100 doors step by step in the C++ programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Picat programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the V programming language
You may also check:How to resolve the algorithm SOAP step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Soundex step by step in the Arturo programming language