Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #801 #803

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ch3/3.5/prime-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const { Worker, isMainThread, parentPort, workerData } = require('worker_threads
const min = 2;
let primes = [];

function findPrimes(start, range) {
function findPrimes(start, end) {
let isPrime = true;
const end = start + range;
for (let i = start; i < end; i++) {
for (let i = start; i <= end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i % j === 0) {
isPrime = false;
Expand All @@ -28,11 +27,11 @@ if (isMainThread) {
let start = min;
console.time('prime');
for (let i = 0; i < threadCount - 1; i++) {
const wStart = start;
threads.add(new Worker(__filename, { workerData: { start: wStart, range } }));
const end = start + range - 1;
threads.add(new Worker(__filename, { workerData: { start, range: end } }));
start += range;
}
threads.add(new Worker(__filename, { workerData: { start, range: max - start } }));
threads.add(new Worker(__filename, { workerData: { start, range: max } }));
for (let worker of threads) {
worker.on('error', (err) => {
throw err;
Expand Down
5 changes: 2 additions & 3 deletions ch3/3.5/prime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ const min = 2;
const max = 10000000;
const primes = [];

function findPrimes(start, range) {
function findPrimes(start, end) {
let isPrime = true;
const end = start + range;
for (let i = start; i < end; i++) {
for (let i = start; i <= end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i % j === 0) {
isPrime = false;
Expand Down