Skip to content

Instantly share code, notes, and snippets.

@LSStaff
Last active October 21, 2020 12:18
Show Gist options
  • Save LSStaff/b72faccd258dc6833e2f9ca66097c372 to your computer and use it in GitHub Desktop.
Save LSStaff/b72faccd258dc6833e2f9ca66097c372 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 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.
      2. Add factor to current_multiple.
  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