How to resolve the algorithm Sum multiples of 3 and 5 step by step in the 8th 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 8th 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 8th programming language
Source code in the 8th programming language
needs combinators/bi
: mul3or5? ( 3 mod 0 = ) ( 5 mod 0 = ) bi or ;
"The sum of the multiples of 3 or 5 below 1000 is " .
0 ( mul3or5? if I n:+ then ) 1 999 loop . cr
with: n
: >triangular SED: n -- n
dup 1+ * 2 / ;
: sumdiv SED: n n -- n
dup >r /mod nip >triangular r> * ;
: sumdiv_3,5 SED: n -- n
( swap sumdiv ) curry [3, 5, 15] swap a:map a:open neg + + ;
;with
"For 10^20 - 1, the sum is " . 10 20 ^ 1- sumdiv_3,5 . cr
bye
You may also check:How to resolve the algorithm Loops/Nested step by step in the BASIC programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Tau number step by step in the Julia programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the PL/I programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Ada programming language