2014-03-25 11:19:58 -04:00
|
|
|
/*
|
|
|
|
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 Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
namespace MPIR.Tests.HugeIntTests
|
|
|
|
{
|
|
|
|
[TestClass]
|
|
|
|
public class Math
|
|
|
|
{
|
2014-03-26 15:31:54 -04:00
|
|
|
#region Exponentiation
|
2014-03-25 11:19:58 -04:00
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntPowerModHugeInt()
|
2014-03-25 11:19:58 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("2835698356928736487698769283645293409781234"))
|
|
|
|
using (var b = new HugeInt("3"))
|
|
|
|
using (var c = new HugeInt("9786459872639458729387304958673243509870923452745892673402935742456"))
|
|
|
|
{
|
|
|
|
a.Value = a.PowerMod(b, c);
|
|
|
|
Assert.AreEqual("5346078446724436806099093819990997994355321605000165187447171753448", a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntPowerModLimb()
|
2014-03-25 11:19:58 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("2835698356928736487698769283645293409781234"))
|
|
|
|
using (var c = new HugeInt("9786459872639458729387304958673243509870923452745892673402935742456"))
|
|
|
|
{
|
|
|
|
a.Value = a.PowerMod(3, c);
|
|
|
|
Assert.AreEqual("5346078446724436806099093819990997994355321605000165187447171753448", a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 14:52:39 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntPowerLimb()
|
2014-03-25 14:52:39 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("9785412309485720938412983404349"))
|
|
|
|
{
|
|
|
|
a.Value = a ^ 4;
|
|
|
|
Assert.AreEqual("9168884832199547717402442404668238841010784738902226284286664833331445628675177089723224507720724521226586825967635414667601", a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 15:31:54 -04:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Roots
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntRoot()
|
2014-03-26 15:31:54 -04:00
|
|
|
{
|
|
|
|
var n = "8984948281360922385394772450147012613851354303";
|
|
|
|
using (var a = new HugeInt(n))
|
|
|
|
using (var b = new HugeInt())
|
|
|
|
using (var c = new HugeInt())
|
|
|
|
{
|
|
|
|
a.Value = a * a * a;
|
|
|
|
|
|
|
|
b.Value = a.Root(3);
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
|
|
|
|
bool exact = false;
|
|
|
|
b.Value = a.Root(3).SettingExactTo(x => exact = x);
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
Assert.IsTrue(exact);
|
|
|
|
|
|
|
|
b.Value = (a + 123).Root(3).SettingExactTo(x => exact = x);
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
Assert.IsFalse(exact);
|
|
|
|
|
|
|
|
b.Value = (a + 123).Root(3).SavingRemainderTo(c);
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
Assert.AreEqual("123", c.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntSquareRoot()
|
2014-03-26 15:31:54 -04:00
|
|
|
{
|
|
|
|
var n = "8984948281360922385394772450147012613851354303";
|
|
|
|
using (var a = new HugeInt(n))
|
|
|
|
using (var b = new HugeInt())
|
|
|
|
using (var c = new HugeInt())
|
|
|
|
{
|
|
|
|
a.Value = a * a;
|
|
|
|
|
|
|
|
b.Value = a.SquareRoot();
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
|
|
|
|
b.SetTo(0);
|
|
|
|
b.Value = (a + 123).SquareRoot().SavingRemainderTo(c);
|
|
|
|
Assert.AreEqual(n, b.ToString());
|
|
|
|
Assert.AreEqual("123", c.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 17:24:23 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntIsPerfectPower()
|
2014-03-26 17:24:23 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("8984948281360922385394772450147012613851354303"))
|
|
|
|
using (var b = new HugeInt())
|
|
|
|
{
|
|
|
|
b.Value = a * a * a;
|
|
|
|
Assert.IsTrue(b.IsPerfectPower());
|
|
|
|
b.Value = a * a;
|
|
|
|
Assert.IsTrue(b.IsPerfectPower());
|
|
|
|
b.Value = a * a * a + 1;
|
|
|
|
Assert.IsFalse(b.IsPerfectPower());
|
|
|
|
b.Value = a * a + 1;
|
|
|
|
Assert.IsFalse(b.IsPerfectPower());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntIsPerfectSquare()
|
2014-03-26 17:24:23 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("8984948281360922385394772450147012613851354303"))
|
|
|
|
using (var b = new HugeInt())
|
|
|
|
{
|
|
|
|
b.Value = a * a * a;
|
|
|
|
Assert.IsFalse(b.IsPerfectSquare());
|
|
|
|
b.Value = a * a;
|
|
|
|
Assert.IsTrue(b.IsPerfectSquare());
|
|
|
|
b.Value = a * a * a + 1;
|
|
|
|
Assert.IsFalse(b.IsPerfectSquare());
|
|
|
|
b.Value = a * a + 1;
|
|
|
|
Assert.IsFalse(b.IsPerfectSquare());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 14:52:39 -04:00
|
|
|
#endregion
|
|
|
|
//more tests coming here
|
2014-03-25 11:19:58 -04:00
|
|
|
}
|
|
|
|
}
|