If you want to work on Number Theory problems, or Diophatine Equations, you need a math package that allows you to use large integers (MATLAB, e.g.). Optionally, you need to encode your ideas as programs and test your theories the old-fashioned way, with Fortran or some other language. I prefer C++ (it’s the language I have been using for 20 years now). To that end, I want to show you how to use Visual Studio 2010 to build a C++ program to work on large integers. As a bit of a bonus, I’ll also show how to do the same thing in C#.
For fun, we’ll write a small program to verify an old Pell’s equation (which is a kind of Diophantine Equation) where the answer has been given by by Brahmagupta and then Euler. The equation is:
61x2 + 1 = y2
Our goal is not to study the math of Pell’s equations, but to plug a large integer package into your Visual Studio. So I will show you all the steps you need to follow to get this done in a Windows environment using C++ (Section I) and C# (Section II), and then we’ll see about Euler’s answer (3,119,882,982,860,264,401).
Section I — C++
Step 1: Download the large integer math package. We will go to Brian Gladmann’s site, and read about the packages, then go to the Multiple Precision Integers and Rationals page and download the latest source tarball (which happens to be version 2.2.1 at the time of this writing). The version won’t change the code, you’ll be able to use the same C++ code no matter which version you get. At the risk of adding confusion: MPIR is a Windows-port of the GMP (GNU Multiple Precision Arithmetic Library).
Step 2: Decompress the package (I use WinRAR for tarball files, but find something on the internet — something free! — and install it on your machine).
Decompress the file to some location on your computer, let’s say c:\projects\ (for the sake of of discussion).
Step 3: Launch Visual Studio 2010, and load the build file you just decompressed. In our sample case, the 2010 build file would be:
c:\projects\mpir-2.2.1\build.vc10\mpir.sln
Step 4: Build the dll_mpir_gc project.

The results (in the sample case of c:\projects\) are in this directory:
c:\projects\mpir-2.2.1\build.vc10\dll\Win32\Release
All the .h files you need, the libs, and the DLL are there.
Step 5: Now we get to use the results! So, create a new blank console application project and name it something related to the problem we are going to solve. We are going to use the results of the above steps which means you’ll place c:\projects\mpir-2.2.1\build.vc10\dll\Win32\Release in your include directory and in your library linking path. Next, you’ll link in the library mpir.lib. The header file you will include is gmpxx.h. For more about this file and the C++ objects in it, go to the gmp site.
Step 6: Create a C++ file in your project with the following code,
#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>
using namespace std;
void main(int argc, char *argv[])
{
mpz_class answer_a = 226153980;
mpz_class answer_b = 1766319049;
if ((61*answer_a*answer_a + 1) == (answer_b * answer_b))
cout << "Euler was right!\n";
else
cout << "Oooops. My math package has failed me.\n";
}
Notice that instead of using int or long, we now use mpz_class. Everything else is as expected.
Other large integer tools exist (OpenSSL comes with a BigNum package that is quite nice -- you'll have to build it with the help of some tools I provide in an early article). Of course, the list of tools is impressive, my goal here was to show you a free C++ method that fits within your life easily (if you are a Visual Studio users).
Part II -- C#
Using Big Numbers in C# (.NET 4) is as easy as adding a reference to System.Numerics and then using BigNumber.
But there is also a BigRational which you can use by hooking C# into F#. To use BigRational, follow these steps:
Step 1: Install the F# PowerPack tool which has the big number class we'll be using. Get PowerPack here:
http://fsharppowerpack.codeplex.com/
Download this package and install it on your system,

You need to install F# PowerPack
To be fair, this will make my comments here "dated", for I predict that Microsoft will move the F# code we care about out of PowerPack and put it somewhere else (probably in the F# core). Anyway, for now it is in PowerPack.
Step 2: Add a reference to the PowerPack into your C# project.

Right click on "References", and browse to the PowerPack (in your Program Files\FSharpPowerPack directory:

Drill down into the bin directory, and select FSharp.PowerPack.dll

Step 3: Use FSharp.Math in your program. Now that you have added a reference to the PowerPack, you can use Microsoft.FSharp.Math via the "using" clause in your C# program as follows:

Writing a C# program to use BigRational or BigInteger:
Having followed the above steps, our C# program looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FSharp.Math; // gets us BigRational
using System.Numerics; // gets us BigInteger
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BigInteger bi_a = 226153980;
BigInteger bi_b = 1766319049; // one way to do it...
bi_b = BigInteger.Parse("1766319049"); // or do it this way
BigInteger sq = bi_b * bi_b;
if ((61 * bi_a * bi_a + 1) == (bi_b * bi_b))
{
Console.WriteLine("Euler was right!");
Console.WriteLine(sq.ToString());
}
BigRational br_a = BigRational.FromInt(226153980);
BigRational br_b = BigRational.FromInt(1766319049);
BigRational br_answer_a = BigRational.FromInt(61) * br_a * br_a + BigRational.FromInt(1);
BigRational br_answer_b = br_b * br_b;
if (br_answer_a.Equals(br_answer_b))
{
Console.WriteLine("\nEuler was right again!");
Console.WriteLine(br_answer_b.ToString());
}
}
}
}
You should see the output that Euler was right. If you don't something went wrong in the code or in one of the steps.
In Part 2 and Part 3 I compare C++ to C# in terms of speed. I also explore more about the historical background behind our sample problem (61x2 + 1 = y2).