javascript - 按顺序生成数字 Order

标签 javascript numbers

我想生成由支票中输入的位置搜索的值。例如,如果输入 20,则函数应生成从 0 开始的数字,并继续按升序排列,直到创建 20 位数字,然后输出生成的数字字符串中第 20 位的值(01234567891011121314),即 4。 我在下面尝试了这个,但是当涉及到 1,000,000,000 这样的数字时,它效率不高,

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

编辑这篇文章以澄清我正在尝试获得更有效的解决方案。 在这里,我试图在一秒内得到长数字(1,000,000,000)的答案。

我已经有了解决方案,但需要超过 1 秒的时间。

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

最佳答案

这与 Champernowne constant 几乎相同.

来自math.stackexchange的解决方案是:

enter image description here

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

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

以编程方式:

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>`);
}

关于javascript - 按顺序生成数字 Order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60052958/

相关文章:

python - 不同数字列表中的第一个公共(public)元素

php - 使用 sprintf 打印浮点值

javascript - jquery 函数原型(prototype)

javascript - 如何根据关键条件通过两个键对对象进行排序

javascript - AngularJS:将指令应用于所有匹配的标签

javascript - 使用文件名结尾将所有 reducer 、操作导入 Redux。即 'TodoList_Reducers.js'

hash - 如何从3个不同的整数中创建唯一的整数(1个Oracle Long,1个Date字段,1个Short)

javascript - 如何在javascript中从左到右从字符串序列中选择数字整数

algorithm - 比较乘法

javascript - HTML5视频搜索