class AddingMultiples::CLI

Public Class Methods

new() click to toggle source

greeting and directions for the user

# File lib/adding_multiples/CLI.rb, line 7
def initialize
    puts
    puts "Hello! Thank you for taking the time to try this out."
    puts "This tool will add the multiples (up to 1,000) of any positive integers you choose." 
    puts "In the prompt below, please enter a positive integer to get started."
end

Public Instance Methods

calculate_least_common_multiple() click to toggle source

calculate least common multiple

# File lib/adding_multiples/CLI.rb, line 95
def calculate_least_common_multiple
    integer_0 = @integer[0].to_i
    integer_1 = @integer[1].to_i
    @least_common_multiple = integer_0 * integer_1

end
calculate_sum() click to toggle source

add multiples to sum

# File lib/adding_multiples/CLI.rb, line 76
def calculate_sum
    @sum += @multiple
end
call() click to toggle source

the order in which methods are called

# File lib/adding_multiples/CLI.rb, line 15
def call
    get_input
end
get_another_integer() click to toggle source

ask user for another integer

# File lib/adding_multiples/CLI.rb, line 41
def get_another_integer
    input = ""
    puts "\nPlease enter another positive integer:"
    @integer << gets.strip

    if @integer[@i].match(/\d+/)
        sum_integer_multiples
    else
        get_another_integer
    end  
end
get_input() click to toggle source

get input from the user

# File lib/adding_multiples/CLI.rb, line 20
def get_input
    @i = 0
    @integer = []

    get_positive_integer 
end
get_positive_integer() click to toggle source

get a positive integer from the user

# File lib/adding_multiples/CLI.rb, line 28
def get_positive_integer
    puts "\nPlease enter a positive integer:"
    @integer << gets.strip

    if @integer[@i].match(/\d+/)
        @i += 1 
        get_another_integer  
    else
        get_positive_integer
    end  
end
subtract_common_multiples() click to toggle source

subtract common multiples

# File lib/adding_multiples/CLI.rb, line 103
def subtract_common_multiples
    @sum -= @common_multiples_sum
end
sum_common_multiples() click to toggle source

sum common multiples

# File lib/adding_multiples/CLI.rb, line 81
def sum_common_multiples
    @range = [1, 1000]
    @least_common_multiple = 15
    @common_multiples_sum = 0
    @multiple = 0


    while @multiple < @range[1]
        @common_multiples_sum += @multiple
        @multiple += @least_common_multiple
    end
end
sum_integer_multiples() click to toggle source

loop through multiples and compare to range [1-1000], consider allowing user input for range

# File lib/adding_multiples/CLI.rb, line 54
def sum_integer_multiples
    @range = [1, 1000]
    @sum = 0

    @integer.each do |i|
        integer = i.to_i
        @multiple = 0
        while @multiple < @range[1]
            calculate_sum
            @multiple += integer
        end
    end
    
    # sum common multiples and subtract from total
    sum_common_multiples
    subtract_common_multiples

    puts "The sum of the multiples of the integers #{@integer} that are less than #{@range[1]} is #{@sum}"

end