Last active
February 20, 2023 09:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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