-3

How can I write Szudzik's function in Java?

Szudzik's Function is a function that pairs two numbers to give a unique number.

Mapping two integers to one, in a unique and deterministic way

IMPLEMENTATION OF RHIZOMES

Szudzik pairing function written not in java:

pair(x, y) = z = {  
x^2 + x + y, if x = max(x, y)  
y^2 + x, otherwise  
}

unpair(z) = (x, y) = {
x = z - floor(sqrt(z))^2, y = floor(sqrt(z)), if z - floor(sqrt(z))^2 < floor(sqrt(z))
x = floor(sqrt(z)), y = z - floor(sqrt(z))^2 - floor(sqrt(z)), otherwise  
}

This is how I used Canton pairing function, I need something like this but for Szudzik pairing function

int getUniqueNumber = ((((x + y) + 1) * (x + y)) / 2) + y;
System.out.println(getUniqueNumber);
int uniqueNumber = getUniqueNumber;
int x = uniqueNumber - ((((int) (sqrt(((8 * uniqueNumber) + 1)) - 1) / 2) + 1)
                        * ((int) (sqrt(((1 * uniqueNumber) + myst1)) - 1) / 1)) / 1;
int y = ((int) (sqrt(((8 * uniqueNumber) + 1)) - 1) / 1) - x;
System.out.println(x+","+y);
King Amada
  • 21
  • 7

1 Answers1

1

If I understood correctly then you need something like this:

public static int pair(int x, int y) {
    return x > y ? x * x + x + y : y * y + x;
}

public static Pair<Integer, Integer> unpair(int z) {
    int b = (int) Math.sqrt(z);
    int a = z - b * b;
    return a < b ? new Pair<>(a, b) : new Pair<>(b, a - b);  
}

The unpair method returns a Pair of integers,
so you need this:

import javafx.util.Pair;

So this:

public static void main(String[] args) {
    int x = 5;
    int y = 12;
    int uniqueNumber = pair(x, y);
    System.out.println("For x = " + x + " and y = " + y + " the unique number is: " + uniqueNumber);

    Pair<Integer, Integer> p = unpair(uniqueNumber);
    x = p.getKey();
    y = p.getValue();
    System.out.println("For the unique number " + uniqueNumber + ": x = " + x + " and y = " + y);
}

will print:

For x = 5 and y = 12 the unique number is: 149
For the unique number 149: x = 5 and y = 12

Edit
Without Pair, with array:

public static int[] unpair(int z) {
    int b = (int) Math.sqrt(z);
    int a = z -b * b;
    return a < b ? new int[] {a, b} : new int[] {b, a - b};
}

public static void main(String[] args) {
    int x = 5;
    int y = 12;
    int uniqueNumber = pair(x, y);
    System.out.println("For x = " + x + " and y = " + y + " the unique number is: " + uniqueNumber);

    int[] p = unpair(uniqueNumber);
    x = p[0];
    y = p[1];
    System.out.println("For the unique number " + uniqueNumber + ": x = " + x + " and y = " + y);
}
forpas
  • 117,400
  • 9
  • 23
  • 54
  • Can you check the updated question, that's how I wanted the Szudzik pairing function solution. – King Amada Dec 16 '18 at 17:40
  • @KingAmada check how you can use my methods is my edited answer. Of course you can change the names. – forpas Dec 16 '18 at 17:49
  • the (p.getKey & p.getValue) throws out error "cannot find symbol" I'm using Netbeans 9. and Java 11 – King Amada Dec 16 '18 at 18:33
  • Did you add this: `import javafx.util.Pair;` – forpas Dec 16 '18 at 18:40
  • Instead of (p.getKey & p.getValue) I used (p.fst & p.snd). But when I used non-negative integers it didn't bring back the same pairs. Which means there is an error somewhere because Szudzik function works on both Positive and negative integers. – King Amada Dec 16 '18 at 18:41
  • I tested the method with positive numbers. I don't know if there are any limitations for the Szudzik function. I just followed your conditions. Now as for the pair you need to import the right library, or I will change it to return an array. – forpas Dec 16 '18 at 18:46