Skip to content

Instantly share code, notes, and snippets.

@jorimvanhove
Last active February 20, 2023 09:08
Show Gist options
  • Save jorimvanhove/cc706a8b9dd4d98350476d28c1ccf9c4 to your computer and use it in GitHub Desktop.
Save jorimvanhove/cc706a8b9dd4d98350476d28c1ccf9c4 to your computer and use it in GitHub Desktop.
Function that calculates a commutative cantor pair. The lowest input parameter is assigned to x, the highest to y.
// C# 10
namespace Helpers;
public static class CantorPairCalculator
{
/// <summary>Calculates the cantor pair of input parameters a & b
/// Commutative operation, the lowest input parameter is assigned to x, the highest to y
/// </summary>
/// <returns>Returns a cantor pair integer</returns>
public static int CalculateCommutativeCantorPair(this int a, int b)
{
int x;
int y;
if (a <= b)
{
x = a;
y = b;
}
else
{
x = b;
y = a;
}
return (((x + y) * (x + y + 1)) / 2) + y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment