The pair of integer numbers `(m, n)`, such that `10 > m > n > 0`,  (below 10), that its sum, `(m + n)`, and rest, `(m - n)`, are perfect squares, is (5, 4).

Let's see what we have explained with numbers.
```
5 + 4 = 9 = 3²
5 - 4 = 1 = 1²
(10 > 5 > 4 > 0)
```

The pair of numbers `(m, n)`, closest to and below 50, having the property described above is `(45, 36)`.
```
45 + 36 = 81 = 9²
45 - 36 = 9 = 3²
(50 > 45 > 36 > 0)
```

With the function `closest_pair_tonum()`, that receives a number `upper_limit`, as an upper limit, we should be able to obtain the closest pair to `upper_limit`, that fulfills the property described above.

The function should return the largest pair of numbers `(m, n)`, satisfying `upper_limit > m > n > 0`. Note that we say pair A `(a0, a1)` is larger than pair B `(b0, b1)` when `a0 > b0` or `a0 == b0 and
a1 > b1`.

Let's see some cases:

```python
closest_pair_tonum(10) == (5, 4) # (m = 5, n = 4)

closest_pair_tonum(30) == (29, 20)

closest_pair_tonum(50) == (45, 36)
```

Happy coding and enjoy it!!
(We will be having a second part of a similar exercise (in some days) that will need a faster algorithm for values of `upper_limit` < 1000)

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code: def closest_pair_tonum(upper_lim):
	

Solutions:
def closest_pair_tonum(uLim):
	return next(((a, b) for a in reversed(range(1, uLim)) for b in reversed(range(1, a)) if not (a + b) ** 0.5 % 1 and (not (a - b) ** 0.5 % 1)))


from itertools import combinations

def closest_pair_tonum(upper_lim):
	return next(((x, y) for (x, y) in combinations(range(upper_lim - 1, 0, -1), 2) if ((x + y) ** 0.5).is_integer() and ((x - y) ** 0.5).is_integer()))


from math import sqrt

def is_square(n):
	return round(sqrt(n)) ** 2 == n

def closest_pair_tonum(lim):
	for i in range(lim - 1, 0, -1):
		for j in range(i - 1, 0, -1):
			if is_square(i + j) and is_square(i - j):
				return (i, j)


def closest_pair_tonum(lim):
	return next(((m, n) for m in range(lim - 1, 0, -1) for n in range(m - 1, 0, -1) if not ((m + n) ** 0.5 % 1 or (m - n) ** 0.5 % 1)))


from math import sqrt

def is_sqrt(x):
	y = sqrt(x)
	return y == round(y)

def closest_pair_tonum(x):
	for i in range(x - 1, 1, -1):
		for j in range(i - 1, 0, -1):
			if is_sqrt(i + j) and is_sqrt(i - j):
				return (i, j)


import itertools
squares = {i * i for i in range(1, 2000 + 1)}

def closest_pair_tonum(upper_lim):
	return next(((a, b) for (a, b) in itertools.combinations(range(upper_lim - 1, 0, -1), 2) if a + b in squares and a - b in squares))


def closest_pair_tonum(upper_lim):
	sq = lambda x: int(x ** 0.5) == x ** 0.5
	for i in range(upper_lim - 1, 1, -1):
		for j in range(i - 1, 1, -1):
			if sq(i + j) * sq(i - j):
				return (i, j)