primeFactors function
Returns the unique prime factors of n
.
For example, 120 returns [2, 3, 5]
.
Implementation
List<int> primeFactors(int n) {
bool newp = true;
final a = <int>[];
for (int i = 0, p = 2;;) {
if (p * p > n) break;
if (n % p != 0) {
i += 1;
p = primes.getPrime(i);
newp = true;
} else {
if (newp) {
a.add(p);
newp = false;
}
n ~/= p;
}
}
if (n != 1 && (a.isEmpty || a.last != n)) a.add(n);
return a;
}