0

I'm trying to find if a number is ordinary or not using inputs. A positive integer is ordinary if it has at least two pairs of positive integer factors such that the difference of one pair equals the sum of the other pair.

For example, 6 is ordinary since 6×1=6,2×3=6,6−1=2+3; and 24 is also ordinary since 12−2=6+4. I'm stuck on how to make a code where it "grabs" a specific match of the factors of a number.

Thanks :)

1010011010
  • 45
  • 1
  • 1
  • 12

1 Answers1

1

First you need a method that will return all factor pairs for a number, this answer provides a decent enough solution https://stackoverflow.com/a/5505024/548562

def f(value):
    factors = []
    for i in range(1, int(value**0.5)+1):
        if value % i == 0:
            factors.append((i, value / i))
    return factors

Now we have all possible factor pairs we need to loop over every permutation to see if the sum of one pair is equal to the difference of another. itertools provides a permutations function that can be used for this

def is_special(x):
    factor_pairs = f(x)
    for p1, p2 in itertools.permutations(factor_pairs, 2):
        if p1[1] - p1[0] == p2[0] + p2[1]:
            return False
    return True
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50