Skip to content

Instantly share code, notes, and snippets.

@Kittoes0124
Last active December 22, 2019 21:07
Show Gist options
  • Save Kittoes0124/d9a45a7e551929660747d294fe3d5195 to your computer and use it in GitHub Desktop.
Save Kittoes0124/d9a45a7e551929660747d294fe3d5195 to your computer and use it in GitHub Desktop.
public static class FigurateNumbers {
public static ulong NthHexagonal(ulong value) => checked((2UL * NthSquare(value)) - value);
public static ulong NthOblong(ulong value) => checked(value * (value + 1UL));
public static ulong NthPentagonal(ulong value) => checked(((3UL * NthSquare(value)) - value) >> 1);
public static ulong NthPhiCubic(ulong value) => unchecked((1UL + CubeRoot(value)) >> 1);
public static ulong NthPhiSquare(ulong value) => unchecked((1UL + SquareRoot(value)) >> 1);
public static ulong NthTriangular(ulong value) => checked(((value * (value + 1UL))) >> 1);
}
public static bool IsApproximatelyEqual(Func<ulong, ulong> x, Func<ulong, ulong> y, Func<ulong> randomNumberGenerator) {
var result = true;
for (var i = 0UL; (i < 9223372036854775808UL); i = NextPowerOfTwo(i)) { // monotonic testing
var cube = NextCube(i);
var square = NextSquare(i);
for (var j = 1UL; (j < 11UL); ++j) {
var cubeA = (cube + j);
var cubeS = (cube - j);
var identityA = (i + j);
var identityS = (i - j);
var squareA = (square + j);
var squareS = (square - j);
if (((x(cubeA) != y(cubeA)) || (x(cubeS) != y(cubeS))) || ((x(identityA) != y(identityA)) || (x(identityS) != y(identityS))) || ((x(squareA) != y(squareA)) || (x(squareS) != y(squareS)))) {
result = false;
}
}
}
for (var i = 0UL; (i < 100000UL); ++i) { // midpoint testing
var tempA = ((ulong.MaxValue >> 1) + i);
var tempS = ((ulong.MaxValue >> 1) - i);
if ((x(tempA) != y(tempA)) || (x(tempS) != y(tempS))) {
result = false;
}
}
for (var i = 0UL; (i < 100000000UL); ++i) { // uniformly distributed testing
var temp = randomNumberGenerator();
if (x(temp) != y(temp)) {
result = false;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment