[英]A bijective function from N*N*N to N
Can anyone help me in finding a bijective mathematical function from N * N * N → N that takes three parameters x, y, and z and returns a number n?谁能帮我从 N * N * N → N 中找到一个双射数学函数,它接受三个参数 x、y 和 z 并返回一个数字 n?
I would like to know the function f and its inverse f' in a way that if I have n I will be able to determine x, y, z by applying f'(n).我想知道函数 f 及其反函数 f',如果我有 n,我将能够通过应用 f'(n) 来确定 x、y、z。
Suppose g is a bijection from N × N to N and let g -1 be its inverse. 假设g是从N×N到N的双射,并且令g -1是它的逆。 Then we can define f in terms of g as follows.
然后我们可以用g来定义f,如下所示。
f(x, y, z) = g(g(x, y), z) = n f(x,y,z)= g(g(x,y),z)= n
f -1 (n) = (x, y, z) where g -1 (n) = (w, z) and g -1 (w) = (x, y) f -1 (n)=(x,y,z)其中g -1 (n)=(w,z)和g -1 (w)=(x,y)
We now have the much simpler problem of defining g. 我们现在有一个更简单的定义g的问题。
g(x, y) = (x + y)(x + y + 1) / 2 + y = n g(x,y)=(x + y)(x + y + 1)/ 2 + y = n
g -1 (n) = (x, y) where m = ⌊(2n) 1/2 ⌋ and exactly one of the following two conditions hold. g -1 (n)=(x,y)其中m =⌊(2n) 1/ 2⌋,并且以下两个条件中的恰好之一成立。
x + y = m and y = n - m(m + 1) / 2 x + y = m且y = n-m(m + 1)/ 2
x + y = m - 1 and y = n - m(m - 1) / 2 x + y = m-1,y = n-m(m-1)/ 2
def f(x, y, z):
return g(g(x, y), z)
def f_inv(n):
w, z = g_inv(n)
x, y = g_inv(w)
return (x, y, z)
def g(x, y):
return (x + y) * (x + y + 1) / 2 + y
def g_inv(n):
m = math.floor(math.sqrt(2 * n))
while True:
y = n - m * (m + 1) / 2
if y >= 0:
break
m -= 1
x = m - y
return x, y
F(x,y,z) = 2^x*3^y*5^z
In fact you can choose any distinct set of prime numbers. 实际上,您可以选择任何不同的素数集。 And inverse is simply by factorizing to corresponding prime factors.
而逆则只是通过分解为相应的素因子。
你的函数不是满射的,让 p 是一个素数,我们在 N 中找不到任何 x,y,z 使得 p=2^x 3^y 5^z ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.