R

Any positive whole is part of the sequence of natural numbers, so the numbers will be in the sequence of numbers of natural numbers.If there is no need for a minimum index of sequencing, it may even be a closed formula to find an index where the given number is located (see below).There is a periodicity in the entry number to be used to find an estimated location where to search if it is necessary. least I'll find the index.For example, if the number figures are consistent:[d, d+1, d+2, d+3]
It is clear immediately that the smallest index at the beginning of the sequence (the natural numbers with one figure):1 2 3 4 5 6 7 8 9
If input (period=1):[d, x, d+1, x]
the index, among the numerals produced by the two-digit natural numbers.If the entry looks like (period=2):[x,y,d,x,y,d+1]
the number may be three-digit, idd.Example of the question: 22324 Looks like a second case. [d,x,d+1,x,d+2]- that says the number is in the area of two-digit numbers that start on. x=2 and intersects with sequence numbers n♪ n+1♪ n+2where n=22♪ Therefore, the answer 22324- it's the first digit index. n in sequence from question plus index where inlet n starting.If kNumber of figures n, the index n equal to: sum((j+1)*9*10**j for j in range(k)) + (n-10**(k-1))*k (number of numbers required for all numbers with fewer numbers (number of figures)<k) plus number of figures with the same number (k) that go before n) Amount http://wolframalpha.com/input/?i=sum+%28j%2B1%29*9*10%5Ej%2Cj%3D0..k-1 ♪For n=22: 9+(22-10)*2Therefore, the answer 33+1 for 22324. We can check it out.>>> import itertools
>>> g = (digit for number in itertools.count(1) for digit in map(int, str(number)))
>>> digits = 2, 2, 3, 2, 4
>>> i=9+(22-10)*2 + 1; list(itertools.islice(g, i, i+len(digits)))
[2, 2, 3, 2, 4]
+1 It's an internal displacement. nwhere digits starting in this case.If the number of numbers on the entrance is small, period You can find a simple internal bulkhead ( log n, which is much better than the sequence itself. n log n) When the number contains a transition between the divisions, for example: 8192021which corresponds to sequence 18 19 20 21It could be a special case to process (in cash 9 and/or 0).You can do no special cases and try all possible initial positions n and all possible dimensions n♪ This approach also does not require a redundancy.To find minimum In the sequence of natural numbers, where the number indicated is:def find_min_index(number):
# O(k**4) if get_digits() is O(k**2) (due to str(number))
digits = get_digits(number)
k = len(digits) # number of digits
# find minimal n and start,end such that digits =
# = get_digits(n)[start:] + get_digits(n+1) + ... + get_digits(n+m)[:end]
# brute force approach
for ndigits_in_n in range(1, k): # m ~ k / ndigits_in_n
for start in range(ndigits_in_n):
i = ndigits_in_n - start # where (n+1) starts in *digits*
# is10pow = (10**ndigits_in_n == (n+1))
is10pow = same_start(digits[i:i + ndigits_in_n + 1], [1] + [0] * ndigits_in_n)
# get_digits(n+1) == digits[i:i+ndigits_in_n+is10pow]
end = i + ndigits_in_n + is10pow
if end <= k: # enough space for all digits of (n+1) in *digits*
n = digits2number(digits[i:end]) - 1
elif start == 0: # all digits of n are in *digits*
n = digits2number(digits[:ndigits_in_n])
else:
continue #NOTE: assume at least one of n or n+1 is in *digits* as a whole
if matched_n(digits, n, start):
return get_index(n) + start
return get_index(number) # m == 0
Example:>>> find_min_index(22324)
34 # верно
where get_digits(number) returns the figures for the number transferred:def get_digits(number):
"""
>>> get_digits(22324)
[2, 2, 3, 2, 4]
"""
return list(map(int, str(number))) # NOTE: O(k**2) in CPython
a digits2number(digits) Repeat: Repeat the number corresponding to the figures transferred:import functools
def digits2number(digits):
"""
>>> digits2number([1,2,3])
123
"""
return functools.reduce(lambda number, digit: 10 * number + digit, digits)
same_start(L1, L2) Whether the sequence begins the same, all the relevant elements shall be equal, but the length may differ:def same_start(L1, L2):
"""Whether L1[:k] == L2[:k] where k = min(len(L1), len(L2))"""
return all(a == b for a, b in zip(L1, L2))
matched_n(digits, n, start) check whether the transferred figures are digits in a given location, in sequence, the numbers of natural numbers are localized by a natural number n and its position start:import itertools
def matched_n(digits, n, start):
"""Check whether digits =
= get_digits(n)[start:] + get_digits(n+1) + ... + get_digits(n+m)[:end]
"""
g = (digit for number in itertools.count(n + 1) for digit in get_digits(number))
return same_start(digits, itertools.chain(get_digits(n)[start:], g))
get_index(number) Repeat the position of the numbers of the natural number number in the sequence under review:def get_index(number):
"""sum((j + 1) * 9 * 10j for j in range(k - 1)) + (n - 10(k - 1)) * k
>>> get_index(22)
33
"""
k = ndigits10(number) - 1
return ((9 * k - 1) * 10**k + 1) // 9 + (number - 10**k) * (k + 1)
where ndigits10(number) Repeat how many figures in the decimal system are required for the submission of the natural number transmitted number:import math
def ndigits10(number):
"""The number of decimal digits in the natural number.
>>> ndigits10(1)
1
>>> ndigits10(99)
2
>>> ndigits10(100)
3
>>> ndigits10(101)
3
"""
assert number > 0
# 10**(k-1) <= number < 10**k
return math.floor(math.log10(number)) + 1
Q: What is it? What's that for? Pitton, True == 1 and 0 == False♪is10pow Is (n+1) dozens. Make the variable understand whether the number of figures is n and n+1♪ Number of figures (n+1) Always. ndigits_in_n + is10powi.e., it is equivalent to the number of figures n Or one more.Example:n=98Then ndigits10(n) == ndigits10(n+1) == 2n=99Then ndigits10(n) == (ndigits10(n+1) - 1) == 2The first cycle is overtaken by number,The second is the bulkhead for each number for consistency,i is the difference for bulkheadExternal cycle crosses all possible dimensions n Number: ndigits_in_n- that's the number of numbers n♪ The cycle is applied to all possible starting points in figures. n♪ In your example, 22324 starting Second Numberstart=1)i- it's an index where (n+1) Figures are starting in the given digits♪ Designation of the code to select all candidates nwhich are possible in the field ndigits_in_n♪ start♪ digitsto find n for which:get_digits(n)[start:] == digits[:i]
get_digits(n+1) == digits[i:i+ndigits_in_n+is10pow]
digits == get_digits(n)[start:] + get_digits(n+1) + ... + get_digits(n+m)[:end]
If not found, the index is returned for itself. number (all figures per natural number are equal number)