Primes

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Generates primes up to a given number, using the Sieve of Eratosthenes.

    const primes = (num: number) => {
      let arr: Array<number> = Array.from({ length: num - 1 }).map((x, i) => i + 2),
        sqroot: number = Math.floor(Math.sqrt(num)),
        numsTillSqroot: Array<number> = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);
      numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));
      return arr;
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.