Skip to content

Instantly share code, notes, and snippets.

@preyes323
Last active January 15, 2018 14:47
Show Gist options
  • Save preyes323/dff439bf3edca8d3f3760bec617ed110 to your computer and use it in GitHub Desktop.
Save preyes323/dff439bf3edca8d3f3760bec617ed110 to your computer and use it in GitHub Desktop.

First Mental Model

Determine a list of all multiples of a set of factors up to a target value, then filter the list of multiples to the unique values. Finally, compute and return the sum of the unique multiples.

  1. Create an empty array called multiples that will contain the list of multiples.
  2. Check whether the list of factors is empty. If there are no factors, set the list to [3, 5]
  3. For every factor in the factors list:
    1. Set the current_multiple to factor to keep track of the multiples of factor.
    2. While current_multiple < target
      1. Append the current_multiple to multiples.
      2. Add factor to current_multiple.
  4. Filter duplicate numbers from multiples.
  5. Compute and return the sum of the numbers in multiples.

Second Mental Model

Incrementally build a list of numbers that are multiples of a set of one or more factors. Add a multiple to the list only if it is not yet on the list. Finally, compute and return the sum of the numbers on the list.

  1. Create an empty array called multiples that will contain the list of multiples
  2. Check whether the list of factors is empty. If there are no factors, set the list to [3, 5]
  3. For every factor in the factors list:
    1. Set the current_multiple to factor to keep track of the multiples of factor.
    2. While current_multiple < target
      1. Is the current_multiple in multiples already?
        1. Yes - do nothing
        2. No - Append the current_multiple to multiples.
  4. Compute and return the sum of the numbers in multiples.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment