|
| 1 | +// potentially slow... |
| 2 | +// function getStrongness(value) { |
| 3 | +// let strongness = 0; |
| 4 | +// while (value % 2 === 0) { |
| 5 | +// value /= 2; // TODO: use docs speedup |
| 6 | +// strongness++; |
| 7 | +// } |
| 8 | +// return strongness; |
| 9 | +// } |
| 10 | + |
| 11 | +// function getStrongness(value) { |
| 12 | +// let strongness = 0; |
| 13 | +// while ((value & 1) === 0) { |
| 14 | +// value = value>>1; // TODO: use docs speedup |
| 15 | +// strongness++; |
| 16 | +// } |
| 17 | +// return strongness; |
| 18 | +// } |
| 19 | + |
| 20 | +// function strongestEven(n, m){ |
| 21 | +// const largest = { |
| 22 | +// value: 0, |
| 23 | +// strongness: 0, |
| 24 | +// }; |
| 25 | +// const calculated = {} |
| 26 | +// for (let value = n; value <= m; value++) { |
| 27 | +// let strongness; |
| 28 | +// if (calculated[value / 2]) { |
| 29 | +// strongness = calculated[value / 2] + 1; |
| 30 | +// calculated[value] = strongness; |
| 31 | +// } else { |
| 32 | +// strongness = getStrongness(value); |
| 33 | +// calculated[value] = strongness; |
| 34 | +// } |
| 35 | +// if (strongness > largest.strongness) { |
| 36 | +// largest.value = value; |
| 37 | +// largest.strongness = strongness; |
| 38 | +// } |
| 39 | +// } |
| 40 | +// return largest.value; |
| 41 | +// } |
| 42 | + |
| 43 | +// const lookup = [ |
| 44 | +// 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, |
| 45 | +// 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 |
| 46 | +// ]; |
| 47 | + |
| 48 | +// function getStrongness(value) { |
| 49 | +// return lookup[(value & -value) % 37]; |
| 50 | +// } |
| 51 | + |
| 52 | +// const clz = Math.clz32; |
| 53 | +// function getStrongness(integer){ // count trailing zeros |
| 54 | +// // 1. fill in all the higher bits after the first one |
| 55 | +// integer |= integer << 16; |
| 56 | +// integer |= integer << 8; |
| 57 | +// integer |= integer << 4; |
| 58 | +// integer |= integer << 2; |
| 59 | +// integer |= integer << 1; |
| 60 | +// // 2. Now, inversing the bits reveals the lowest bits |
| 61 | +// return 32 - clz(~integer) |0; // `|0` ensures integer coercion |
| 62 | +// } |
| 63 | + |
| 64 | +// function strongestEven(n, m){ |
| 65 | +// const largest = { |
| 66 | +// value: 0, |
| 67 | +// strongness: 0, |
| 68 | +// }; |
| 69 | +// let start = n % 2 === 0 ? n : n + 1; |
| 70 | +// for (let value = start; value <= m; value+=2) { |
| 71 | +// let strongness = getStrongness(value); |
| 72 | +// if (strongness > largest.strongness) { |
| 73 | +// largest.value = value; |
| 74 | +// largest.strongness = strongness; |
| 75 | +// } |
| 76 | +// } |
| 77 | +// return largest.value; |
| 78 | +// } |
| 79 | + |
| 80 | +const strongestEven = (m, n) => { |
| 81 | + const N=BigInt(n), M=BigInt(m); |
| 82 | + for (let b=1n<<53n, x; b>1n; b>>=1n) |
| 83 | + if ((x=(N+b-1n)/b*b)<=M) return Number(x); |
| 84 | + return n; |
| 85 | +} |
| 86 | + |
| 87 | +// thanks to doc |
| 88 | + |
| 89 | +// x>>1 divides by 2 pretty quickly |
| 90 | + |
| 91 | +// idea: search odd multiples of powers of two from large to small that fit in the range, so multiples of 2^31 then 2^30 then 2**29 ... |
| 92 | + |
| 93 | +// instead of searching all numbers, searching from big powers of two -> small powers of two (all odd multiples of them) will be more fasterer. There's probably some even faster number theory way using prime decomposition? |
| 94 | + |
| 95 | +// completely unhelpful comment: fastest way to check if number is divisible by 2^N -> does the binary representation of the number in memory end in N or more zeroes (bit scan / count trailing zeroes x86 instruction) - in JS you could do toString(2) and count the zeroes but that will be slower |
| 96 | + |
| 97 | +// just search for odd multiples of 2^31 in the range, then 2^30, then 2^29... |
| 98 | + |
| 99 | +// Thank you mertcane |
| 100 | +// Consider this : The (strongness of 2N) is the (strongness of N) + 1, if you cache the strongness of N, you don't need to calculate it again |
| 101 | + |
| 102 | + |
| 103 | +// ❣️ Thank you larry |
| 104 | +// sorry, just coming in, isn't this just the number of trailing 0s in the binary |
| 105 | + |
| 106 | +//and in that case, isn't it just the smallest even number >= some power of 2 |
| 107 | + |
| 108 | +// I think you can just count from powers of 2s, if not found, then the smallest even number between n and m, I think |
| 109 | + |
| 110 | +// console.log(getStrongness(12), 2); |
| 111 | +// console.log(getStrongness(16), 4); |
| 112 | +// console.log(getStrongness(3), 0); |
| 113 | + |
| 114 | + |
| 115 | +// console.log(strongestEven(1, 2), 2); |
| 116 | +// console.log(strongestEven(5, 10), 8); |
| 117 | +// console.log(strongestEven(48, 56), 48); |
| 118 | +// console.log(strongestEven(129, 193), 192); |
| 119 | + |
| 120 | +console.log(strongestEven(1180381085, 1590463313), 1342177280) |
0 commit comments