Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

number theory: 64 bit integer overloads, additional unit tests

  • Loading branch information...
commit e70a36ef1e6e87497c2e4d26afbbbd3e5198ffab 1 parent 5e9db3b
@cdrnet cdrnet authored
View
72 src/app/MathNet.Iridium/Library/NumberTheory/IntegerTheory.cs
@@ -42,40 +42,56 @@ namespace MathNet.Numerics.NumberTheory
public static class IntegerTheory
{
/// <summary>
- /// Find out whether the provided integer is an even number.
+ /// Find out whether the provided 32 bit integer is an even number.
/// </summary>
/// <returns>True if and only if it is an even number.</returns>
public static
bool
- IsEven(
- int number
- )
+ IsEven(int number)
{
return (number & 0x1) == 0x0;
}
/// <summary>
- /// Find out whether the provided integer is an odd number.
+ /// Find out whether the provided 64 bit integer is an even number.
+ /// </summary>
+ /// <returns>True if and only if it is an even number.</returns>
+ public static
+ bool
+ IsEven(long number)
+ {
+ return (number & 0x1) == 0x0;
+ }
+
+ /// <summary>
+ /// Find out whether the provided 32 bit integer is an odd number.
/// </summary>
/// <returns>True if and only if it is an odd number.</returns>
public static
bool
- IsOdd(
- int number
- )
+ IsOdd(int number)
{
return (number & 0x1) == 0x1;
}
/// <summary>
- /// Find out whether the provided integer is a perfect square, i.e. a square of an integer.
+ /// Find out whether the provided 64 bit integer is an odd number.
+ /// </summary>
+ /// <returns>True if and only if it is an odd number.</returns>
+ public static
+ bool
+ IsOdd(long number)
+ {
+ return (number & 0x1) == 0x1;
+ }
+
+ /// <summary>
+ /// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>
/// <returns>True if and only if it is a perfect square.</returns>
public static
bool
- IsPerfectSquare(
- int number
- )
+ IsPerfectSquare(int number)
{
if(number < 0)
{
@@ -88,10 +104,6 @@ int number
return false; // return immediately in 6 cases out of 16.
}
- /*
- TODO (ruegg, 2009-01-02): Test/benchmark wherher the inverse test performs better:
- lastHexDigit != 2 && lastHexDigit != 3 && lastHexDigit != 5 && lastHexDigit != 6 && lastHexDigit != 7 && lastHexDigit != 8
- */
if(lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
{
int t = (int)Math.Floor(Math.Sqrt(number) + 0.5);
@@ -100,5 +112,33 @@ int number
return false;
}
+
+ /// <summary>
+ /// Find out whether the provided 64 bit integer is a perfect square, i.e. a square of an integer.
+ /// </summary>
+ /// <returns>True if and only if it is a perfect square.</returns>
+ public static
+ bool
+ IsPerfectSquare(long number)
+ {
+ if(number < 0)
+ {
+ return false;
+ }
+
+ int lastHexDigit = (int)(number & 0xF);
+ if(lastHexDigit > 9)
+ {
+ return false; // return immediately in 6 cases out of 16.
+ }
+
+ if(lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
+ {
+ long t = (long)Math.Floor(Math.Sqrt(number) + 0.5);
+ return (t * t) == number;
+ }
+
+ return false;
+ }
}
}
View
54 src/test/MathNet.Iridium.Test/NumberTheoryTest.cs
@@ -41,7 +41,7 @@ namespace Iridium.Test
public class NumberTheoryTest
{
[Test]
- public void TestEvenOdd()
+ public void TestEvenOdd32()
{
Assert.IsTrue(IntegerTheory.IsEven(0), "0 is even");
Assert.IsFalse(IntegerTheory.IsOdd(0), "0 is not odd");
@@ -60,7 +60,26 @@ public void TestEvenOdd()
}
[Test]
- public void TestIsPerfectSquare()
+ public void TestEvenOdd64()
+ {
+ Assert.IsTrue(IntegerTheory.IsEven((long)0), "0 is even");
+ Assert.IsFalse(IntegerTheory.IsOdd((long)0), "0 is not odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven((long)1), "1 is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd((long)1), "1 is odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven((long)-1), "-1 is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd((long)-1), "-1 is odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven(Int64.MaxValue), "Int64.Max is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd(Int64.MaxValue), "Int64.Max is odd");
+
+ Assert.IsTrue(IntegerTheory.IsEven(Int64.MinValue), "Int64.Min is even");
+ Assert.IsFalse(IntegerTheory.IsOdd(Int64.MinValue), "Int64.Min is not odd");
+ }
+
+ [Test]
+ public void TestIsPerfectSquare32()
{
// Test all known suares
int lastRadix = (int)Math.Floor(Math.Sqrt(Int32.MaxValue));
@@ -87,5 +106,36 @@ public void TestIsPerfectSquare()
Assert.IsTrue(IntegerTheory.IsPerfectSquare(0), "0 (+)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(-1), "-1 (-)");
}
+
+ [Test]
+ public void TestIsPerfectSquare64()
+ {
+ // Test all known suares
+ for(int i = 0; i < 32; i++)
+ {
+ long t = ((long)1) << i;
+ Assert.IsTrue(IntegerTheory.IsPerfectSquare(t * t), t.ToString() + "^2 (+)");
+ }
+
+ // Test 1-offset from all known squares
+ for(int i = 1; i < 32; i++)
+ {
+ long t = ((long)1) << i;
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) - 1), t.ToString() + "^2-1 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) + 1), t.ToString() + "^2+1 (-)");
+ }
+
+ // Selected Cases
+ Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1000000000000000000), "1000000000000000000 (+)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)1000000000000000001), "1000000000000000001 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999999), "999999999999999999 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999993), "999999999999999993 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-4), "-4 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MinValue), "Int32.MinValue (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MaxValue), "Int32.MaxValue (-)");
+ Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1), "1 (+)");
+ Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)0), "0 (+)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-1), "-1 (-)");
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.