The first positive integer, `n`, with its value `4n² + 1`, being divisible by `5` and `13` is `4`. (condition 1)

It can be demonstrated that we have infinite numbers that may satisfy the above condition.

If we name **ai**, the different terms of the sequence of numbers with this property, we define `S(n)` as:



We are interested in working with numbers of this sequence from 10 to 15 digits. Could you elaborate a solution for these results?

You will be given an integer value `m`(always valid an positive) and you should output the closest value of the sequence to `m`.

If the given value `m` is in the sequence, your solution should return the same value.

In the case that there are two possible solutions: `s1` and `s2`, (`s1 < s2`), because `|m - s1| = |m - s2|`, output the highest solution `s2`

No hardcoded solutions are allowed.

No misterious formulae are required, just good observation to discover hidden patterns.

See the example tests to see output format and useful examples, edge cases are included.

Features of the random tests
`1000 <= m <= 5.2 e14`

**Note** Sierpinsky presented the numbers that satisfy condition (1)

Source: codewars | Difficulty: EASY
Tags: Number theory

Fundamentals

Starter code: def find_closest_value(m):
	

Solutions:
def sierpinski():
	x = s = 0
	while 1:
		for a in (4, 9, 56, 61):
			s += x + a
			yield s
		x += 65
s = sierpinski()
S = [next(s)]
from bisect import bisect_left

def find_closest_value(m):
	while S[-1] < m:
		S.append(next(s))
	i = bisect_left(S, m)
	return min(S[i:i - 2:-1], key=lambda n: abs(m - n))


import math

def find_closest_value(Q):
	B = math.ceil((math.ceil(Q / 65) / 2) ** 0.5)
	(S, B) = (130 * B * B, 65 * B)
	return S - min((0, B - 4, B + B - 13, 3 * B - 69, 4 * B - 130), key=lambda V: abs(Q - S + V)) or 4


def find_closest_value(m):
	x = int((m / 130) ** 0.5)
	(y, z) = (130 * x ** 2, 65 * x)
	res = [y + v + i * z for (i, v) in enumerate((0, 4, 13, 69, 130))]
	return min(res, key=lambda k: (abs(m - k), m - k)) or 4


class SierpinskiSumFunc:

	def __init__(self):
		sierpinski_number = 4
		sierpinski_numbers_add = (5, 47, 5, 8)
		sierpinski_sum = [4]
		n = 0
		while sierpinski_sum[-1] < 520000000000000:
			sierpinski_number += sierpinski_numbers_add[n]
			sierpinski_sum.append(sierpinski_sum[-1] + sierpinski_number)
			n = 0 if n == 3 else n + 1
		self.sierpinski_sum = tuple(sierpinski_sum)
sierpinski_sum_obj = SierpinskiSumFunc()

def find_closest_value(m):
	if m <= 4:
		return 4
	if m <= 100000:
		sierpinski_sum = sierpinski_sum_obj.sierpinski_sum
		nn = 0
		while True:
			if m == sierpinski_sum[nn]:
				return m
			elif m < sierpinski_sum[nn + 1] and m > sierpinski_sum[nn]:
				return sierpinski_sum[nn + 1] if m - sierpinski_sum[nn + 1] >= sierpinski_sum[nn] - m else sierpinski_sum[nn]
			nn += 1
	elif m > 100000:
		sierpinski_sum = sierpinski_sum_obj.sierpinski_sum
		nn = -1
		while True:
			if m == sierpinski_sum[nn]:
				return m
			elif m > sierpinski_sum[nn - 1] and m < sierpinski_sum[nn]:
				return sierpinski_sum[nn - 1] if m - sierpinski_sum[nn - 1] < sierpinski_sum[nn] - m else sierpinski_sum[nn]
			if m < sierpinski_sum[nn - 20]:
				nn -= 20
			else:
				nn -= 1


import math

def find_closest_value(x):
	if x < 4:
		return 4
	n = math.sqrt(x / 130)
	n_low = math.floor(n)
	seed = 130 * n_low ** 2
	n_inc = n_low * 65
	candidates = [seed, seed + 4 + 1 * n_inc, seed + 13 + 2 * n_inc, seed + 69 + 3 * n_inc, seed + 130 + 4 * n_inc]
	candidates = sorted(candidates, reverse=True)
	candidates = sorted(candidates, key=lambda a: abs(a - x))
	return candidates[0]


s = [0]
for m in range(0, 10 ** 9):
	for p in (4, 9, 56, 61):
		s.append(s[-1] + m * 65 + p)
	if s[-1] > 52 * 10 ** 13:
		break
from bisect import bisect

def find_closest_value(m):
	if m < 4:
		return 4
	i = bisect(s, m)
	return s[min([i, i - 1], key=lambda i: abs(s[i] - m))]


def find_closest_value(m):
	if m < 4:
		return 4
	l = [130 * k ** 2 + k * i * 65 + a for (i, a) in enumerate([4, 13, 69, 130], 1) for k in [int((m // 130) ** 0.5) - 1, int((m // 130) ** 0.5)]]
	return min(l, key=lambda x: (abs(m - x), -x))


def find_closest_value(m):
	a = lambda i: (i - 1) // 4 * 65 + [4, 9, 56, 61][(i - 1) % 4]
	s = lambda n: a(-~((n * n - 1) // 2)) + (4 if n % 2 == 0 else 0)
	n = int((m // 65 * 8) ** 0.5)
	while s(n) <= m:
		n += 1
	if n == 1:
		return 4
	d1 = s(n) - m
	d2 = m - s(n - 1)
	return s(n) if d1 <= d2 else s(n - 1)