Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. # Some tips for solving problems
  2.  
  3. Let's solve the first Euler problem:
  4.  
  5. > 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.
  6. > Find the sum of all the multiples of 3 or 5 below 1000.
  7.  
  8. ## Understand the problem
  9.  
  10. Make sure you fully understand what you're supposed to be doing. Check anything you're not sure about.
  11.  
  12. **Q.** What's a "natural number?"
  13.  
  14. **A.** the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well.
  15.  
  16. **Q.** What's a mutliple?
  17.  
  18. **A.** a number that may be divided by another a certain number of times without a remainder.
  19.  
  20. ## Try and solve the problem on paper (or a subset of the problem)
  21. Let's do it for the natural numbers below 10:
  22.  
  23. - Take the number 1: can 1 be divided by 3 or 5 with no remainder? No, so forget about it.
  24. - Take the number 2: can 2 be divided by 3 or 5 with no remainder? No, so forget about it.
  25. - Take the number 3: can 3 be divided by 3 or 5 with no remainder? **Yes**, so write it down.
  26. ...
  27. - Take the number 3: can 9 be divided by 3 or 5 with no remainder? **Yes**, so write it down.
  28.  
  29. By now we should have written down **3, 5, 6 & 9**
  30.  
  31. Finally, add them all up to get the sum: **23**
  32.  
  33. ## Try and write out instructions (in human language) that explain how to solve the problem
  34. Imagine you're explaining it to someone who doesn't know anything about coding, they don't know what a condition or a loop is. Keep it simple. This is my attempt:
  35.  
  36. - For every number below 10:
  37. - Take that number and divide it by 3, does the answer have a remainder? If it does not, then write it down.
  38. - Take the same number and divide it by 5, does the answer havea remainder? If it does not, then write it down.
  39.  
  40. Once you have finished this:
  41. - Add up all of the numbers you have written down.
  42.  
  43. Let's do it again with a little more detail:
  44.  
  45. - For the number 1:
  46. -- Take that number and divide it by 3, does the answer have a remainder? If it does not, then write it down.
  47. -- Take the same number and divide it by 5, does the answer havea remainder? If it does not, then write it down.
  48. - Do the same for the number 2
  49. - Do the same for the number 3,
  50. - Repeat until the number 9
  51.  
  52. ## Look for familiar patterns
  53.  
  54. Is this problem similar to ones you've solved before? Can you use the same approach? Sometimes a keyword in the question can give away what you need to do.
  55.  
  56. Examples:
  57. - Finding the **sum** or **count** of an array of numbers usually means you'll need a for loop.
  58. - Reading input **until** the user enters a certain character usually means you'll need a while loop.
  59.  
  60. ## Now try and write the code
  61. Break down the task into smaller goals, and then do those one at a time. These can be whatever makes sense for you, but try to make very small incremental changes. Here are mine:
  62.  
  63. ### Goal 1: Output the natural numbers below 10
  64.  
  65. ```javascript
  66. for(var i = 1; i < 10; i++) {
  67. console.log(i);
  68. }
  69. ```
  70.  
  71. ### Goal 2: Output the natural numbers below 10 that are multiples of 3
  72.  
  73. ```javascript
  74. for(var i = 1; i < 10; i++) {
  75. if (i % 3 === 0) {
  76. console.log(i);
  77. }
  78. }
  79. ```
  80. ### Goal 3: Output the natural numbers below 10 that are multiples of 3 or 5
  81.  
  82. ```javascript
  83. for(var i = 1; i < 10; i++) {
  84. if (i % 3 === 0 || i % 5 === 0) {
  85. console.log(i);
  86. }
  87. }
  88. ```
  89.  
  90. ### Gosl 4: _Record_ the natural numbers below 10 that are multiples of 3 or 5
  91. ```javascript
  92. var multiples = [];
  93.  
  94. for(var i = 1; i < 10; i++) {
  95. if (i % 3 === 0 || i % 5 === 0) {
  96. multiples.push(i);
  97. }
  98. }
  99.  
  100. console.log(multiples);
  101. ```
  102.  
  103. ### Goal 5: Calculate the sum of the natural numbers below 10 that are multiples of 3 or 5
  104. ```javascript
  105. var multiples = [];
  106.  
  107. for(var i = 1; i < 10; i++) {
  108. if (i % 3 === 0 || i % 5 === 0) {
  109. multiples.push(i);
  110. }
  111. }
  112.  
  113. var sum = 0;
  114.  
  115. for(var i = 0; i < multiples.length; i++) {
  116. sum += multiples[i];
  117. }
  118.  
  119. console.log(sum);
  120. ```
  121. ### Goal 6 (The final answer): Calculate the sum of the natural numbers below 1000 that are multiples of 3 or 5
  122. ```javascript
  123. var multiples = [];
  124.  
  125. for(var i = 1; i < 1000; i++) {
  126. if (i % 3 === 0 || i % 5 === 0) {
  127. multiples.push(i);
  128. }
  129. }
  130.  
  131. var sum = 0;
  132.  
  133. for(var i = 0; i < multiples.length; i++) {
  134. sum += multiples[i];
  135. }
  136.  
  137. console.log(sum);
  138. ```
  139.  
  140. ## Optimize your solution (but ONLY if you need to)
  141.  
  142. The solution above produces the correct answer and, on my PC, it runs in 0.054s, that's definitely good enough, so I would stop here.
  143.  
  144. However, what if I need to find the sum of all the multiples of 3 or 5 below 1,000,000,000?
  145.  
  146. The following code crashes on my computer as it runs out of memory:
  147.  
  148. ```javascript
  149. var multiples = [];
  150.  
  151. for(var i = 1; i < 1000000000; i++) {
  152. if (i % 3 === 0 || i % 5 === 0) {
  153. multiples.push(i);
  154. }
  155. }
  156.  
  157. var sum = 0;
  158.  
  159. for(var i = 0; i < multiples.length; i++) {
  160. sum += multiples[i];
  161. }
  162.  
  163. console.log(sum);
  164. ```
  165.  
  166. So we would need to optimize it. Let's try and figure it out.
  167.  
  168. I'm pretty sure that the problem is the **multiples** array. It's probably getting pretty big and taking up a lot of space. What can we do about it?
  169.  
  170. One pattern that I recognize is we have one loop that creates the array, then another loop that reads from it. Often this means we can do the whole process in a single loop. Also I don't care about the contents of **multiples** I only care about the sum. So I should be able to get rid of multiples completely by keeping a running total:
  171.  
  172. ```javascript
  173. var sum = 0;
  174.  
  175. for(var i = 1; i < 1000000000; i++) {
  176. if (i % 3 === 0 || i % 5 === 0) {
  177. sum += i;
  178. }
  179. }
  180. console.log(sum);
  181. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement