Project Euler 7: 10001 Prime in Functional Programming (FP) I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Project Euler:


  By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can
  see that the 6th prime is 13.
  
  What is the 10 001st prime number?


This is my solution:

(function () {
  'use strict';

  function* checkForPrime(n) {
    let m = Math.floor(Math.sqrt(n) + 1);
    while (true) {
      if (n &lt; 2) {
        yield false;
        return;
      }
      if (m &lt; 2 || n === 2 || n === 3) {
        yield true;
        return;
      }
      if (n % m === 0) {
        yield false;
        return;
      }
      m--;
    }
  }

  const isPrime = n =&gt; checkForPrime(n)
    .next();

  function* searchForPrimeNumbers(max) {
    let primeCounter = 0;
    let newPrimeNumber = 1;
    let runningNumber = 1;
    while (true) {
      if (primeCounter === max) {
        yield newPrimeNumber;
        return newPrimeNumber;
      }
      if (isPrime(runningNumber)
        .value) {
        newPrimeNumber = runningNumber;
        primeCounter = primeCounter + 1;
      }
      runningNumber++;
    }
  }

  const finalsolution = searchForPrimeNumbers(1e4 + 1);

  const findPrimeTill10001 = () =&gt; {
    return finalsolution.next()
      .value ? finalsolution.next()
      .value : findPrimeTill10001();
  };

  const solution = findPrimeTill10001();
  console.log(solution);
})();


As you can see, it is not consistent with the idea of FP. First I wanted to write it using recursion but I hit the stack limit. Therefore I used generators (and loops). That was the only solution I could come up with that resembles FP. 

Any suggestions how to write in FP without any additional FP-Library (i.e. in pure JS only) is much appreciated.
You shouldn't be doing things in a loop that don't depend on things changing in the loop. n &lt; 2, n === 2, and  n === 3 all depend on n, which is constant throughout your loop. Also, if you store your primes, you just have to loop through the primes until you get to sqrt(n), rather than checking every number. There are further tricks to speed up the algorithm, such as checking n only if n mod 60 is in [7,11,13,17,23,29,31,37,41,43,47,49,53,59]; that reduces the number of n to check by more than 3/4. 

Is tail recursion an option? 

How large is your stack size? You can break recursion into subrecursion. For instance, suppose you have a function that recursively finds the nth to n+100th primes. Then you can have a recursive function than, when asked to find k primes, calls itself on k-101, then appends the k-100 to kth primes. You can then add another layer, and have something that does 10 primes at a time. Do this in binary rather than base 10, and you'll need 14 layers to get 10001 primes.