matharithmetic-expressionsgreatest-common-divisor

Is it possible to find a few common multiples of a list of numbers, without them having to be integers?


I don't even know if something like this is possible, but:

Let us say we have three numbers:

A = 6
B = 7.5
C = 24

I would like to find a few evenly spaced common multiples of these numbers between 0 and 2. So the requirement is: one_of_these_numbers / common_multiple = an_integer (or almost an integer with a particular tolerance)

For example, a good result would be [0.1 , 0.5 , 1 , 1.5]

I have no idea if this is possible, because one can not iterate through a range of floats, but is there a smart way to do it?

I am using python, but a solution could be represented in any language of your preference.

Thank you for your help!


Solution

  • While I was writing my question, I actually came up with an idea for the solution.

    To find common divisors using code, we have to work with integers.

    My solution is to multiply all numbers by a factor = 1, 10, 100, ... so that we can act as if they are integers, find their integer common divisors, and then redivide them by the factor to get a result.

    Better explained in code:

    a = 6
    b = 7.5
    c = 24
    
    # Find a few possible divisors between 0 and 2 so that all numbers are divisible 
    by div.
    
    # We define a function that finds all divisors in a range of numbers, supposing 
    all numbers are integers.
    
    def find_common_divisors(numbers, range_start, range_end):
        results = []
        for i in range(range_start + 1, range_end + 1):
            if all([e % i == 0 for e in numbers]):
                results.append(i)
        return results
    
    def main():
        nums = [a, b, c]
        range_start = 0
        range_end = 2
        factor = 1
        results = [1]
        while factor < 11:
            nums_i = [e * factor for e in nums]
            range_end_i = range_end * factor
    
            results += [e / factor for e in find_common_divisors(nums_i, range_start, range_end_i)]
    
            factor *= 10
    
        print(sorted(set(results)))
    
    if __name__ == '__main__':
        main()
    

    For these particular numbers, I get the output:

    [0.1, 0.3, 0.5, 1, 1.5]
    

    If we need more results, we can adjust while factor < 11: to a higher number than 11 like 101.

    I am curious to see if I made any mistake in my code. Happy to hear some feedback.

    Thank you!