167 lines
5.2 KiB
C#
167 lines
5.2 KiB
C#
/*
|
|
Copyright 2014 Alex Dyachenko
|
|
|
|
This file is part of the MPIR Library.
|
|
|
|
The MPIR Library is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
by the Free Software Foundation; either version 3 of the License, or (at
|
|
your option) any later version.
|
|
|
|
The MPIR Library is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
|
|
*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Linq;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace MPIR.Tests.HugeIntTests
|
|
{
|
|
[TestClass]
|
|
public class NumberTheoretic
|
|
{
|
|
[TestMethod]
|
|
public void IsProbablePrime()
|
|
{
|
|
using (var a = new HugeInt("622288097498926496141095869268883999563096063592498055290461"))
|
|
using (var random = MpirRandom.Default())
|
|
{
|
|
Assert.IsTrue(a.IsProbablePrime(random, 10, 0));
|
|
a.Value = a * 2;
|
|
Assert.IsFalse(a.IsProbablePrime(random, 10, 0));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void IsLikelyPrime()
|
|
{
|
|
using (var a = new HugeInt("622288097498926496141095869268883999563096063592498055290461"))
|
|
using (var random = MpirRandom.Default())
|
|
{
|
|
Assert.IsTrue(a.IsLikelyPrime(random, 0));
|
|
a.Value = a * 2;
|
|
Assert.IsFalse(a.IsLikelyPrime(random, 0));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void NextPrimeCandidate()
|
|
{
|
|
using (var a = new HugeInt("622288097498926496141095869268883999563096063592498055290460"))
|
|
using (var random = MpirRandom.Default())
|
|
{
|
|
Assert.AreEqual(a + 1, a.NextPrimeCandidate(random));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Gcd()
|
|
{
|
|
using (var a = new HugeInt("29927402397991286489627837734179186385188296382227"))
|
|
using (var b = new HugeInt())
|
|
using (var c = new HugeInt())
|
|
{
|
|
b.Value = a * 39;
|
|
c.Value = a * 41;
|
|
Assert.AreEqual(a, b.Gcd(c));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GcdExt()
|
|
{
|
|
using (var a = HugeInt.FromUlong(12))
|
|
using (var b = HugeInt.FromUlong(21))
|
|
using (var g = new HugeInt())
|
|
using (var s = new HugeInt())
|
|
using (var t = new HugeInt())
|
|
{
|
|
g.Value = a.Gcd(b).SavingDiophantineMultipliersTo(s, t);
|
|
Assert.AreEqual("3", g.ToString());
|
|
Assert.AreEqual("2", s.ToString());
|
|
Assert.AreEqual("-1", t.ToString());
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GcdExt1()
|
|
{
|
|
using (var a = HugeInt.FromUlong(12))
|
|
using (var b = HugeInt.FromUlong(21))
|
|
using (var g = new HugeInt())
|
|
using (var s = new HugeInt())
|
|
{
|
|
g.Value = a.Gcd(b).SavingDiophantineMultipliersTo(s, null);
|
|
Assert.AreEqual("3", g.ToString());
|
|
Assert.AreEqual("2", s.ToString());
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GcdExt2()
|
|
{
|
|
using (var a = HugeInt.FromUlong(12))
|
|
using (var b = HugeInt.FromUlong(21))
|
|
using (var g = new HugeInt())
|
|
using (var t = new HugeInt())
|
|
{
|
|
g.Value = a.Gcd(b).SavingDiophantineMultipliersTo(null, t);
|
|
Assert.AreEqual("3", g.ToString());
|
|
Assert.AreEqual("-1", t.ToString());
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GcdLimb()
|
|
{
|
|
using (var a = new HugeInt("29927402397991286489627837734179186385188296382227"))
|
|
{
|
|
a.Value = a * 127;
|
|
Assert.AreEqual(127UL, a.Gcd(254));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Lcm()
|
|
{
|
|
using (var a = new HugeInt("29927402397991286489627837734179186385188296382227"))
|
|
{
|
|
var b = a * 39;
|
|
var c = a * 41;
|
|
Assert.AreEqual(a * 39 * 41, b.Lcm(c));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void LcmLimb()
|
|
{
|
|
using (var a = new HugeInt("29927402397991286489627837734179186385188296382227"))
|
|
{
|
|
var b = a * 41;
|
|
Assert.AreEqual(a * 39 * 41, b.Lcm(39));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Invert()
|
|
{
|
|
using (var a = new HugeInt("29927402397991286489627837734179186385188296382227"))
|
|
using (var m = new HugeInt("622288097498926496141095869268883999563096063592498055290461"))
|
|
using (var r = new HugeInt())
|
|
{
|
|
r.Value = a.Invert(m);
|
|
Assert.IsTrue(r > 0);
|
|
Assert.IsTrue(1 == (r * a) % m);
|
|
}
|
|
}
|
|
}
|
|
}
|