Problem #86 says:
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the opposite corner. By travelling on the surfaces of the room the shortest “straight line” distance from S to F is 10 and the path is shown on the diagram.
However, there are up to three “shortest” path candidates for any given cuboid and the shortest route is not always integer.
By considering all cuboid rooms with integer dimensions, up to a maximum size of M by M by M, there are exactly 2060 cuboids for which the shortest distance is integer when M=100, and this is the least value of M for which the number of solutions first exceeds two thousand; the number of solutions is 1975 when M=99.
Find the least value of M such that the number of solutions first exceeds one million.
I thought this problem was going to be more interesting than it turned out being. Basically we’re looking for triplets (a,b,M) where 1<=a<=b<=M that make the equation \((a+b)^{2}+n^{2}\) a perfect square. We just do this for every value M=1-?? and count the number of solutions for that M value until the sum of all previous solution counts equals one million. Basically we take M = 3 which has a solution count of 3 and is the first none zero solution count and M = 4 with a solution count of 1; We add the solutions counts for M = 1-4 which are {0,0,2,1} and check to see if the sum of that series is greater than one million. If that sum isn’t one million we calculate the next value of M and add that to the series and check the series sum again; wash, rinse, repeat.
This problem became a lot less interesting when I found the formula for the shortest point and googled it and came back with two OEIS sequences (A143714,A143715) which made the solution trivial to write because it showed the method of building on the previous M values. On the other hand trivial to program solutions mean a lot less time spent frustrated trying to get the program to work.
Solution provided in c#/mono and runs in ~30ish seconds. Not the fastest but it works.
using System;
using System.Linq;
using System.Collections.Generic;
namespace Problem86
{
class MainClass
{
public static List<int> A143714 = new List<int>();
public static void Main (string[] args)
{
A143714.Add(0);
A143714.Add(0);
while(A143714.Sum() < 1000000)
{
int n = A143714.Count+1;
int count = 0;
for (int b = 1;b<=n;b++)
{
for (int a = 1;a<=b;a++)
{
if (IsPerfectSquare(((a+b)*(a+b))+(n*n)))
count++;
}
}
A143714.Add(count);
}
Console.WriteLine(A143714.Count);
}
public static bool IsPerfectSquare(int input)
{
var sqrt = Math.Sqrt((double)input);
return Math.Ceiling(sqrt) == Math.Floor(sqrt);
}
}
}
With this solution I have only 11 problems left between 1-100 and only 39 left until I hit level 4!