整数排列函数的算法
我想写一些函数如下
y = f(x) 和另一个函数,
x = g(y) 充当可逆函数,其中
y = f(g(y)) 并且其中 x 和 y 是置换整数。
对于 0 到 10 之间的整数范围内的非常简单的示例,它看起来像这样:
0->1
1->2
2->3
...
9->10
10->0
但这是最简单的方法,即加 1 并通过减 1 进行反转。
我想要一个更复杂的算法,可以执行以下操作,
234927773->4299
34->33928830
850033->23234243423
但是可以通过转换获得相反的结果
可以通过存储唯一整数对的巨大表来获得解决方案,但这将是不正确的。这必须是一个函数。
I want to write some functions as follows
y = f(x) and another function,
x = g(y) that acts as a reversible, where
y = f(g(y)) and where x and y are permutated integers.
For very simple example in the range of integers in 0 to 10 it would look like this:
0->1
1->2
2->3
...
9->10
10->0
but this is the simplest method by adding 1 and reversing by subtracting 1.
I want to have a more sofisticated algorithm that can do the following,
234927773->4299
34->33928830
850033->23234243423
but the reverse can be obtained by conversion
The solution could be obtained with a huge table storing pairs of unique integers but this will not be correct. This must be a function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以只进行异或运算。
虽然不是我的专业领域,但我认为密码学应该为您的问题提供一些有价值的答案。
You could just XOR.
Though not my area of expertise, I think that cryptography should provide some valuable answers to your question.
如果您的排列域是 2 的幂,则可以使用任何分组密码:“f”是使用特定密钥进行加密,“g”是使用相同密钥进行解密。如果您的域不是 2 的幂,您可能仍然可以使用分组密码:请参阅 本文。
If the domain of your permutation is a power of 2, you can use any block cipher: 'f' is encryption with a specific key, and 'g' is decryption with the same key. If your domain is not a power of 2, you can probably still use a block cipher: see this article.
您可以使用多项式插值方法以一种方式对函数进行插值,然后进行反向插值以找到反函数。
以下是 MATLAB 中的一些示例代码:
它构建一个除差表并根据牛顿插值计算函数。
然后,如果您的点集是 x 和 y(作为相同长度的向量,其中 x(i) 与 y(i) 匹配,则值 n 处的前向插值函数将是 Eval(x, Coef(x , y), n) ,反向插值函数将是
Eval(y, Coef(y, x), n)
,具体取决于您的语言,可能有更简洁的方法。这样做,但这与数学有关。
这是我的数值方法课程中使用的教科书的摘录:Google 图书链接
You could use polynomial interpolation methods to interpolate a function one way, then do reverse interpolation to find the inverse function.
Here is some example code in MATLAB:
It builds a Divided Difference table and evaluates a function based on Newtons Interpolation.
Then if your sets of points are x, and y (as vectors of the same length, where x(i) matches to y(i), your forward interpolation function at value n would be
Eval(x, Coef(x, y), n)
and the reverse interpolation function would beEval(y, Coef(y, x), n)
.Depending on your language, there are probably much cleaner ways to do this, but this gets down and dirty with the maths.
Here is an excerpt from the Text Book which is used in my Numerical Methods class: Google Book Link