V
V
vitali10242018-04-03 13:50:58
JavaScript
vitali1024, 2018-04-03 13:50:58

How can the code be optimized to find prime numbers in a given range?

Actually, I did a challenge on codewars, the essence of which is as follows: you need to find the first pair of prime numbers in a given range with a given interval of prime numbers. I have implemented an algorithm that passes all tests, but ran into a runtime problem (my "top" code takes more than 12 seconds to complete). And I don't know how to optimize it yet. I immediately apologize for the pain in my eyes when viewing my code (I have been doing JS for the 4th week). And now to the point, the function, respectively, has 3 parameters:

/*
g (integer >= 2) интервал между простыми числами
m (integer > 2) начало заданного интервала (включительно)
n (integer >= m) конец заданного интервала (включительно)
*/
function gap(g, m, n) {
 // Your code
}

Execution example:
gap(2, 3, 50); // [3, 5]
gap(2, 5, 5); // null
gap(2, 5, 7); // [5, 7]
gap(4, 130, 200); // [163, 167]
gap(6,100,110); // null

Link to this task on codewars: https://www.codewars.com/kata/gap-in-primes/train/...
My implementation of the algorithm:
See the Pen Prime gap by Vitalii ( @vitalii1024 ) on CodePen .
It seems to me that the problem lies in my inefficient implementation of the algorithm for finding prime numbers. I would be grateful for the guidance of more experienced JS developers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pugachev, 2018-04-03
@Stqs

standard algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question