4 "cell_type": "markdown",
8 "# [Project Euler, first problem](https://projecteuler.net/problem=1)\n",
10 "## Multiples of 3 and 5\n",
12 "> 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.\n",
14 "> Find the sum of all the multiples of 3 or 5 below 1000."
18 "cell_type": "markdown",
22 "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."
33 "output_type": "stream",
35 "[false false true false false true false true]"
40 "[1 2 3 4 5 6 10 15] [3 % not] map"
51 "output_type": "stream",
67 "output_type": "stream",
69 "[false false false false true false true true]"
74 "[1 2 3 4 5 6 10 15] [5 % not] map"
85 "output_type": "stream",
94 "cell_type": "markdown",
98 "Now we can run them both using `fork` and then `or` the results:"
103 "execution_count": 5,
109 "output_type": "stream",
116 "23 [3 % not] [5 % not] fork or"
121 "execution_count": 6,
127 "output_type": "stream",
137 "execution_count": 7,
143 "output_type": "stream",
145 "[false false true false true true true true]"
150 "[1 2 3 4 5 6 10 15] [[3 % not] [5 % not] fork or] map"
155 "execution_count": 8,
161 "output_type": "stream",
170 "cell_type": "markdown",
174 "Given the predicate function (and a `filter` function, to be defined later) a suitable program is:\n",
176 " 1000 range [[3 % not] [5 % not] fork or] filter sum"
180 "cell_type": "markdown",
184 "This function generates a list of the integers from 0 to 999, filters that list by the predicate, and then sums the result.\n",
186 "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."
190 "cell_type": "markdown",
194 "Consider the first few terms in the series:\n",
196 " 3 5 6 9 10 12 15 18 20 21 ...\n",
198 "Subtract each number from the one after it (subtracting 0 from 3):\n",
200 " 3 5 6 9 10 12 15 18 20 21 24 25 27 30 ...\n",
201 " 0 3 5 6 9 10 12 15 18 20 21 24 25 27 ...\n",
202 " -------------------------------------------\n",
203 " 3 2 1 3 1 2 3 3 2 1 3 1 2 3 ...\n",
205 "You get this lovely repeating palindromic sequence:\n",
209 "To make a counter that increments by factors of 3 and 5 you just add\n",
210 "these differences to the counter one-by-one in a loop."
214 "cell_type": "markdown",
218 "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:\n",
225 "execution_count": 9,
231 "output_type": "stream",
233 " 0 0 3 • + [+] dupdip\n",
234 " 0 3 • [+] dupdip\n",
235 "0 3 [+] • dupdip\n",
245 "0 0 3 [+ [+] dupdip] trace"
250 "execution_count": 10,
256 "output_type": "stream",
266 "execution_count": 11,
272 "output_type": "stream",
279 "0 0 [3 2 1 3 1 2 3] [+ [+] dupdip] step"
283 "cell_type": "markdown",
287 "So one `step` through all seven terms brings the counter to 15 and the total to 60."
292 "execution_count": 12,
298 "output_type": "stream",
310 "execution_count": 13,
316 "output_type": "stream",
323 "[3 2 1 3 1 2 3] [+ [+] dupdip] step"
328 "execution_count": 14,
334 "output_type": "stream",
341 "[3 2 1 3 1 2 3] [+ [+] dupdip] step"
346 "execution_count": 15,
352 "output_type": "stream",
359 "[3 2 1 3 1 2 3] [+ [+] dupdip] step"
364 "execution_count": 16,
370 "output_type": "stream",
377 "[3 2 1 3 1 2 3] [+ [+] dupdip] step"
381 "cell_type": "markdown",
385 "Going through one sequence of the palindrome counts off 15 of the 1000. So how many \"flights\" in total do we need?"
390 "execution_count": 17,
396 "output_type": "stream",
406 "execution_count": 18,
412 "output_type": "stream",
423 "cell_type": "markdown",
427 "So sixty-six times and a few left over. How many?"
432 "execution_count": 19,
438 "output_type": "stream",
450 "execution_count": 20,
456 "output_type": "stream",
458 " 990 • 1000 swap -\n",
459 "990 1000 • swap -\n",
468 "[1000 swap -] trace"
472 "cell_type": "markdown",
476 "We only want the terms *less than* 1000."
481 "execution_count": 21,
487 "output_type": "stream",
499 "execution_count": 22,
505 "output_type": "stream",
517 "execution_count": 23,
523 "output_type": "stream",
534 "cell_type": "markdown",
538 "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."
543 "execution_count": 24,
549 "output_type": "stream",
561 "execution_count": 25,
567 "output_type": "stream",
574 "66 [[3 2 1 3 1 2 3] [+ [+] dupdip] step] times"
579 "execution_count": 26,
585 "output_type": "stream",
592 "[3 2 1 3] [+ [+] dupdip] step"
597 "execution_count": 27,
603 "output_type": "stream",
614 "cell_type": "markdown",
622 "cell_type": "markdown",
628 "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.\n",
631 " 0b 11 10 01 11 01 10 11 == 14811"
636 "execution_count": 28,
642 "output_type": "stream",
654 "execution_count": 29,
660 "output_type": "stream",
667 "[3 &] [2 >>] cleave"
672 "execution_count": 30,
678 "output_type": "stream",
685 "[3 &] [2 >>] cleave"
690 "execution_count": 31,
696 "output_type": "stream",
703 "[3 &] [2 >>] cleave"
708 "execution_count": 32,
714 "output_type": "stream",
721 "[3 &] [2 >>] cleave"
726 "execution_count": 33,
732 "output_type": "stream",
739 "[3 &] [2 >>] cleave"
744 "execution_count": 34,
750 "output_type": "stream",
757 "[3 &] [2 >>] cleave"
762 "execution_count": 35,
768 "output_type": "stream",
775 "[3 &] [2 >>] cleave"
780 "execution_count": 36,
786 "output_type": "stream",
793 "[3 &] [2 >>] cleave"
798 "execution_count": 37,
804 "output_type": "stream",
806 "3 2 1 3 1 2 3 0 0 0"
811 "[3 &] [2 >>] cleave"
815 "cell_type": "markdown",
819 "We can run it in a `loop`..."
824 "execution_count": 38,
830 "output_type": "stream",
842 "execution_count": 39,
848 "output_type": "stream",
855 "? [[3 &] [2 >>] cleave ?] loop pop"
859 "cell_type": "markdown",
868 "execution_count": 40,
874 "output_type": "stream",
886 "execution_count": 41,
892 "output_type": "stream",
899 "[3 &] [2 >>] cleave"
904 "execution_count": 42,
910 "output_type": "stream",
922 "execution_count": 43,
928 "output_type": "stream",
935 "[3 &] [2 >>] cleave"
940 "execution_count": 44,
946 "output_type": "stream",
958 "execution_count": 45,
964 "output_type": "stream",
971 "[3 &] [2 >>] cleave [+ [+] dupdip] dip"
976 "execution_count": 46,
982 "output_type": "stream",
989 "[3 &] [2 >>] cleave [+ [+] dupdip] dip"
994 "execution_count": 47,
1000 "output_type": "stream",
1007 "[3 &] [2 >>] cleave [+ [+] dupdip] dip"
1011 "cell_type": "code",
1012 "execution_count": 48,
1018 "output_type": "stream",
1025 "[3 &] [2 >>] cleave [+ [+] dupdip] dip"
1029 "cell_type": "code",
1030 "execution_count": 49,
1036 "output_type": "stream",
1043 "[3 &] [2 >>] cleave [+ [+] dupdip] dip"
1047 "cell_type": "code",
1048 "execution_count": null,
1055 "cell_type": "code",
1056 "execution_count": 50,
1062 "output_type": "stream",
1073 "cell_type": "code",
1074 "execution_count": 51,
1080 "output_type": "stream",
1087 "14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop"
1091 "cell_type": "code",
1092 "execution_count": 52,
1098 "output_type": "stream",
1105 "14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop"
1109 "cell_type": "code",
1110 "execution_count": null,
1117 "cell_type": "code",
1118 "execution_count": 53,
1124 "output_type": "stream",
1135 "cell_type": "code",
1136 "execution_count": 54,
1142 "output_type": "stream",
1149 "66 [14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop] times"
1153 "cell_type": "code",
1154 "execution_count": 55,
1160 "output_type": "stream",
1167 "14811 4 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop"
1171 "cell_type": "code",
1172 "execution_count": 56,
1178 "output_type": "stream",
1189 "cell_type": "code",
1190 "execution_count": null,
1197 "cell_type": "code",
1198 "execution_count": 57,
1204 "output_type": "stream",
1206 "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1212 "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1216 "cell_type": "code",
1217 "execution_count": 58,
1223 "output_type": "stream",
1225 "3 [3702 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1234 "cell_type": "code",
1235 "execution_count": 59,
1241 "output_type": "stream",
1252 "cell_type": "code",
1253 "execution_count": 60,
1259 "output_type": "stream",
1271 "cell_type": "code",
1272 "execution_count": 61,
1278 "output_type": "stream",
1287 "cell_type": "code",
1288 "execution_count": null,
1295 "cell_type": "code",
1296 "execution_count": 62,
1302 "output_type": "stream",
1304 "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1310 "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1314 "cell_type": "code",
1315 "execution_count": 63,
1321 "output_type": "stream",
1323 "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3"
1332 "cell_type": "code",
1333 "execution_count": 64,
1339 "output_type": "stream",
1350 "cell_type": "code",
1351 "execution_count": null,
1358 "cell_type": "code",
1359 "execution_count": 65,
1365 "output_type": "stream",
1367 "0 0 [0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1373 "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]"
1377 "cell_type": "code",
1378 "execution_count": 66,
1384 "output_type": "stream",
1391 "466 [x [+ [+] dupdip] dip] times popop"
1395 "cell_type": "code",
1396 "execution_count": null,
1405 "display_name": "Joypy",
1410 "file_extension": ".joy",
1411 "mimetype": "text/plain",