public static List <Tuple <int, int> > GetPrimeFactors(this long number) { List <Tuple <int, int> > returnFactorisation = new List <Tuple <int, int> >(); foreach (int currentPrime in Sequences.CachedPrimes().TakeWhile(p => p <= number)) { int timesDivisible = 0; while (number % currentPrime == 0) { timesDivisible++; number = number / currentPrime; } if (timesDivisible > 0) { returnFactorisation.Add(Tuple.Create(currentPrime, timesDivisible)); } } return(returnFactorisation); }
public static List <KeyValuePair <int, int> > PrimeFactors(int value) { var primeFactors = new List <KeyValuePair <int, int> >(); foreach (var p in Sequences.PrimesUpTo(1, value)) { var pair = new KeyValuePair <int, int>(p, 0); while (value % p == 0) { pair = new KeyValuePair <int, int>(p, pair.Value + 1); value = value / p; } if (pair.Value > 0) { primeFactors.Add(pair); } } return(primeFactors); }