Prime factors

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Finds the prime factors of a given number using the trial division algorithm.

    const primeFactors = (n: number) => {
      let a: Array<number> = [],
        f = 2;
      while (n > 1) {
        if (n % f === 0) {
          a.push(f);
          n /= f;
        } else {
          f++;
        }
      }
      return a;
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.