Created
June 22, 2012 16:31
-
-
Save JonasGroeger/2973869 to your computer and use it in GitHub Desktop.
Cantorsche Paarungsfunktion
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
module Cantor ( | |
c, | |
cx, | |
cy, | |
d, | |
h, | |
c_umkehr, | |
findmax) | |
where | |
-- Calculates the c function from cantor | |
c x y = y + 0.5 * (x+y+1) * (x+y) | |
-- Calculates "backwards" the x value of given c | |
cx z = _q(z) - cy(z) - 1 | |
-- Calculates "backwards" the y value of given c | |
cy z = z - _f(_q(z)) | |
c_umkehr z = (cx z, cy z) | |
-- Added this method for convenience, as x isn't | |
-- important. Tt's always length(list) - 1 | |
d x list = _d list | |
h list = 1 + (c (fromIntegral(index)) (d index list)) | |
where index = length(list) - 1 | |
-- Finding the value for q in a fast way that is calculatable | |
-- by hand even for large numbers. | |
findmax z = floor (sqrt (2*z)) | |
{----------------------------------------------- | |
------------ HELPER FUNCTIONS BELOW ------------ | |
-----------------------------------------------} | |
_f x = 0.5 * x * (x-1) | |
_q x = maximum [v | v <- [1..x], _f v <= x] | |
_d list | |
| (length(list) == 1) = head list | |
| otherwise = c (head list) (_d (tail list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unter Linux mit der Konsole starten:
Funktion c
Funktionen cx und cy
cx 28
und
cy 28
Schon haben wir die x und y Werte von 28, nämlich cx=7 und cy=0
Funktion h
Funktion d
wobei die erste Zahl der Index ist und die Liste die Argumente für d.
Da der Index nur die Anzahl der Parameter-1 ist, kann man auch
verwenden und so den Index weglassen.