I'm new to C# and have run into a problem with the following code (I have the target framework as 4.5 and I've added a reference to System.Numerics):
using System;
using System.Numerics;
namespace Test
{
class Program
{
static BigInteger Gcd(BigInteger x, BigInteger y)
{
Console.WriteLine("GCD {0}, {1}", x, y);
if (x < y) return Gcd(y, x);
if (x % y == 0) return y;
return Gcd(y, x % y);
}
static void Main(string[] args)
{
BigInteger a = 13394673;
BigInteger b = 53578691;
Gcd(a, b);
}
}
}
When the release build is started with debugging (F5 in Visual Studio - and a break-point at the end of program so I can see the output), I get the following output:
GCD 13394673, 53578691
GCD 53578691, 13394673
GCD 13394673, 13394672
GCD 13394672, 1
However, when the release build is started without debugging (Ctrl-F5), I get the following:
GCD 13394673, 53578691
GCD 53578691, 53578691
Strangely if I add a Console.ReadLine() at the end of the program it works as expected!
Any ideas what is causing this? Thanks.