代码之家  ›  专栏  ›  技术社区  ›  GirkovArpa

如果已经知道任何一个数字,如何尽可能减少遍历4位PIN码的所有可能组合所需的时间?

  •  1
  • GirkovArpa  · 技术社区  · 5 年前

    如果我对一个PIN进行暴力破解,我可以使用以下代码遍历所有可能的PIN:

    for (PIN = 0000; PIN <= 9999; PIN++) {
      console.log(String(PIN).padStart(4, '0'));
    }
    3 7 ,我在浪费时间去重复不可能的组合。

    我可以检查循环内组合的有效性,如下所示:

    for (PIN = 0000; PIN <= 9999; PIN++) {
      if (String(PIN)[1] != '3' || String(PIN)[3] != '7') {
        continue;
      }
      console.log(String(PIN).padStart(4, '0'));
    }


    我并不是蛮力的逼着别针。我想用暴力破解魔方 this 竞争。但我想更多的人熟悉破解密码然后解魔方。

    2 回复  |  直到 5 年前
        1
  •  3
  •   Chloe Dev    5 年前

    你可以分别迭代两个未知数字。

    let timestamp = performance.now();
    
    for(let digit1 = 0; digit1 < 10; digit1++) {
      for(let digit2 = 0; digit2 < 10; digit2++) {
        const digit = digit1 + "3" + digit2 + "7";
        
        //console.log(digit);
      }
    }
    
    console.log("first iteration took " + (performance.now() - timestamp) + "ms");
    
    timestamp = performance.now();
    
    for (PIN = 0000; PIN <= 9999; PIN++) {
      if (String(PIN)[1] != '3' || String(PIN)[3] != '7') {
        continue;
      }
      
      //console.log(String(PIN).padStart(4, '0'));
    }
    
    console.log("second iteration took " + (performance.now() - timestamp) + "ms");
        2
  •  1
  •   Barmar    5 年前

    for (let first_digit = 0; first_digit <= 9000; first_digit += 1000) {
        for (let third_digit = 0; third_digit <= 90; third_digit += 10) {
            let PIN = first_digit + 300 + third_digit + 7;
            console.log(String(PIN).padStart(4, '0'));
        }
    }
    
    推荐文章