Skip to content

Instantly share code, notes, and snippets.

@yoavst
Created November 1, 2017 20:38
Show Gist options
  • Save yoavst/bc81028c2c8f8f4993e10d39f7c83844 to your computer and use it in GitHub Desktop.
Save yoavst/bc81028c2c8f8f4993e10d39f7c83844 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
namespace CSharp
{
class FindFrog
{
static void Main(string[] args)
{
long now = DateTimeOffset.Now.ToUnixTimeSeconds();
ulong sum = Enumerable.Range(-100, 201).AsParallel().Select(mx =>
{
ulong totalSum = 0;
for (int my = -100; my <= 100; my++)
{
for (int dx = -100; dx <= 100; dx++)
{
for (int dy = -100; dy <= 100; dy++)
{
totalSum += pair(pair(z2n(mx), z2n(my)), pair(z2n(dx), z2n(dy)));
}
}
}
return totalSum;
}).Aggregate((x, y) => x + y);
Console.WriteLine("sec: {0}", DateTimeOffset.Now.ToUnixTimeSeconds() - now);
Console.WriteLine("sum: {0}", sum);
Console.WriteLine("avg: {0}", sum / 1632240801);
}
static void simple()
{
long now = DateTimeOffset.Now.ToUnixTimeSeconds();
ulong sum = 0;
for (int mx = -100; mx <= 100; mx++)
{
for (int my = -100; my <= 100; my++)
{
for (int dx = -100; dx <= 100; dx++)
{
for (int dy = -100; dy <= 100; dy++)
{
sum += pair(pair(z2n(mx), z2n(my)), pair(z2n(dx), z2n(dy)));
}
}
}
}
Console.WriteLine("sec: {0}", DateTimeOffset.Now.ToUnixTimeSeconds() - now);
Console.WriteLine("sum: {0}", sum);
Console.WriteLine("avg: {0}", sum / 1632240801);
}
static ulong pair(ulong a, ulong b)
{
return ((a + b) * (a + b + 1) >> 1) + a;
}
static ulong z2n(int z)
{
return (ulong)(z >= 0 ? z << 1 : -1 - (z << 1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment