Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

IRID-192 - Basic Integer Number Theory

  • Loading branch information...
commit 538a00243f80ece8c47d3ee6136d4b88064645c1 1 parent 35c60cc
@cdrnet cdrnet authored
View
1  src/app/MathNet.Iridium/Library/Iridium.csproj
@@ -101,6 +101,7 @@
<Compile Include="LinearAlgebra\IMatrix.cs" />
<Compile Include="LinearAlgebra\IVector.cs" />
<Compile Include="LinearAlgebra\Vector.cs" />
+ <Compile Include="NumberTheory\IntegerTheory.cs" />
<Compile Include="Properties\LocalStrings.cs" />
<Compile Include="Sorting.cs" />
<Compile Include="OnDemandComputation.cs" />
View
104 src/app/MathNet.Iridium/Library/NumberTheory/IntegerTheory.cs
@@ -0,0 +1,104 @@
+//-----------------------------------------------------------------------
+// <copyright file="IntegerTheory.cs" company="Math.NET Project">
+// Copyright (c) 2002-2009, Christoph Rüegg.
+// All Right Reserved.
+// </copyright>
+// <author>
+// Christoph Rüegg, http://christoph.ruegg.name
+// </author>
+// <product>
+// Math.NET Iridium, part of the Math.NET Project.
+// http://mathnet.opensourcedotnet.info
+// </product>
+// <license type="opensource" name="LGPL" version="2 or later">
+// This program 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 2 of the License, or
+// any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// </license>
+// <contribution>
+// StackOverflow.com, John D. Cook
+// </contribution>
+//-----------------------------------------------------------------------
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace MathNet.Numerics.NumberTheory
+{
+ /// <summary>
+ /// Number Theory for Integers
+ /// </summary>
+ public static class IntegerTheory
+ {
+ /// <summary>
+ /// Find out whether the provided integer is an even number.
+ /// </summary>
+ /// <returns>True if and only if it is an even number.</returns>
+ public static
+ bool
+ IsEven(
+ int number
+ )
+ {
+ return (number & 0x1) == 0x0;
+ }
+
+ /// <summary>
+ /// Find out whether the provided integer is an odd number.
+ /// </summary>
+ /// <returns>True if and only if it is an odd number.</returns>
+ public static
+ bool
+ 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.
+ /// </summary>
+ /// <returns>True if and only if it is a perfect square.</returns>
+ public static
+ bool
+ IsPerfectSquare(
+ int number
+ )
+ {
+ if(number < 0)
+ {
+ return false;
+ }
+
+ int lastHexDigit = number & 0xF;
+ if(lastHexDigit > 9)
+ {
+ 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);
+ return (t * t) == number;
+ }
+
+ return false;
+ }
+ }
+}
View
1  src/test/MathNet.Iridium.Test/Iridium.Test.csproj
@@ -45,6 +45,7 @@
<Compile Include="ComplexMatrixTest.cs" />
<Compile Include="DistributionTest.cs" />
<Compile Include="InterpolationTest.cs" />
+ <Compile Include="NumberTheoryTest.cs" />
<Compile Include="NumericAssert.cs" />
<Compile Include="SetTest.cs" />
<Compile Include="ComplexTest.cs" />
View
80 src/test/MathNet.Iridium.Test/NumberTheoryTest.cs
@@ -0,0 +1,80 @@
+//-----------------------------------------------------------------------
+// <copyright file="NumberTheoryTest.cs" company="Math.NET Project">
+// Copyright (c) 2002-2009, Christoph Rüegg.
+// All Right Reserved.
+// </copyright>
+// <author>
+// Christoph Rüegg, http://christoph.ruegg.name
+// </author>
+// <product>
+// Math.NET Iridium, part of the Math.NET Project.
+// http://mathnet.opensourcedotnet.info
+// </product>
+// <license type="opensource" name="LGPL" version="2 or later">
+// This program 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 2 of the License, or
+// any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// </license>
+//-----------------------------------------------------------------------
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+using NUnit.Framework;
+
+using MathNet.Numerics.NumberTheory;
+
+namespace Iridium.Test
+{
+ [TestFixture]
+ public class NumberTheoryTest
+ {
+ [Test]
+ public void TestEvenOdd()
+ {
+ Assert.IsTrue(IntegerTheory.IsEven(0), "0 is even");
+ Assert.IsFalse(IntegerTheory.IsOdd(0), "0 is not odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven(1), "1 is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd(1), "1 is odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven(-1), "-1 is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd(-1), "-1 is odd");
+
+ Assert.IsFalse(IntegerTheory.IsEven(Int32.MaxValue), "Int32.Max is not even");
+ Assert.IsTrue(IntegerTheory.IsOdd(Int32.MaxValue), "Int32.Max is odd");
+
+ Assert.IsTrue(IntegerTheory.IsEven(Int32.MinValue), "Int32.Min is even");
+ Assert.IsFalse(IntegerTheory.IsOdd(Int32.MinValue), "Int32.Min is not odd");
+ }
+
+ [Test]
+ public void TestIsPerfectSquare()
+ {
+ // Test all known suares
+ int lastRadix = (int)Math.Floor(Math.Sqrt(Int32.MaxValue));
+ for(int i = 0; i <= lastRadix; i++)
+ {
+ Assert.IsTrue(IntegerTheory.IsPerfectSquare(i * i), i.ToString() + "^2 (+)");
+ }
+
+ // Test 1-offset from all known squares
+ for(int i = 2; i <= lastRadix; i++)
+ {
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) - 1), i.ToString() + "^2-1 (-)");
+ Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) + 1), i.ToString() + "^2+1 (-)");
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.