public long Calc(int n) { var oneFraction = new Fraction { Numerator = 1, Denominator = 1 }; var hele = new Fraction { Numerator = n, Denominator = 1 }; var prim = Primes.GetPrimeFactorsInNumber(primeFactors, primeDict, n); var primes = prim.Distinct(); List <Fraction> fractionListe = new List <Fraction>(primes.Count()); if (primes.Count() == 1) { int antall = prim.Count(); for (int i = 1; i <= antall; i++) { var fr2 = new Fraction { Numerator = prim[0], Denominator = 1 }; fractionListe.Add(fr2); } var fr = new Fraction { Numerator = -1, Denominator = prim[0] }; var sumFracttion = Fraction.AddFraction(oneFraction, fr); fractionListe.Add(sumFracttion); } else { foreach (var prime in primes) { var fr = new Fraction { Numerator = -1, Denominator = prime }; var sumFracttion = Fraction.AddFraction(oneFraction, fr); fractionListe.Add(sumFracttion); } fractionListe.Add(hele); } long productDenominator = 1; long productNumerator = 1; fractionListe.ForEach(f => productDenominator *= f.Denominator); fractionListe.ForEach(f => productNumerator *= f.Numerator); long p = productNumerator / productDenominator; return(p); }
public static List <long> GetAllUniqueDivisorsIn(long number) { List <long> allDivisorsIn = new List <long> { 1 }; List <long> PrimeFactorsInNumber = Primes.GetPrimeFactorsInNumber(number).ToList(); // allDivisorsIn.Add(1); for (int offset = 0; offset < PrimeFactorsInNumber.Count; offset++) { int to = allDivisorsIn.Count; //multi with everything found so fare... for (int i = 0; i < to; i++) { allDivisorsIn.Add(allDivisorsIn[i] * PrimeFactorsInNumber[offset]); } } return(allDivisorsIn.Distinct().OrderBy(e => e).ToList());; }
public static Fraction ReduceFraction(Fraction fraction) { if (fraction.Denominator % fraction.Numerator == 0) { return(new Fraction { Numerator = 1, Denominator = fraction.Denominator / fraction.Numerator }); } List <long> PrimeFactorsInDenominator = Primes.GetPrimeFactorsInNumber(fraction.Denominator).ToList(); List <long> PrimeFactorsInNumerator = Primes.GetPrimeFactorsInNumber(fraction.Numerator).ToList(); List <long> ReducedDenominator = new List <long>(); List <long> ReducedNumerator = new List <long>(); var iterator1 = PrimeFactorsInDenominator.GetEnumerator(); var iterator2 = PrimeFactorsInNumerator.GetEnumerator(); bool more1 = iterator1.MoveNext(); bool more2 = iterator2.MoveNext(); while (more1 && more2) { if (iterator1.Current == iterator2.Current) { more1 = iterator1.MoveNext(); more2 = iterator2.MoveNext(); continue; } if (iterator1.Current < iterator2.Current) { ReducedDenominator.Add(iterator1.Current); more1 = iterator1.MoveNext(); continue; } if (iterator1.Current > iterator2.Current) { ReducedNumerator.Add(iterator2.Current); more2 = iterator2.MoveNext(); continue; } } while (more1) { ReducedDenominator.Add(iterator1.Current); more1 = iterator1.MoveNext(); } while (more2) { ReducedNumerator.Add(iterator2.Current); more2 = iterator2.MoveNext(); } long Denominator = 1; for (int index = 0; index < ReducedDenominator.Count; index++) { Denominator *= ReducedDenominator[index]; } long Numerators = 1; for (int index = 0; index < ReducedNumerator.Count; index++) { Numerators *= ReducedNumerator[index]; } return(new Fraction { Numerator = Numerators, Denominator = Denominator }); }
public static Fraction Product(List <Fraction> liste) { List <long> Numerators = liste.Select(e => e.Numerator).ToList(); List <long> PrimeFactorsInNumber1 = Primes.GetPrimeFactorsInNumber(liste[0].Denominator).ToList(); // List<long> CommonPrimeFactors = new List<long>(); List <long> combined = new List <long>(); for (int index = 1; index < liste.Count; index++) { combined = new List <long>(); List <long> PrimeFactorsInNumber2 = Primes.GetPrimeFactorsInNumber(liste[index].Denominator).ToList(); var iterator1 = PrimeFactorsInNumber1.GetEnumerator(); var iterator2 = PrimeFactorsInNumber2.GetEnumerator(); bool more1 = iterator1.MoveNext(); bool more2 = iterator2.MoveNext(); while (more1 && more2) { if (iterator1.Current == iterator2.Current) { combined.Add(iterator1.Current); combined.Add(iterator2.Current); more1 = iterator1.MoveNext(); more2 = iterator2.MoveNext(); continue; } if (iterator1.Current < iterator2.Current) { combined.Add(iterator1.Current); more1 = iterator1.MoveNext(); continue; } if (iterator1.Current > iterator2.Current) { combined.Add(iterator2.Current); more2 = iterator2.MoveNext(); continue; } } while (more1) { combined.Add(iterator1.Current); more1 = iterator1.MoveNext(); } while (more2) { combined.Add(iterator2.Current); more2 = iterator2.MoveNext(); } PrimeFactorsInNumber1 = combined; } // long LowestCommonTerms = 1; for (int index = 0; index < combined.Count; index++) { LowestCommonTerms *= combined[index]; } long CommonNumerators = 1; for (int index = 0; index < Numerators.Count; index++) { CommonNumerators *= Numerators[index]; } var fraction = new Fraction { Numerator = CommonNumerators, Denominator = LowestCommonTerms }; fraction = Fraction.ReduceFraction(fraction); return(fraction); }
/* simple slow */ public int CalcSlow(int n) { var primes = Primes.GetPrimeFactorsInNumber(n).Distinct(); return(Enumerable.Range(1, n - 1).Where(e => !primes.Any(p => e % p == 0)).Count()); }
public Totient(int size) { primeFactors = Primes.GetPrimeFactorsBelowNumber((long)size); primeDict = primeFactors.ToDictionary(e => e); }