How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Joy 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 Joy 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 Joy programming language

Source code in the joy programming language

DEFINE divisor == rem 0 = ;
       mul3or5 == [3 divisor] [5 divisor] cleave or ;
       when == swap [] ifte .

"The sum of the multiples of 3 or 5 below 1000 is " putchars

0 999 [0 =] [pop]
[
   [dup rollup + swap] [mul3or5] when
   pred
] tailrec .

  

You may also check:How to resolve the algorithm Numerical integration step by step in the Julia programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Scheme programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Raku programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Groovy programming language