nodef / extra-number

A number is a mathematical object used to count, measure, and label.
https://www.npmjs.com/package/extra-number
MIT License
2 stars 0 forks source link

too much useless loops #11

Closed c0ncentus closed 1 year ago

c0ncentus commented 1 year ago

at least use sqrt for no waste of execution time exemple : 1000000 => 1x100000 => 2x 50000 => until 1000x1000;

rather make 1000000 loops, you have 1000; not a great deal but much much save compute memory.

my suggestion

export function properDivisors(x: number): number[] {
    const num = Math.abs(x);
    const a:number[] = [];
    const LIMIT = Math.sqrt(num)
    for (let i=1; i<LIMIT; i++){
if (num % i===0){
 a.push(i);
a.push(num/i) // if have rest of 0, the division is not a float number
}
    return uniq(a);
  }

image https://en.wikipedia.org/wiki/Prime_number relative to testing prime number because for testing prime number, we have to find if there are no devider except 1 and himself .

I'm sure for big number there are few tricks to make it faster : /

PostScriptum: I see that many function on fetch factor of, not have so clever algo and put a tons of loops for litterally nothing.

wolfram77 commented 1 year ago

@c0ncentus If you are only looking for prime factors, check primeFactors, or have a look at its source code.