public void TestFactoredRationals(IPrimeFactorizer factorizer) { FactoredRational factoredRational; factoredRational = new FactoredRational(Rational.Nan, factorizer); Assert.AreEqual(FactoredRational.Nan, factoredRational); factoredRational = new FactoredRational(Rational.Infinity, factorizer); Assert.AreEqual(FactoredRational.Infinity, factoredRational); factoredRational = new FactoredRational(Rational.Zero, factorizer); Assert.AreEqual(FactoredRational.Zero, factoredRational); // // Test simplifying to 1 // for (int i = 1; i < 30; i++) { factoredRational = new FactoredRational(new Rational(i, (UInt32)i), factorizer); Assert.AreEqual((Single)1, factoredRational.ConvertToSingle()); Assert.AreEqual((Double)1, factoredRational.ConvertToDouble()); } // // TODO: Add test logic here // }
public FactoredRational(Int32 value, IPrimeFactorizer factorizer) { FactoredInt32 factoredInt32 = FactoredInt32.Create(value, factorizer); this.numerator = value; this.denominator = 1; this.primeFactors = factoredInt32.primeFactors; }
public CachingFactorizer(IPrimeFactorizer underlyingFactorizer) { if (underlyingFactorizer == null) { throw new ArgumentNullException("underlyingFactorizer"); } this.underlyingFactorizer = underlyingFactorizer; }
public FactoredRational(Rational rational, IPrimeFactorizer factorizer) { if (rational.numerator == 0 || rational.denominator == 0) { this.numerator = rational.numerator; this.denominator = rational.denominator; this.primeFactors = PoweredPrime.None; } else { // // Take the absolute value of the rational // PoweredPrime[] denominatorFactors = factorizer.PrimeFactorize(rational.denominator); if (denominatorFactors == null) { throw factorizer.UnableToFactorize(rational.denominator); } UInt32 numeratorAbsoluteValue = (UInt32)((rational.numerator >= 0) ? rational.numerator : -rational.numerator); this.primeFactors = factorizer.Divide(numeratorAbsoluteValue, denominatorFactors); if (this.primeFactors == null) { throw factorizer.UnableToDivide(numeratorAbsoluteValue, denominatorFactors); } // Get new numerator and denominator this.numerator = 1; this.denominator = 1; for (int i = 0; i < this.primeFactors.Length; i++) { PoweredPrime factor = this.primeFactors[i]; if (factor.power > 0) { numerator *= (Int32)factor.value; // I don't need to check overflow here because these factors came from an Int32 } else { denominator *= factor.value; // I don't need to check overflow here because these factors came from a UInt32 } } if (rational.numerator < 0) { numerator = -numerator; } } }
public static FactoredUInt32 Create(UInt32 value, IPrimeFactorizer factorizer) { if (value <= 3) { return(FirstFour[value]); } PoweredPrime[] factors = factorizer.PrimeFactorize(value); if (factors == null) { throw factorizer.UnableToFactorize(value); } return(new FactoredUInt32(value, factors)); }
void TestFactorize(IPrimeFactorizer factorizer, UInt32 value, params PoweredPrime[] expectedPrimeFactors) { PoweredPrime[] calculatedPrimeFactors = factorizer.PrimeFactorize(value); builder.Length = 0; calculatedPrimeFactors.SerializeArray(builder); Console.WriteLine("Value '{0}' PrimeFactors: {1}", value, builder.ToString()); String sosDiff = expectedPrimeFactors.Diff(calculatedPrimeFactors); if (sosDiff != null) { Assert.Fail("Diff {0}", sosDiff); } }
public static FactoredInt32 Create(Int32 value, IPrimeFactorizer factorizer) { if (value >= 0 && value <= 3) { return(FirstFour[value]); } UInt32 absoluteValue = (UInt32)((value >= 0) ? value : -value); PoweredPrime[] factors = factorizer.PrimeFactorize(absoluteValue); if (factors == null) { throw factorizer.UnableToFactorize(absoluteValue); } return(new FactoredInt32(value, factors)); }
public void TestFactoredIntegers(IPrimeFactorizer factorizer) { FactoredInt32 factoredInt32; FactoredUInt32 factoredUInt32; factoredInt32 = FactoredInt32.Create(0, factorizer); Assert.AreEqual(FactoredInt32.Zero, factoredInt32); factoredUInt32 = FactoredUInt32.Create(0, factorizer); Assert.AreEqual(FactoredUInt32.Zero, factoredUInt32); factoredInt32 = FactoredInt32.Create(1, factorizer); Assert.AreEqual(FactoredInt32.One, factoredInt32); factoredUInt32 = FactoredUInt32.Create(1, factorizer); Assert.AreEqual(FactoredUInt32.One, factoredUInt32); }
void TestFactorizer(IPrimeFactorizer factorizer) { Assert.AreEqual(PoweredPrime.None, factorizer.PrimeFactorize(0)); Assert.AreEqual(PoweredPrime.None, factorizer.PrimeFactorize(1)); TestFactorize(factorizer, 2, new PoweredPrime(2, 1)); TestFactorize(factorizer, 3, new PoweredPrime(3, 1)); TestFactorize(factorizer, 4, new PoweredPrime(2, 2)); TestFactorize(factorizer, 5, new PoweredPrime(5, 1)); TestFactorize(factorizer, 6, new PoweredPrime(2, 1), new PoweredPrime(3, 1)); TestFactorize(factorizer, 7, new PoweredPrime(7, 1)); TestFactorize(factorizer, 8, new PoweredPrime(2, 3)); TestFactorize(factorizer, 9, new PoweredPrime(3, 2)); TestFactorize(factorizer, 10, new PoweredPrime(2, 1), new PoweredPrime(5, 1)); TestFactorize(factorizer, 11, new PoweredPrime(11, 1)); TestFactorize(factorizer, 12, new PoweredPrime(2, 2), new PoweredPrime(3, 1)); TestFactorize(factorizer, 13, new PoweredPrime(13, 1)); TestFactorize(factorizer, 14, new PoweredPrime(2, 1), new PoweredPrime(7, 1)); TestFactorize(factorizer, 15, new PoweredPrime(3, 1), new PoweredPrime(5, 1)); TestFactorize(factorizer, 16, new PoweredPrime(2, 4)); TestFactorize(factorizer, 17, new PoweredPrime(17, 1)); TestFactorize(factorizer, 18, new PoweredPrime(2, 1), new PoweredPrime(3, 2)); TestFactorize(factorizer, 19, new PoweredPrime(19, 1)); }
void GetMaxPrime(IPrimeFactorizer factorizer, UInt32 printEvery) { UInt32 i = 0; while (true) { try { PoweredPrime[] primeFactors = factorizer.PrimeFactorize(i); if (i % printEvery == 0) { builder.Length = 0; primeFactors.SerializeArray(builder); Console.WriteLine("Value '{0}' PrimeFactors: {1}", i, builder.ToString()); } } catch (Exception e) { Console.WriteLine("At {0}, got exception {1}", i, e); throw; } i++; } }
public static InvalidOperationException UnableToFactorize(this IPrimeFactorizer factorizer, UInt32 value) { return(new InvalidOperationException(String.Format("{0} was not able to factorize {1}", factorizer.GetType().Name, value))); }
public static InvalidOperationException UnableToDivide(this IPrimeFactorizer factorizer, UInt32 value, PoweredPrime[] factors) { return(new InvalidOperationException(String.Format("{0} was not able to divide {1} byte factors {2}", factorizer.GetType().Name, value, factors.SerializeObject()))); }