简体   繁体   English

如何找到像1491625这样的数字中的第n位?

[英]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,....
  • Then (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.

相关问题 查找字符串中第 n 个数字的高效算法 112123123412345 - Efficient algorithm to find the n-th digit in the string 112123123412345 在{1,…,n}中找到以7作为最后一位的最大质数 - Find the greatest prime number with 7 as the last digit in {1, …, n} 查找数组中第n1个最小数到第n2个最小数的快速算法 - Fast algorithm to find the n1 th smallest number to n2 th smallest number in an array 给定 n,找出有多少个 n 位数字,使得该数字在素数索引处具有素数并且可以被 m 整除? - Given n, find how many n-digit numbers are there such that the number has prime digit at prime indices and is divisible by m? 给定数字n,找出0 ... n范围内有数字2的数字 - Given a number n, find out how many numbers have digit 2 in the range 0…n 为第 n 个斐波那契数编程大斐波那契数的最后一位。 C++ - Program Last Digit of a Large Fibonacci Number for n-th Fibonacci number. C++ 如何找到小于N的最大数M,其中Java中不允许存在某些特定数字 - how to find maximum number M less than N in which some specific digit is not allowed to exist in Java 用于计算不包含给定数字的第n个数字的程序 - Program to compute n-th number that doesn't contain given digit 查找n位数的子序列数,该子序列可被8整除 - Find the number of subsequences of a n-digit number, that are divisible by 8 如何减少在不到1秒的时间内从连续数字找到第n位的时间 - How to reduce time to find the n-th place from consecutive digits number for less than 1 second
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM