Created
February 4, 2026 21:38
-
-
Save cemdrk/f5b20b4104ad5dbf46e653882055208c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { | |
| Worker | |
| } = require('worker_threads'); | |
| // const NUM_WORKERS = os.cpus().length; | |
| const NUM_WORKERS = 12; | |
| const START = 100; | |
| const END = 1_000_000_000; | |
| const CHUNK_SIZE = Math.ceil((END - START) / NUM_WORKERS); | |
| let completedWorkers = 0; | |
| console.log(`Starting ${NUM_WORKERS} workers...`); | |
| console.log(`Range: ${START} to ${END}`); | |
| console.time('Total time'); | |
| const completed = [] | |
| for (let i = 0; i < NUM_WORKERS; i++) { | |
| const rangeStart = START + i * CHUNK_SIZE; | |
| const rangeEnd = Math.min(rangeStart + CHUNK_SIZE, END); | |
| const worker = new Worker("./kapre.js", { | |
| workerData: { | |
| rangeStart, | |
| rangeEnd, | |
| workerId: i + 1, | |
| } | |
| }); | |
| worker.on('message', ({ | |
| workerId, | |
| seen | |
| }) => { | |
| console.log(`Worker ${workerId} completed`); | |
| console.log(workerId, seen) | |
| completed.push(seen) | |
| }); | |
| worker.on('error', err => console.error(`Worker ${i + 1} error:`, err)); | |
| worker.on('exit', () => { | |
| completedWorkers++; | |
| if (completedWorkers === NUM_WORKERS) { | |
| console.timeEnd('Total time'); | |
| console.log(completed) | |
| } | |
| }); | |
| } | |
| ///worker | |
| const { | |
| parentPort, | |
| workerData | |
| } = require('worker_threads'); | |
| const { | |
| rangeStart, | |
| rangeEnd, | |
| workerId, | |
| } = workerData; | |
| console.log(workerId, 'started') | |
| const maxNum2 = num => { | |
| const count = new Uint8Array(10); | |
| for (let n = num; n > 0; n = n / 10 | 0) count[n % 10]++; | |
| let result = 0; | |
| for (let d = 9; d >= 0; d--) | |
| while (count[d]--) result = result * 10 + d; | |
| return result; | |
| } | |
| const minNum2 = num => { | |
| const count = new Uint8Array(10); | |
| for (let n = num; n > 0; n = n / 10 | 0) count[n % 10]++; | |
| let result = 0; | |
| for (let d = 0; d <= 9; d++) | |
| while (count[d]--) result = result * 10 + d; | |
| return result; | |
| } | |
| const seen = {}; | |
| const MAX_STEP = 10_000; | |
| const findKaprekar = (i, _min, _max, prev, step) => { | |
| let diff = _max-_min; | |
| while (diff) { | |
| if (diff in seen) { | |
| break; | |
| } | |
| if (step > MAX_STEP) { | |
| seen[diff] = -i; | |
| break; | |
| } | |
| if (prev===diff) { | |
| seen[diff] = i; | |
| break; | |
| } | |
| prev = diff; | |
| diff = maxNum2(diff)-minNum2(diff); | |
| step++; | |
| } | |
| } | |
| for (let i = rangeStart; i< rangeEnd; i++) { | |
| findKaprekar(i, minNum2(i), maxNum2(i), null, 1); | |
| if ((i % 1_000_000)===0){ | |
| console.log(i.toLocaleString(), seen) | |
| } | |
| } | |
| console.log(seen); | |
| parentPort.postMessage({ | |
| workerId, | |
| seen | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
'495': 102,
'6_174': 1001,
'549_945': 100155,
'631_764': 100147,
'63_317_664': 10001567,
'97_508_421': 10000024,
'554_999_445': 333333888,
'864_197_532': 333333401,
}