IsProbablePrime/IsLikelyPrime methods
This commit is contained in:
parent
77204b819e
commit
8b547cc544
@ -118,6 +118,9 @@
|
||||
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeIntTests\Math.cs">
|
||||
<Link>HugeIntTests\Math.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeIntTests\NumberTheoretic.cs">
|
||||
<Link>HugeIntTests\NumberTheoretic.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeIntTests\XmlCommentsTests.cs">
|
||||
<Link>IntegrationTests\XmlCommentsTests.cs</Link>
|
||||
</Compile>
|
||||
|
55
mpir.net/mpir.net-tests/HugeIntTests/NumberTheoretic.cs
Normal file
55
mpir.net/mpir.net-tests/HugeIntTests/NumberTheoretic.cs
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "Stdafx.h"
|
||||
#include "Random.h"
|
||||
|
||||
using namespace System::Runtime::InteropServices;
|
||||
using namespace System::Text;
|
||||
@ -642,4 +643,18 @@ namespace MPIR
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region number-theoretic
|
||||
|
||||
bool HugeInt::IsProbablePrime(MpirRandom^ random, int probability, mpir_ui pretested)
|
||||
{
|
||||
return mpz_probable_prime_p(_value, random->_value, probability, pretested) != 0;
|
||||
}
|
||||
|
||||
bool HugeInt::IsLikelyPrime(MpirRandom^ random, mpir_ui pretested)
|
||||
{
|
||||
return mpz_likely_prime_p(_value, random->_value, pretested) != 0;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
};
|
@ -263,6 +263,7 @@ private ref class Mpir##name##Expression : base
|
||||
|
||||
namespace MPIR
|
||||
{
|
||||
ref class MpirRandom;
|
||||
ref class HugeInt;
|
||||
ref class MpirDivideExpression;
|
||||
ref class MpirDivideUiExpression;
|
||||
@ -1993,6 +1994,38 @@ namespace MPIR
|
||||
size_t GetLimb(mp_size_t index) { return mpz_getlimbn(_value, index); }
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region number-theoretic
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the number is a probable prime with the chance of error being at most 1 in 2^<paramref name="probability"/>.
|
||||
/// <para>This function does some trial divisions to speed up the average case, then some probabilistic
|
||||
/// primality tests to achieve the desired level of error.
|
||||
/// </para>This function interface is preliminary and may change in the future.
|
||||
/// </summary>
|
||||
/// <param name="random">Random number generator to use for probabilistic primality tests</param>
|
||||
/// <param name="probability">Defines the maximum allowed probability of a false positive.
|
||||
/// <para>The odds of a composite number being reported as a probable prime are at most 1 in 2^probability</para></param>
|
||||
/// <param name="pretested">Used to inform the function that trial division up to div has already been performed,
|
||||
/// and so the number is known to have NO divisors <= pretested.
|
||||
/// <para>Use 0 to inform the function that no trial division has been done.</para></param>
|
||||
/// <returns>true if the number is probably prime, or false if it is definitely composite.</returns>
|
||||
bool IsProbablePrime(MpirRandom^ random, int probability, mpir_ui pretested);
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether the number is likely a prime, i.e. you can consider it a prime for practical purposes.
|
||||
/// <para>This function does some trial divisions to speed up the average case, then some probabilistic primality tests.
|
||||
/// </para>The term "likely" refers to the fact that the number will not have small factors.
|
||||
/// <para>This function interface is preliminary and may change in the future.
|
||||
/// </para></summary>
|
||||
/// <param name="random">Random number generator to use for probabilistic primality tests</param>
|
||||
/// <param name="pretested">Used to inform the function that trial division up to div has already been performed,
|
||||
/// and so the number is known to have NO divisors <= pretested.
|
||||
/// <para>Use 0 to inform the function that no trial division has been done.</para></param>
|
||||
/// <returns>true if the number is likely prime, or false if it is definitely composite.</returns>
|
||||
bool IsLikelyPrime(MpirRandom^ random, mpir_ui pretested);
|
||||
|
||||
#pragma endregion
|
||||
};
|
||||
|
||||
#pragma endregion
|
||||
|
Loading…
Reference in New Issue
Block a user