title: Postponed Sieve algorithm with start logic
body: <p>Based on <a href="https://stackoverflow.com/a/10733621/849891">this Python answer</a> by <a href="https://stackoverflow.com/users/849891/will-ness">Will Ness</a>, I've been using a JavaScript adaptation for the postponed sieve algorithm from that answer:</p>
  <pre class="lang-js prettyprint-override"><code>function * primes() {
      yield 2;
      yield 3;
      yield 5;
      yield 7;
      const sieve = new Map();
      const ps = primes();
      ps.next() &amp;&amp; ps.next();
      for (let p = 3, i = 9; true; i += 2) {
          let s = sieve.get(i);
          if (s !== undefined) {
              sieve.delete(i);
          } else if (i &lt; p * p) {
              yield i;
              continue;
          } else {
              s = 2 * p;
              p = ps.next().value;
          }
          let k = i + s;
          while (sieve.has(k)) k += s;
          sieve.set(k, s);
      }
  }
  </code></pre>
  <p>But now that I need to add <code>start</code> point to it, I am struggling to wrap my head around it, for the logic here isn't straightforward.</p>
  <p>When <code>start</code> is a prime, I need it to be the first value. And when <code>start</code> is not a prime, I need the sequence to start with the first prime after <code>start</code>.</p>
  <p>Will Ness suggested in one of the comments:</p>
  <blockquote>
  <p>You would have to come up with the valid sieve dictionary for the start point. It's easy to get the needed primes - just run this algo up to <code>sqrt(start)</code>, then you need to find each core prime's multiple just above (or is it just below?) the start, while minding the duplicates.</p>
  </blockquote>
  <p>Wielding this into reality however is not that simple (or at least for me) :|</p>
  <p>Can anybody help with updating this algorithm for such <code>*primes(start)</code> implementation (preferably in JavaScript as above)?</p>
  <pre class="lang-js prettyprint-override"><code>function * primes(start) {
  
    // how to amend it to support 'start' logic???
  
  }
  </code></pre>
  <h2>Conclusion</h2>
  <p>Following the excellent answer by Will Ness, I decided to share the final code I used, through a public library - <a href="https://github.com/vitaly-t/primes-generator" rel="nofollow noreferrer">primes-generator</a>. All main algorithms can be found in <a href="https://github.com/vitaly-t/primes-generator/blob/main/src/sieve.ts" rel="nofollow noreferrer">src/sieve.ts</a>.</p>