The number ```89``` is the first positive integer that has a particular, curious property:

The square of ```89``` is ```7921```; ```89² = 7921```

The reverse of ```7921``` is ```1297```, and ```1297``` is a prime number.

The cube of ```89``` is ```704969```; ```89³ = 704969```

The reverse of ```704969``` is ```969407```, and ```969407``` is a prime number.

The first four terms of this sequence having this special property are:
```
n-th term      term value
    1               89 
    2              271
    3              325
    4              328
```

Create a function ```sq_cub_rev_prime()```, that receives the ordinal number of the sequence and outputs its correspoding value.

Use the above table to show how the function should work:
```python 
sq_cub_rev_prime(1) == 89 
sq_cub_rev_prime(2) == 271
sq_cub_rev_prime(3) == 325
sq_cub_rev_prime(4) == 328
```
Your code will be tested up to the 250th term

This is not a registered sequence of OESIS, so if you are one of the first ten people that solve this kata, you may have the privilege to register the sequence at https://oeis.org, with your name. If you do so, please, mention in your biography that you are a Codewarrior.

Memoize your results to pass the tests.

Enjoy it!

Source: codewars | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Fundamentals

Mathematics

Starter code: def sq_cub_rev_prime(n):
	

Solutions:
sq_cub_rev_prime = (None, 89, 271, 325, 328, 890, 1025, 1055, 1081, 1129, 1169, 1241, 2657, 2710, 3112, 3121, 3149, 3244, 3250, 3263, 3280, 3335, 3346, 3403, 4193, 4222, 4231, 4289, 4291, 5531, 5584, 5653, 5678, 5716, 5791, 5795, 5836, 5837, 8882, 8900, 8926, 8942, 9664, 9794, 9875, 9962, 10178, 10250, 10393, 10429, 10499, 10550, 10577, 10651, 10679, 10717, 10718, 10739, 10756, 10762, 10810, 10844, 10895, 10898, 10943, 10996, 11035, 11039, 11084, 11137, 11159, 11164, 11182, 11191, 11290, 11351, 11371, 11575, 11690, 11695, 11707, 11722, 11732, 11795, 11827, 11861, 11885, 12109, 12124, 12242, 12268, 12304, 12361, 12362, 12410, 12433, 12436, 12535, 19144, 19267, 19271, 19273, 19385, 19433, 19442, 19451, 19501, 19564, 19597, 19603, 19631, 19637, 19766, 19846, 19865, 19871, 19909, 19927, 26464, 26491, 26570, 26579, 26621, 26704, 26944, 26965, 27001, 27029, 27052, 27100, 27101, 31120, 31210, 31223, 31237, 31261, 31327, 31331, 31351, 31463, 31469, 31490, 31534, 31561, 31657, 31726, 31739, 31784, 31807, 31883, 31928, 31978, 32066, 32072, 32213, 32255, 32308, 32431, 32440, 32446, 32500, 32539, 32564, 32573, 32630, 32656, 32708, 32749, 32759, 32800, 32888, 32969, 33059, 33254, 33325, 33338, 33350, 33404, 33460, 33475, 33509, 33568, 33575, 33701, 33833, 34030, 34112, 34159, 34163, 41351, 41429, 41473, 41501, 41608, 41639, 41839, 41879, 41930, 41933, 41992, 42029, 42089, 42103, 42121, 42179, 42220, 42235, 42310, 42326, 42385, 42463, 42466, 42524, 42575, 42607, 42682, 42782, 42839, 42890, 42910, 42982, 43045, 43049, 54986, 54991, 55073, 55310, 55492, 55589, 55598, 55603, 55651).__getitem__


mem = []

def sq_cub_rev_prime(n):
	x = mem[-1] + 1 if mem else 89
	while len(mem) < n:
		if is_prime(reverse(x ** 2)) and is_prime(reverse(x ** 3)):
			mem.append(x)
		x += 1
	return mem[n - 1]

def reverse(x):
	return int(str(x)[::-1])

def is_prime(x):
	return pow(2, x - 1, x) == 1


def _try_composite(a, d, n, s):
	if pow(a, d, n) == 1:
		return False
	for i in range(s):
		if pow(a, 2 ** i * d, n) == n - 1:
			return False
	return True

def is_prime(n, _precision_for_huge_n=16):
	if n in _known_primes:
		return True
	if any((n % p == 0 for p in _known_primes)) or n in (0, 1):
		return False
	(d, s) = (n - 1, 0)
	while not d % 2:
		(d, s) = (d >> 1, s + 1)
	if n < 1373653:
		return not any((_try_composite(a, d, n, s) for a in (2, 3)))
	if n < 25326001:
		return not any((_try_composite(a, d, n, s) for a in (2, 3, 5)))
	if n < 118670087467:
		if n == 3215031751:
			return False
		return not any((_try_composite(a, d, n, s) for a in (2, 3, 5, 7)))
	if n < 2152302898747:
		return not any((_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11)))
	if n < 3474749660383:
		return not any((_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11, 13)))
	if n < 341550071728321:
		return not any((_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11, 13, 17)))
	return not any((_try_composite(a, d, n, s) for a in _known_primes[:_precision_for_huge_n]))
_known_primes = [2, 3]
_known_primes += [x for x in range(5, 1000, 2) if is_prime(x)]
counter = 1
i = 1
results = {}
while counter < 251:
	square = i * i
	cube = square * i
	rs = int(''.join(reversed(str(square))))
	rc = int(''.join(reversed(str(cube))))
	if is_prime(rs) and is_prime(rc):
		results[counter] = i
		counter += 1
	i += 1

def sq_cub_rev_prime(n):
	return results[n]


(r, p) = (lambda n: int(str(n)[::-1]), lambda n: pow(2, n, n) == 2)
sq_cub_rev_prime = ([0] + [n for n in range(2, 10 ** 5) if p(r(n ** 2)) * p(r(n ** 3))]).__getitem__


li = [1, 89, 271, 325, 328, 890, 1025, 1055, 1081, 1129, 1169, 1241, 2657, 2710, 3112, 3121, 3149, 3244, 3250, 3263, 3280, 3335, 3346, 3403, 4193, 4222, 4231, 4289, 4291, 5531, 5584, 5653, 5678, 5716, 5791, 5795, 5836, 5837, 8882, 8900, 8926, 8942, 9664, 9794, 9875, 9962, 10178, 10250, 10393, 10429, 10499, 10550, 10577, 10651, 10679, 10717, 10718, 10739, 10756, 10762, 10810, 10844, 10895, 10898, 10943, 10996, 11035, 11039, 11084, 11137, 11159, 11164, 11182, 11191, 11290, 11351, 11371, 11575, 11690, 11695, 11707, 11722, 11732, 11795, 11827, 11861, 11885, 12109, 12124, 12242, 12268, 12304, 12361, 12362, 12410, 12433, 12436, 12535, 19144, 19267, 19271, 19273, 19385, 19433, 19442, 19451, 19501, 19564, 19597, 19603, 19631, 19637, 19766, 19846, 19865, 19871, 19909, 19927, 26464, 26491, 26570, 26579, 26621, 26704, 26944, 26965, 27001, 27029, 27052, 27100, 27101, 31120, 31210, 31223, 31237, 31261, 31327, 31331, 31351, 31463, 31469, 31490, 31534, 31561, 31657, 31726, 31739, 31784, 31807, 31883, 31928, 31978, 32066, 32072, 32213, 32255, 32308, 32431, 32440, 32446, 32500, 32539, 32564, 32573, 32630, 32656, 32708, 32749, 32759, 32800, 32888, 32969, 33059, 33254, 33325, 33338, 33350, 33404, 33460, 33475, 33509, 33568, 33575, 33701, 33833, 34030, 34112, 34159, 34163, 41351, 41429, 41473, 41501, 41608, 41639, 41839, 41879, 41930, 41933, 41992, 42029, 42089, 42103, 42121, 42179, 42220, 42235, 42310, 42326, 42385, 42463, 42466, 42524, 42575, 42607, 42682, 42782, 42839, 42890, 42910, 42982, 43045, 43049, 54986, 54991, 55073, 55310, 55492, 55589, 55598, 55603, 55651, 55697, 55718, 55778, 55840, 55859, 55879, 55916, 56005, 56093, 56261, 56279, 56356, 56530, 56681, 56780, 56809, 56968, 57160, 57185, 57251, 57479, 57668, 57715, 57809, 57874, 57875, 57910, 57946, 57950, 57973, 58282, 58303, 58312, 58360, 58370, 58415, 58438, 58439, 58444, 88817, 88820, 88841, 88897, 88945, 88948, 88976, 89000, 89068, 89101, 89137, 89176, 89251, 89260, 89420, 96640, 96653, 96733, 96832, 97033, 97076, 97309, 97375, 97423, 97459, 97558, 97604, 97754, 97940, 98113, 98168, 98228, 98302, 98371, 98462, 98536, 98618, 98647, 98744, 98750, 98837, 98854, 99142, 99620, 99703, 99751, 99998]

def sq_cub_rev_prime(n):
	return li[n]


nums = {0: 89, 1: 271, 2: 325, 3: 328, 4: 890, 5: 1025, 6: 1055, 7: 1081, 8: 1129, 9: 1169, 10: 1241, 11: 2657, 12: 2710, 13: 3112, 14: 3121, 15: 3149, 16: 3244, 17: 3250, 18: 3263, 19: 3280, 20: 3335, 21: 3346, 22: 3403, 23: 4193, 24: 4222, 25: 4231, 26: 4289, 27: 4291, 28: 5531, 29: 5584, 30: 5653, 31: 5678, 32: 5716, 33: 5791, 34: 5795, 35: 5836, 36: 5837, 37: 8882, 38: 8900, 39: 8926, 40: 8942, 41: 9664, 42: 9794, 43: 9875, 44: 9962, 45: 10178, 46: 10250, 47: 10393, 48: 10429, 49: 10499, 50: 10550, 51: 10577, 52: 10651, 53: 10679, 54: 10717, 55: 10718, 56: 10739, 57: 10756, 58: 10762, 59: 10810, 60: 10844, 61: 10895, 62: 10898, 63: 10943, 64: 10996, 65: 11035, 66: 11039, 67: 11084, 68: 11137, 69: 11159, 70: 11164, 71: 11182, 72: 11191, 73: 11290, 74: 11351, 75: 11371, 76: 11575, 77: 11690, 78: 11695, 79: 11707, 80: 11722, 81: 11732, 82: 11795, 83: 11827, 84: 11861, 85: 11885, 86: 12109, 87: 12124, 88: 12242, 89: 12268, 90: 12304, 91: 12361, 92: 12362, 93: 12410, 94: 12433, 95: 12436, 96: 12535, 97: 19144, 98: 19267, 99: 19271, 100: 19273, 101: 19385, 102: 19433, 103: 19442, 104: 19451, 105: 19501, 106: 19564, 107: 19597, 108: 19603, 109: 19631, 110: 19637, 111: 19766, 112: 19846, 113: 19865, 114: 19871, 115: 19909, 116: 19927, 117: 26464, 118: 26491, 119: 26570, 120: 26579, 121: 26621, 122: 26704, 123: 26944, 124: 26965, 125: 27001, 126: 27029, 127: 27052, 128: 27100, 129: 27101, 130: 31120, 131: 31210, 132: 31223, 133: 31237, 134: 31261, 135: 31327, 136: 31331, 137: 31351, 138: 31463, 139: 31469, 140: 31490, 141: 31534, 142: 31561, 143: 31657, 144: 31726, 145: 31739, 146: 31784, 147: 31807, 148: 31883, 149: 31928, 150: 31978, 151: 32066, 152: 32072, 153: 32213, 154: 32255, 155: 32308, 156: 32431, 157: 32440, 158: 32446, 159: 32500, 160: 32539, 161: 32564, 162: 32573, 163: 32630, 164: 32656, 165: 32708, 166: 32749, 167: 32759, 168: 32800, 169: 32888, 170: 32969, 171: 33059, 172: 33254, 173: 33325, 174: 33338, 175: 33350, 176: 33404, 177: 33460, 178: 33475, 179: 33509, 180: 33568, 181: 33575, 182: 33701, 183: 33833, 184: 34030, 185: 34112, 186: 34159, 187: 34163, 188: 41351, 189: 41429, 190: 41473, 191: 41501, 192: 41608, 193: 41639, 194: 41839, 195: 41879, 196: 41930, 197: 41933, 198: 41992, 199: 42029, 200: 42089, 201: 42103, 202: 42121, 203: 42179, 204: 42220, 205: 42235, 206: 42310, 207: 42326, 208: 42385, 209: 42463, 210: 42466, 211: 42524, 212: 42575, 213: 42607, 214: 42682, 215: 42782, 216: 42839, 217: 42890, 218: 42910, 219: 42982, 220: 43045, 221: 43049, 222: 54986, 223: 54991, 224: 55073, 225: 55310, 226: 55492, 227: 55589, 228: 55598, 229: 55603, 230: 55651, 231: 55697, 232: 55718, 233: 55778, 234: 55840, 235: 55859, 236: 55879, 237: 55916, 238: 56005, 239: 56093, 240: 56261, 241: 56279, 242: 56356, 243: 56530, 244: 56681, 245: 56780, 246: 56809, 247: 56968, 248: 57160, 249: 57185}

def sq_cub_rev_prime(n):
	return nums[n - 1]