简体   繁体   English

汉明数字使用自定义函数而不是素数

[英]Hamming number using custom functions instead of prime

Hamming Problem is a famous problem which basically generates all integers which prime factors are {2,3,5} only. 汉明问题是一个着名的问题,它基本上产生所有整数,其素数因子只有{2,3,5}。 (And it can be extended to any set of prime factors I think) (它可以扩展到我认为的任何一组素因子)

To find the n-th Hamming number, there is a clever O(N) constructing algorithm by Dijkstra, which pseudo code is as following: 为了找到第n个汉明数,Dijkstra有一个聪明的O(N)构造算法,伪代码如下:

List<int> H
int i=0,j=0,k=0, n=10 // find the 10-th hamming number
H.add(1)
for(i=0 to 10)
   int next = min(2*H[i], 3*H[j], 5*H[k])
   H.add(next)
   if(next == 2*H[i]) ++i
   if(next == 3*H[j]) ++j
   if(next == 5*H[k]) ++k

output(H[10])

The key point in this solution is that, if H is a hamming number, then 2H, 3H, 5H is also a hamming number 该解决方案的关键点在于,如果H是汉明数,则2H,3H,5H也是汉明数


I came across a problem , which I sensed it's a bit like Hamming Problem, but it's not constructing number using set of prime factors, instead if I rephase the problem statement, it is like the following: 我遇到了一个问题 ,我觉得它有点像汉明问题,但它不是使用一组素因子来构造数字,而是如果我重新讨论问题陈述,它就像下面这样:

1 is in the result set. 1在结果集中。 If H is in result set, then 2H+1 and 3H+1 is also in the result set. 如果H在结果集中,那么2H + 1和3H + 1也在结果集中。 Find the n-th number in the result set 在结果集中找到第n个数字

Then I wonder if the same constructing algorithm works for this problem, turns out it does! 然后我想知道相同的构造算法是否适用于这个问题,事实证明它确实存在! (And I even have no idea why it works) (我甚至不知道为什么会这样)

Def f(x) 2x+1
Def g(x) 3x+1

List<int> H
int i=0,j=0,n=10 // find the 10-th hamming number
H.add(1)
for(i=0 to 10)
   int next = min(f(H[i]), g(H[j]))
   H.add(next)
   if(next == f(H[i])) ++i
   if(next == g(H[j])) ++j

output(H[10])

So then I wonder: 那么我想知道:

Is this constructing algorithm works for problems of generating numbers, given a rule like "If x is in the result, then all f(x), g(x), p(x), q(x)... are also in the result", provided that these functions will give a result >= x ? 这个构造算法是否适用于生成数字的问题,给定一个规则,如“如果x在结果中,则所有f(x), g(x), p(x), q(x)...也在结果“,只要这些函数给出结果> = x

A sufficient condition is that all functions f_i from the integers to the integers must be monotonic increasing and have n < f_i(n) for all i and n . 一个充分条件是从整数到整数的所有函数f_i必须是单调递增的,并且对于所有in都具有n < f_i(n)

An example demonstrating that you need something like the integers part of the rule is the pair of functions (n+0.5, (n + floor(n+1))/2) . 证明你需要类似规则的整数部分的例子是一对函数(n+0.5, (n + floor(n+1))/2) This will lead to the sequence 1, 3/2, 7/4, 15/8, ... and you'll never get to 2 . 这将导致序列1, 3/2, 7/4, 15/8, ...并且你永远不会达到2

The functions (2^n, 20 - 5n + n^2) comes out in the order 1, 2, 4, 16, 14, ... and is clearly not in order. 函数(2^n, 20 - 5n + n^2) 1, 2, 4, 16, 14, ...的顺序出现1, 2, 4, 16, 14, ...并且显然不是有序的。 Hence the need for non-decreasing. 因此需要不减少。

The function (n-3) gives the sequence (1, -2, -5, ...) and shows the importance of n < f_i(n) . 函数(n-3)给出序列(1,-2,-5,...)并显示n < f_i(n)的重要性。

So why does this work? 那么为什么这样呢?

First of all it is clear that anything output by this algorithm is in the set. 首先,很明显,该算法输出的任何内容都在集合中。

Going the other way, suppose all three conditions are met. 换句话说,假设满足所有三个条件。 We then have to prove by induction that if you belong in the sequence, we begin looking for you before we get there, and then must produce it when we pass you. 然后我们必须通过归纳证明,如果你属于序列,我们会在到达之前开始寻找你,然后在我们通过你时必须生成它。 (That we pass you is guaranteed by the fact that the sequence is an increasing set of integers.) The proof is a little messy, but straightforward. (我们通过你的事实是,序列是一组不断增加的整数。)证明有点混乱,但很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM