[英]how to find n'th digit in a number like 1491625…?
Let's concatenate the squares of numbers that start with 1. So, what is the n'th digit in this string ? 让我们连接以1开头的数字的平方。那么,这个字符串中的第n个数字是多少?
For example, the 10th digit is 4. 例如,第10位是4。
1 4 9 16 25 36 49 64 81
It is just an ordinary question that come to mine ordinary mind. 这只是一个普通的问题,是我的普通思想。 How can I solve this to sleep well tonight? 今晚如何才能解决这个问题呢? any algorithm without looping ? 没有循环的任何算法?
You can work enumerate how many 1-digit, 2-digit, 3-digit, etc. numbers there are in this sequence by taking square roots of powers-of-10. 您可以通过取10次幂的平方根来计算此序列中有多少1位,2位,3位等数字。 This will allow you to establish which number the n -th digit lies in. From there, it should be pretty trivial. 这将允许您确定第n个数字所在的数字。从那里,它应该是非常微不足道的。
This should be O(log n) complexity. 这应该是O(log n)复杂度。
ceil(log 10 (x+1)) will give you the number of digits in a number. ceil(log 10 (x + 1))将给出一个数字中的位数。 Iterate through the squares keeping a count of the total length and once you've reached or exceeded the target length n, you know you need the mth digit of the last number for some m (easy to work out). 迭代通过正方形保持总长度的计数,一旦达到或超过目标长度n,你就知道你需要m的最后一个数字的第m位数(易于计算)。 Get the mth digit of this number by dividing by 10 m-1 than taking the last digit with a mod 10. 通过除以10 m-1获得该数字的第m位数,而不是使用mod 10获取最后一位数字。
All-in-all, constant space overhead and O(n) runtime. 总而言之,恒定空间开销和O(n)运行时。
Lazy infinite lists in Haskell make this trivial to express naïvely. Haskell中的懒惰无限列表使这个无聊的表达天真。
ghci> concat [show $ i*i | i <- [1..]] !! 9 '4'
To solve this i have used Python Generators . 为了解决这个问题,我使用了Python生成器 。 My solution in Python: 我的Python解决方案:
def _countup(n):
while True:
yield n
n += 1
def get_nth_element(n):
i = 0 # Initialized just to keep track of iterations.
final_string = ''
cu_generator = _countup(0)
while True:
num = cu_generator.next()
final_string += str(num * num)
if len(final_string) > n:
print "Number of iterations %s" % i
return final_string[n]
i += 1
RUN: 跑:
>>> get_nth_element(1000)
Number of iterations 229
'2'
>>> get_nth_element(10000)
Number of iterations 1637
'7'
Why would you not loop over, taking each number, squaring and incrementing the count from 1 checking at each step if you have reached n? 如果你已达到n,你为什么不循环,取每个数字,平方并递增计数,从每一步检查1? You don't have to keep track of the whole number. 您无需跟踪整数。 It is a simple simulation exercise. 这是一个简单的模拟练习。 I afraid, I cannot identify a pattern or formula for this. 我担心,我无法确定这种模式或公式。
This is a direct port of ephemient's Haskell answer to Scala 这是一个直接端口的ephemient的Haskell对Scala的回答
Iterator.from(1).flatMap(x=>(x*x).toString.iterator).drop(9).next
returns 4
返回4
O(n) 上)
Iterator.from(1)
creates an infinite iterator that counts 1,2,3,4,....
. Iterator.from(1)
创建一个无数的迭代器,它计算1,2,3,4,....
(x*x).toString
computes squares of each of these and turns them into strings. 然后(x*x).toString
计算每个的平方并将它们变成字符串。 flatMap( ... .iterator)
concatenates these to become an infinite iterator of characters from the sequence in question flatMap( ... .iterator)
将这些连接起来,成为所讨论序列中字符的无限迭代器 drop(9)
removes the first 9 elements (indexes 0 thru 8) from the iterator and gives us a new iterator that's waiting at index 9 drop(9)
从迭代器中删除前9个元素(索引0到8)并给我们一个等待索引9的新迭代器 next
gives us that single character. next
给我们那个单一的角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.