Szudzik配对算法存在重复映射问题:与Cantor配对对比验证
Szudzik配对算法出现重复映射问题
我尝试使用Szudzik配对算法替代Cantor配对算法,却发现该算法存在重复映射的情况。
示例代码
function szudzikPair(x, y) { return (x >= y ? (x * x) + x + y : (y * y) + x); } console.log(szudzikPair( 127382263, 10902761)); console.log(szudzikPair( 127382263, 10902759)); console.log(szudzikPair( 127382263, 10902760)); function cantorPair(x, y) { return (0.5 * (x + y) * (x + y + 1)) + y; } console.log(cantorPair( 127382263, 10902761)); console.log(cantorPair( 127382263, 10902759)); console.log(cantorPair( 127382263, 10902760));
调用Szudzik配对算法处理上述三组不同的整数对,返回结果均为16226241065286192;而Cantor配对算法对相同整数对返回的结果各不相同,我原本预期Szudzik算法能像Cantor算法一样实现唯一映射。
实际输出
16226241065286192 16226241065286192 16226241065286192 9561374011385560 9561373734815512 9561373873100536
内容的提问来源于stack exchange,提问作者Barbara Epifanova
相关产品推荐
相关产品推荐

