If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Let's create a predicate that returns True
if a number is a multiple of 3 or 5 and False
otherwise. It is simple enough using the modulus operator.
[1 2 3 4 5 6 10 15] [3 % not] map
clear
[1 2 3 4 5 6 10 15] [5 % not] map
clear
Now we can run them both using fork
and then or
the results:
23 [3 % not] [5 % not] fork or
clear
[1 2 3 4 5 6 10 15] [[3 % not] [5 % not] fork or] map
clear
Given the predicate function (and a filter
function, to be defined later) a suitable program is:
1000 range [[3 % not] [5 % not] fork or] filter sum
This function generates a list of the integers from 0 to 999, filters that list by the predicate, and then sums the result.
Logically this is fine, but pragmatically we are doing more work than we should. We generate one thousand integers but actually use less than half of them. A better solution would be to generate just the multiples we want to sum, and to add them as we go rather than storing them and adding summing them at the end.
Consider the first few terms in the series:
3 5 6 9 10 12 15 18 20 21 ...
Subtract each number from the one after it (subtracting 0 from 3):
3 5 6 9 10 12 15 18 20 21 24 25 27 30 ...
0 3 5 6 9 10 12 15 18 20 21 24 25 27 ...
-------------------------------------------
3 2 1 3 1 2 3 3 2 1 3 1 2 3 ...
You get this lovely repeating palindromic sequence:
3 2 1 3 1 2 3
To make a counter that increments by factors of 3 and 5 you just add these differences to the counter one-by-one in a loop.
To make use of this sequence to increment a counter and sum terms as we go we need a function that will accept the sum, the counter, and the next term to add, and that adds the term to the counter and a copy of the counter to the running sum. This function will do that:
+ [+] dupdip
0 0 3 [+ [+] dupdip] trace
clear
0 0 [3 2 1 3 1 2 3] [+ [+] dupdip] step
So one step
through all seven terms brings the counter to 15 and the total to 60.
clear 0 0
[3 2 1 3 1 2 3] [+ [+] dupdip] step
[3 2 1 3 1 2 3] [+ [+] dupdip] step
[3 2 1 3 1 2 3] [+ [+] dupdip] step
[3 2 1 3 1 2 3] [+ [+] dupdip] step
Going through one sequence of the palindrome counts off 15 of the 1000. So how many "flights" in total do we need?
clear
1000 15 /
So sixty-six times and a few left over. How many?
15 *
[1000 swap -] trace
We only want the terms less than 1000.
clear 999 990 -
[3 2 1 3] sum
=
That means we want to run the full list of numbers sixty-six times to get to 990 and then the first four numbers 3 2 1 3 to get to 999.
clear 0 0
66 [[3 2 1 3 1 2 3] [+ [+] dupdip] step] times
[3 2 1 3] [+ [+] dupdip] step
pop
Ta-da!
This form uses no extra storage and produces no unused summands. It's good but there's one more trick we can apply. The list of seven terms takes up at least seven bytes. But notice that all of the terms are less than four, and so each can fit in just two bits. We could store all seven terms in just fourteen bits and use masking and shifts to pick out each term as we go. This will use less space and save time loading whole integer terms from the list.
3 2 1 3 1 2 3
0b 11 10 01 11 01 10 11 == 14811
clear 14811
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
[3 &] [2 >>] cleave
We can run it in a loop
...
clear 14811
? [[3 &] [2 >>] cleave ?] loop pop
?
clear 0 0 14811
[3 &] [2 >>] cleave
[+ [+] dupdip] dip
[3 &] [2 >>] cleave
[+ [+] dupdip] dip
[3 &] [2 >>] cleave [+ [+] dupdip] dip
[3 &] [2 >>] cleave [+ [+] dupdip] dip
[3 &] [2 >>] cleave [+ [+] dupdip] dip
[3 &] [2 >>] cleave [+ [+] dupdip] dip
[3 &] [2 >>] cleave [+ [+] dupdip] dip
clear 0 0
14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop
14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop
clear 0 0
66 [14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop] times
14811 4 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop
pop
clear
[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]
x
x x x x x x pop
clear
7 66 * 4 +
clear
clear
[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]
466 [x] times pop
enstacken sum
clear 0 0
[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]
466 [x [+ [+] dupdip] dip] times popop