F
F
FoxSparky2019-02-19 10:13:11
Algorithms
FoxSparky, 2019-02-19 10:13:11

How to write a verbal algorithm for this task?

Task: Given a natural number k. Output the k-th digit of the sequence 149162536, in which the squares of all natural numbers are written in a row.
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2019-02-19
@sergiks

The number of digits in the number is obtained by rounding up the ratio of the natural logarithm of this number to the natural logarithm of 10: ceil( ln(x) / ln(10))Special case "one" - for it the logarithm will give 0, so we will add the length of the unit squared manually)
You can create a sequence length counter and a variable for the current number . Starting from 1, accumulate the total length of the sequence in the counter, adding the number of digits in the next square.
When the sequence hits at least the given K, the current value squared contains the digit you are looking for. The difference between the length of the dialed sequence and K will determine which digit of this square to take.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question