网上偶然碰到一道 JS 题目,做到半截没思路了,求助
发布于 7 年前 作者 winglight 5007 次浏览 来自 问答

有这么个数组: [25867, 13799, 14327, 25933, 14447, 14087, 25471, 13249, 13457, 25303, 25423, 14639, 14489, 12757, 12589, 14747, 14177, 13627, 14401, 13147, 14051, 26053, 13159, 26309, 25793, 14149, 25841, 12763, 14639, 26309, 25847, 25171, 25073, 25747, 26237, 25679, 12799, 25913, 14843, 13309] 需要转换成字母和数字的一串字符,提示是:ASCII 我试了几种组合出来的都是有各种符号的字符串,请教高手这种五位数字要怎么转换?

补充一下:最后得到的数字应该是仅仅由字母和数字组成的类似密码一样的随机字符串组成。这个数组我猜测应该是可以转成 ASCII 码的十进制:

1-10:80-89

A-Z:65-90

a-z:97-122

排除 16 进制是因为没有出现 ABCDE,目前就这些进展,下面是原始的题目,大家也可以参考一下

{ “instructions”: “Remove non prime numbers from data then right shift [322th decimal of the constant quantity that determines the area of a circle by multiplying it by the radius squared] bits from the remaining numbers, it will give you the alphanumeric password of the next level”, “numbers”: [ 25867, 13799, 21482, 53921, 47007, 49366, 14327, 25933, 21273, 62148, 14176, 12808, 14447, 27848, 58068, 10178, 14087, 59292, 11214, 20913, 34717, 25471, 51560, 44315, 3565, 64856, 44700, 13249, 59231, 29574, 23457, 8656, 13457, 46453, 57613, 49252, 25303, 12388, 36165, 25423, 14639, 14489, 46704, 47425, 61421, 12757, 55193, 12589, 30639, 14747, 43083, 32278, 14177, 36145, 13269, 60743, 49602, 46120, 950, 15315, 13627, 14401, 34785, 10730, 54048, 48931, 7197, 13147, 4950, 50446, 14051, 34377, 31909, 26053, 40948, 48302, 13159, 24393, 26309, 17079, 25793, 10032, 14149, 8799, 60764, 8512, 61130, 45268, 49655, 26114, 58837, 25841, 49625, 57651, 49491, 12763, 14639, 26309, 24045, 25847, 25171, 63458, 17745, 38110, 27636, 18585, 25073, 40319, 25747, 26237, 25679, 12799, 57711, 1518, 55981, 27000, 4462, 25913, 44948, 555, 43094, 59661, 3452, 20068, 14843, 13309 ] }

3 回复
function isPrime(num) {
    for (let l = Math.sqrt(num), i = 2; i <= l; i++) {
        if (num % i === 0) return false;
    }
    return true;
}

const nums = [ 25867, 13799, 21482, 53921, 47007, 49366, 14327, 25933, 21273, 62148, 14176, 12808, 14447, 27848, 58068, 10178, 14087, 59292, 11214, 20913, 34717, 25471, 51560, 44315, 3565, 64856, 44700, 13249, 59231, 29574, 23457, 8656, 13457, 46453, 57613, 49252, 25303, 12388, 36165, 25423, 14639, 14489, 46704, 47425, 61421, 12757, 55193, 12589, 30639, 14747, 43083, 32278, 14177, 36145, 13269, 60743, 49602, 46120, 950, 15315, 13627, 14401, 34785, 10730, 54048, 48931, 7197, 13147, 4950, 50446, 14051, 34377, 31909, 26053, 40948, 48302, 13159, 24393, 26309, 17079, 25793, 10032, 14149, 8799, 60764, 8512, 61130, 45268, 49655, 26114, 58837, 25841, 49625, 57651, 49491, 12763, 14639, 26309, 24045, 25847, 25171, 63458, 17745, 38110, 27636, 18585, 25073, 40319, 25747, 26237, 25679, 12799, 57711, 1518, 55981, 27000, 4462, 25913, 44948, 555, 43094, 59661, 3452, 20068, 14843, 13309 ];
let ret = ''

for (let i = 0; i < nums.length; i++) {
    if (!isPrime(nums[i])) continue;
    ret += String.fromCharCode(nums[i] >>> 8);
}
console.log(ret);

PS: 刚喝完酒比较脑残Orz, Remove non prime 理解成 !isPrime, 结果纠结半天囧

话说这个 π 的 322位, 我是当小数点后322位(也就是8)处理的, 算上小数点前的3, 小数点321位是 4, 这样右移后的结果怎么都不正常…说到底没办法验证答案对不对, 看出题人脑洞了

@ekoneko 感谢回复!很抱歉,好久没上来cnode,这问题在v2那边已经有高手解决了,322位就是你理解的那种

这个判断是不是素数干嘛用的,还有无符号右移

回到顶部