Home > Enterprise >  FInd float numbers that have at least two multiples in given list
FInd float numbers that have at least two multiples in given list

Time:09-23

I need a function to find all float numbers that have at least two multiples in a given list.

Do you know if an already existing and efficient function exists in pandas, scipy or numpy for this purpose?

Example of expected behavior

Given the list [3.3, 3.4, 4.4, 5.1], I want a function that returns [.2, .3, 1.1, 1.7]

CodePudding user response:

You can do something like:

import itertools
from itertools import chain
from math import sqrt

l = [3.3, 3.4, 4.4, 5.1]

def divisors(n):
    # Find all divisors
    return set(chain.from_iterable((i,n//i) for i in range(1,int(sqrt(n)) 1) if n%i == 0))

# Multiply all numbers by 10, make integer, concatenate all divisors from all numbers
divisors_all = list(itertools.chain(*[list(divisors(int(x*10))) for x in l])) 

# Take divisors with more than 2 multiples, then multiply by 10 back
div, counts = np.unique(divisors_all, return_counts=True)
result = div[counts > 1]/10

Output:

array([0.1, 0.2, 0.3, 1.1, 1.7])

This makes the hypothesis that all number have one decimal maximum in the original set.

This keeps 1 as it divides everything, but can be removed easily.

CodePudding user response:

I think numpy.gcd() can be used to do what your question asks subject to the following clarifying constraints:

  • the input numbers will be examined to 1 decimal precision
  • inputs must be > 0
  • results must be > 1.0
import numpy as np
a = [3.3, 3.4, 4.4, 5.1]
b = [int(10*x) for x in a]
res = {np.gcd(x, y) for i, x in enumerate(b) for j, y in enumerate(b) if i != j}
res = [x/10 for x in res if x > 10]

Output:

[1.1, 1.7]

UPDATE:

To exactly match the results in the question after edit by OP (namely: [.2, .3, 1.1, 1.7]), we can do this:

import numpy as np
a = [3.3, 3.4, 4.4, 5.1]
b = [int(10*x) for x in a]
res = sorted({np.gcd(x, y) / 10 for i, x in enumerate(b) for j, y in enumerate(b) if i != j} - {1 / 10})

Output:

[0.2, 0.3, 1.1, 1.7]
  • Related