Skip to content

Instantly share code, notes, and snippets.

@LSStaff
Created January 16, 2018 06:21
Show Gist options
  • Save LSStaff/ce0dc73f57e82b1936880fa1af57cd88 to your computer and use it in GitHub Desktop.
Save LSStaff/ce0dc73f57e82b1936880fa1af57cd88 to your computer and use it in GitHub Desktop.

Example 1

Inputs:

  • Target number: 20
  • Number to get multiples: [3, 5]

Output

  • 78

Algorithm

  1. Create an empty array called multiples that will contain the list of multiples

    multiples = []

  2. Check whether the list of factors is empty. If there are no factors, set the list to [3, 5]

    [3, 5] obtained from supplied factors.

  3. For every factor in the factors list: [3, 5]

    1. Set the current_multiple to factor to keep track of the multiples of factor.

      current_multiple = 3
      current_multiple = 5
    2. While current_multiple < target

      1. Append the current_multiple to multiples.

        multiples = [3]
        multiples = [3, 6]
        multiples = [3, 6, 9]
        ...
        multiples = [3, 6, 9, 12, 15, 18, 5, 10, 15]
      2. Add factor to current_multiple.

        current_multiple = 6
        current_multiple = 9
         ...
        current_multiple = 18
        current_multiple = 21
        current_multiple = 5
        current_multiple = 10
        current_multiple = 15
        current_multiple = 20
  4. Filter duplicate numbers from multiples.

    multiples = [3, 6, 9, 12, 15, 18, 5, 10]

  5. Compute and return the sum of the numbers in multiples.

    78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment