按顺序生成数字 [英] Generate numbers in sequence Order

查看:40
本文介绍了按顺序生成数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成通过检查中输入的位置搜索的值.例如,如果输入20,则函数应生成从0开始的数字,并以升序继续直到创建20位数字,然后在生成的数字字符串(01234567891011121314)中输出第20位数字的值,即4.我在下面尝试过此方法,但是在涉及1,000,000,000之类的数字时效率不高

  [... Array(5).keys()];输出=>[0,1,2,3,4] 

编辑此帖子以澄清我正在尝试获得更有效的解决方案.在这里,我试图在一秒钟之内得到长整数(1,000,000,000)的答案.

我已经有一个解决方案,但是要花1秒钟以上的时间.

  [... Array(5).keys()].join(")[4];输出=>4 

解决方案

这几乎与

(不幸的是,Stack Overflow不支持MathJax)

第一步是找到您所处的年代.一位数字中有9位数字,两位数字中有2⋅90= 180位数字,总共189,并且通常是n⋅9⋅10n−n个数字中的1.找到十年后,您可以从早期的十年中减去数字.因此,如果您想要第765位,那么前189位来自第一个和第二个十年,因此我们需要3位数字中的第576位.这将是⌈5763⌉= 192的数字,即291.作为576≡3(mod3),数字为1

以编程方式:

  const getDigit =(target)=>{让我= 0;让xDigitNumbers = 1;//例如1位数字,2位数字let digitsSoFar = 1;而(true){const digitsThisDecade = xDigitNumbers * 9 * 10 **(xDigitNumbers-1);if(digitsSoFar + digitsThisDecade>目标){//这就是目标数字为的十年"//digitIndexThisDecade:例如,从'100101102'开始,要查找'101'中的最后一个'1',digitIndexThisDecade将为6const digitIndexThisDecade = target-digitsSoFar;//numIndexThisDecade:标识十年中数字的索引//例如,从"100101102"开始,它可以是索引2,以对应于101(一个索引)const numIndexThisDecade = Math.floor(digitIndexThisDecade/xDigitNumbers);//decadeStartNum:十年开始之前的数字(0、9、99、999)const decadeStartNum = 10 **(xDigitNumbers-1);//num:目标索引所在的数字,例如101const num = decadeStartNum + numIndexThisDecade;//digitIndexInNum:目标为num的数字索引//例如,对于101,定位最后一个"1"将来自digitIndexInNum 2(零索引)const digitIndexInNum = digitIndexThisDecade%xDigitNumbers;返回字符串(数字)[digitIndexInNum]}digitsSoFar + = digitsThisDecade;xDigitNumbers ++;}};for(让i = 0; i< 1000; i ++){document.write(`$ {i}:$ {getDigit(i)}< br>`);}  

I want to generate the value being searched by the position entered in the check. For example, if 20 is entered, the function should generate numbers starting from 0 and continue in ascending order until 20 digits are created, then output the value of the 20th digit in the generated number string (01234567891011121314), which is 4. I tried this below, however it is not efficient when it comes to numbers like 1,000,000,000,

[...Array(5).keys()];  output => [0, 1, 2, 3, 4]

Edit this post to clarify I am trying to get a more efficient solution. Here I am trying to get the answer for long numbers(1,000,000,000) in below one second.

I already have a solution but it takes more than 1 second.

 [...Array(5).keys()].join("")[4]; output => 4

解决方案

This is nearly identical to the Champernowne constant.

A solution from math.stackexchange is:

(Stack Overflow doesn't support MathJax, unfortunately)

The first step is to find what decade you are in. There are 9 digits from the 1 digit numbers, 2⋅90=180 digits from the 2 digit numbers for a total of 189, and generally n⋅9⋅10n−1 from the n digit numbers. Once you have found the decade, you can subtract the digits from the earlier decades. So if you want the 765th digit, the first 189 come from the first and second decades, so we want the 576th digit of the 3 digit numbers. This will come in the ⌈5763⌉=192nd number, which is 291. As 576≡3(mod3), the digit is 1

Programatically:

const getDigit = (target) => {
  let i = 0;
  let xDigitNumbers = 1; // eg 1 digit numbers, 2 digit numbers
  let digitsSoFar = 1;
  while (true) {
    const digitsThisDecade = xDigitNumbers * 9 * 10 ** (xDigitNumbers - 1);
    if (digitsSoFar + digitsThisDecade > target) {
      // Then this is the "decade" in which the target digit is
      
      // digitIndexThisDecade: eg, starting from '100101102', to find the last '1' in '101', digitIndexThisDecade will be 6
      const digitIndexThisDecade = target - digitsSoFar;
      // numIndexThisDecade: this identifies the index of the number in the decade
      // eg, starting from '100101102', this could be index 2 to correspond to 101 (one-indexed)
      const numIndexThisDecade = Math.floor(digitIndexThisDecade / xDigitNumbers);
      // decadeStartNum: the number right before the decade starts (0, 9, 99, 999)
      const decadeStartNum = 10 ** (xDigitNumbers - 1);
      // num: the number in which the target index lies, eg 101
      const num = decadeStartNum + numIndexThisDecade;
      // digitIndexInNum: the digit index in num that the target is
      // eg, for 101, targeting the last '1' will come from a digitIndexInNum of 2 (zero-indexed)
      const digitIndexInNum = digitIndexThisDecade % xDigitNumbers;
      return String(num)[digitIndexInNum]
    }
    digitsSoFar += digitsThisDecade;
    xDigitNumbers++;
  }
};



for (let i = 0; i < 1000; i++) {
  document.write(`${i}: ${getDigit(i)}<br>`);
}

这篇关于按顺序生成数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆