The number 45 is the first integer in having this interesting property:
the sum of the number with its reversed is divisible by the difference between them(absolute Value).
```
45 + 54 = 99 
abs(45 - 54) = 9
99 is divisible by 9.
```
The first terms of this special sequence are :
```
n               a(n)
1               45
2               54
3               495
4               594
```
Make the function ```sum_dif_rev()```(`sumDivRef` in JavaScript and CoffeeScript), that receives ```n```, the ordinal number of the term and may give us, the value of the term of the sequence.
```python
sum_dif_rev(n) -----> a(n)
```
Let's see some cases:
```python
sum_dif_rev(1) -----> 45
sum_dif_rev(3) -----> 495
```
"Important: Do not include numbers which, when reversed, have a leading zero, e.g.: 1890 reversed is 0981, so 1890 should not be included."

Your code will be tested up to the first 65 terms, so you should think to optimize it as much as you can.

(Hint: I memoize, you memoize, he memoizes, ......they (of course) memoize)

Happy coding!!

Source: codewars | Difficulty: EASY
Tags: Dynamic programming

Fundamentals

Mathematics

Starter code: def sum_dif_rev(n):
	

Solutions:
MEMO = []

def sum_dif_rev(n):
	i = MEMO[-1] if MEMO else 0
	while len(MEMO) < n:
		i += 1
		r = int(str(i)[::-1])
		if i % 10 and r != i and ((i + r) % abs(r - i) == 0):
			MEMO.append(i)
	return MEMO[n - 1]


from itertools import count

def gen():
	for x in count(0, 9):
		y = int(str(x)[::-1])
		if x != y and x % 10 and ((x + y) % abs(x - y) == 0):
			yield x
(numbers, result) = (gen(), [])

def sum_dif_rev(n):
	while len(result) < n:
		result.append(next(numbers))
	return result[n - 1]


def sum_dif_rev(num):
	n = 36
	while num:
		n += 9
		if n % 10 == 0:
			continue
		r = int(str(n)[::-1])
		if n != r and (n + r) % abs(n - r) == 0:
			num -= 1
	return n


def memoize_sum_dif(f):
	memory = {}

	def inner(n):
		if n not in memory:
			memory[n] = f(n)
		return memory[n]
	return inner

@memoize_sum_dif
def sum_dif_rev(n):
	if n == 1:
		return 45
	num = sum_dif_rev(n - 1)
	while True:
		num = num + 1
		if str(num)[::-1][0] == '0' or abs(num - int(str(num)[::-1])) == 0:
			continue
		if (num + int(str(num)[::-1])) % abs(num - int(str(num)[::-1])) == 0:
			break
	return num


def sum_dif():
	yield 45
	yield 54
	yield 495
	yield 594
	n1 = 595
	while True:
		n_rev = str(n1)[::-1]
		if n_rev[0] == '0':
			n1 += 1
			continue
		n_rev_int = int(n_rev)
		if n1 - n_rev_int != 0 and (n1 + n_rev_int) % abs(n1 - n_rev_int) == 0:
			yield n1
		n1 += 1

def sum_dif_rev(n):
	p = sum_dif()
	return [next(p) for x in range(1, n + 1)][-1]


def sum_dif_rev(n):
	k = 0
	i = 1
	while i <= n:
		k += 1
		if str(k)[-1] != '0':
			p = int(str(k)[::-1])
			if abs(p - k) != 0:
				if (p + k) % abs(p - k) == 0:
					i += 1
	return k


buf = [45, 54, 495, 594]

def sum_dif_rev(n):
	i = buf[-1]
	while len(buf) < n:
		i += 1
		j = int(str(i)[::-1])
		d = abs(i - j)
		if i % 10 and d and (not (i + j) % d):
			buf.append(i)
	return buf[n - 1]