mathematische Kniffe

Mit der Cantorschen Paarungsfunktion lassen sich zwei Zahlen zu einer zusammenführen und später wieder trennen:

http://de.wikipedia.org/wiki/Cantorsche_Paarungsfunktion

 


vb.net Code:

Public Class CantorBerechnung

‚diese Berechnung erstellt aus zwei Zahlen eine (contorberechnen(Zahl1,Zahl2) und zerlegt diese wieder ComputeX(Cantorzahl); ComputeY(Cantorzahl)
Public Function cantorberechnen(x As Long, y As Long)
cantorberechnen = (x + y) * (x + y + 1) / 2 + y
End Function

Public Function computeX(kantorzahlZ As Long)
Dim j As Long
j = CLng(Math.Floor(Math.Sqrt(0.25 + 2 * kantorzahlZ) – 0.5))
computeX = j – (kantorzahlZ – j * (j + 1) / 2)
End Function

Public Function computeY(z As Long) As Long
Dim j As Long
j = CLng(Math.Floor(Math.Sqrt(0.25 + 2 * z) – 0.5))
computeY = CLng(z – j * (j + 1) / 2) ‚ist stets ganzzahlig
End Function
End Class


Kommentare

Schreibe einen Kommentar