k-Smooth Numbers and its Generalization
1. Introduction
A positive integer is called -smooth if none of its prime factors are greater than . Let be the set of prime numbers not greater than , i.e.,
In the classic Hamming problem, we are asked to print the first -smooth numbers (they are also called Hamming numbers) in the increasing order. Dijkstra [1] proposed the algorithm below in 1981 to solve this problem.
Dijstra’s Algorithm
, ,
while
.
( means the set )
Put into
endwhile
Although Dijkstra’s algorithm is designed to solve the classic Hamming problem, it is quite straightforward to extend it to solve general Hamming problem, i.e., print the first -smooth number in the increasing order. However, the programming language without lazy evaluation feature, it is very hard to manage it to run in time, provided that computing multiplication of two integers takes time. In this paper, we propose a new algorithm to solve Hamming problem in a more general setting described below.
Let be a finite set of positive integers. We arrange the elements in so that . We say that is a smooth base if for each element , it has a prime factor whose is not included in any other element. Formally, is a smooth base if and only if
where means there is a integer such that , and means no such integer exsits. Such a prime factor is called the key factor of . In other words, is a smooth base if and only if every element has at least one key factor.
Given a smooth base , we use to denote the set containing all the integers in the form
where are nonnegative integers. We also say that is generated by , and numbers in are called generalized Hamming numbers. It is easy to see that every number in can be uniquely represented by this way, i.e., every number in correpsonds to a unique tupple . Thus, once the smooth base is fixed, we also simply use to denote .
Note that is a smooth base for all . For example, if , then is the collection of all -smooth numbers. Thus, computing the first -smooth numbers is just a special case of computing first generalized Hamming numbers generated by a smooth given smooth base.
2. Algorithm
Examinating the Dijkstra’s algorithm closely, we can see that there are two essential operations: finding the minimum of and merging , and . For the merging operation, if the three components merged are disjoint, then it is very easy. Unfortunately, they are not. For example, belong to and for . To avoid such duplicating, one possible way is to resolve the ambiguity: assign to either or , but not both. This can be done by adopting a kind of “Maximum Principle”: if and at the same time, and , we only assign to . For belongs to more than two such sets , we assign to the one with largest . Inspired by this idea, we have following algorithm to compute first generalized Hamming numbers generated by a smooth base .
k-Smooth Algorithm
Initialize queues to be empty
for from to
push into queue
endfor
while do
Let be the minimum element in the front of each queue and assume
for from to
push into queue
endfor
Remove from .
Put into output sequence
endwhile
Theorem 1: When k-Smooth Algorithm terminates, is the sequence of the first generalized Hamming numbers generated by .
To prove the theorem, we shall firstly establish two impartant facts. The first one shows that and are disjoint if .
Fact 1: If , then .
Proof: We first show by induction that any element in does not contain any key factors of . At the beginning, and by the definition of smooth base , does not have key factors of . Now assume after the first loops of the while block, elements in do not contain key factors of . If at the st loop of the while block, a new element is pushed into , then according to the algorithm, must come from and hence does not contain key factors of . This also implies that does not contain key factors of and all members of do not contain key factors of in the end of the st loop. The statement is trivially true if there is no element pushed into in st loop.
Again, by induction we show that members in () do not contain key factors in . For , the statement holds by the argument above. Now assume the statement holds for all . By the similar argument used to prove the statement for , we can show that the statement also holds for . Therefore, the statement holds for .
By the algorithm, we also know that elements in includes key factor of . Now let . Since members in do not contain any key factor of while all members in have a key factor of , no element in can belong to and hence .
Now we have known that elements from different queues are distinct. To demonstrate that no duplicated numbers will be added to , we need to show that elements in the same queue is also distinct. Actually, we manage to show a stronger conclusion: elements in the same queue are pushed into the queue by the strictly increasing order. We also obtain an important fact at the same time, which shows every generalized Hamming number will be pushed into some queue. This guranttees than no generalized Hamming numbers are skipped by the algorithm.
Before starting the next fact, we define followers of a generalized Hamming number as the numbers .
Fact 2: At any time, elements in () are strictly increasing and hence distinct. Also, if at step , is removed from some queue, then each of its followers, either has been already pushed into some queue at some step ), or will be pushed into some queue at the step .
Proof: Again, we prove it by induction. Obviously, at the very beginning of the first loop of the while block, the statement above holds. Assume the statement is correct at the step . Suppose at the step , is removed from queue .
For , since is smaller than , it was removed at some step , according to the induction assumption. Therefore, by the assumption, its follower , which is also a follower of , was pushed into some queue before the step . For , the follower is pushed into at the step . Therefore, the second half of the statement still holds for the step .
For each such that , there is a new element pushed into . If contains only one element , then is increasing trivially. Now assume contains more than one element. Let be any one in other than . According to the algorithm, was pushed into before because the element was removed from some queue before . Hence , and further . That is, , and is strictly increasing.
Given these two facts established, it is quite straightforward to see the correctness of the statement in Theorem 1.
Proof: Since numbers in each queue is strictly increasing and numbers in all queues are distinct, the outputed sequence is strictly increasing and hence has no duplicates. Also, since no number will be skipped, the output sequence must contain the first generalized Hamming numbers generated by the base when the algorithm terminates.
A PDF version of this article can be found here.
References
[1] Edsger W. Dijkstra. Hamming’s exercise in sasl. 1981.
An Integer Decomposition Problem
There are a lot of integer decomposition problems. Here is one of them: decompose an integer N into k different integers so that the sum of them is N and the multiplication of them is maximized. A vivid version of this problem described below.
Parliament (source: Northeastern Europe 1998): New convocation of The Fool Land’s Parliament consists of delegates. According to the present regulation delegates should be divided into disjoint groups of different sizes and every day each group has to send one delegate to the conciliatory committee. The composition of the conciliatory committee should be different each day. The Parliament works only while this can be accomplished. You are to write a program that will determine how many delegates should contain each group in order for Parliament to work as long as possible.
Input: The input file contains a single integer ( ).
Output: Write to the output file the sizes of groups that allow the Parliament to work for the maximal possible time. These sizes should be printed on a single line in ascending order and should be separated by spaces.
Sample Input
7
Sample Output
3 4
Before introduce the solution, let us review an inequality and the multiplication principle described below.
Theorem 1 (Inequality of Arithmetric and Geometric Means) For any list of nonnegative numbers , we have
and the equality holds if and only if .
Theorem 2 (Multiplication Principle) If a task consists of different operations , and each operation can be done by ways. Then, there are in total different ways to complete the task.
Back to our problem on hand. Assume that all delegates are separated into disjoint groups, each of which has delegates. Then, according to multiplication principle, there are different ways to compose a conciliatory committee. According to the inequality of arithmetric and geometric means, reaches its maximal value when .
Unfortunately, for any and , if , then , and should be an integer. However, the inequality of arithmetric and geometric means does give us some intuition: should be as larger as get close to each other. The following lemmas confirms this intuition. Without loss of generality, assume . Define the of two integers and as the number of integers between them. Denote it by . For example, since there are 6 and 7 between them. If , then we say that and has gap.
Lemma 3 Let when , and are nonnegative integers and . has the maximal value if and only if . Particularly, when , the maximum of is larger than strictly.
Given a increasing sequence of integers , define the number of gaps in as the number of pairs (,) which . For example, sequence has 2 gaps: one between 2 and 5, and the other between 6 and 8.
Lemma 4 reaches the maximal value if and only if there is at most one gap in and that gap is at most 1 if any.
The lemma above tells us that to make as large as possible, the sequence should composed by a list of continous integers , or two segments of continous integers and where . Example: , . Then . If , then . For , .
One natural question is that, given is fixed, how to look for valid sequence which maximize ? Well, by lemmas above, we can assume that where and . So
Or
by assuming where ,
Since is integer and , when is odd, . When is even, , depending on if is larger than half of . By this way, for any and valid , we compute the unique pair of and .
Lemma 5 Given and , the sequence maximizing is unique.
At this point, we can solve the problem as follows: For all possible , compute the and by above way. Then compute their corresponding values, between which we pick the largest one. The algorithm is inefficient since we involve computing the pretty number of many factors (imagine !!). Java and some programming language do provide BigInteger class, but it is time consuming.
The question following is, can we void manipulation on big integers? Yes. To make it, we need some insightful thoughts. From the example given above, it seems that increases and decreases when get larger, but not too large. At least, is not expected and should be voided. In a word, we try to get an as small as possible. The perfect value for would be 2, of course. However, not always we can make it. The good news is, should be 2 or 3.
Lemma 6 Let be the sequence maximinze among all valid sequences. Then is 2 or 3.
Proof: We prove it by contradiction. Assume . Consider the case . By Lemma 4, or . If , we construct a new sequence by replacing with and and keeping others intacted. Note that is a valid sequence, meaning that all elements in it are different and sum to . Then . That’s , which is a contradiction. If , we construct a new valid by replacing and by 2,3 and 5. Since , it contradicts the optimality of . Consider . In this case, we construct the new valid by replacing with two small factors and such that . According to Lemma 5, , yet another contradiction. So . Obviously, if , we can easily construct an valid .
Now, we narrow the search scope to those starting with 2 or 3. Furthermore,
Lemma 7 Let be the sequence maximizing starting from 2 () and starting from 3 (), where and . Let be the length of and be the length of . Then, and .
Proof: Obviously, . and . Combining them, we have
Assume . Then , or . However, since and , it’s impossible. So and then .
The above lemma simply states that if we get two valid sequences, namely, one starting from 2 with length which maximize when , and the other starting from 3 with length which maximize when , and each of them consists of two segments of continguous integers, then latter segment of the one starting from 2 is short than that of the one starting from 3.
This fact leads to following important lemma.
Lemma 8 Let valid start from 3 (). Then maximizes among all valid sequences if and only if or .
Proof: In either case of or , no valid sequence starting from 2 can be constructed according Lemma 7. According to Lemma 6, maximizes . Now assume maximize but . Then, we construct a new valid by replacing with 2 and . Since and , . So . Contradiction.
Now we come to the crux of the problem. We construct a valid sequence starting from where by the following way: start from , we keep adding until adding will make the sum of elements in larger than . Then, we increases each element by 1 in the order of and repeat the process until it sums to . The sequence we finally get is the answer.
array stores the sequence
while
end while
while
if
end if
end while
The next step is to prove the algorithm is correct. Given above lemmas, it’s an easy task.
Proof: Firstly, . Otherwise, the first while loop won’t terminate. So the body of second while loop will be executed at most times. If or , then starts from 3 with the second segment of length 0 or 1, respectively. According to Lemma 8, is optimal. If , we are unable to construct a sequence such that starts from 3 and will the second segment of length at most 1. If , has second segment of length at least 1. According to Lemma 7, we also cann’t construct maximing and starting from 3 with second segment of length at most 1. Combining Lemma 6, the is optimal.
The PDF version of this solution is available here.
Yet Another Note On Skewness And Kurtosis
Due to a project I recently work on, I would like to know the relationship between skewness and kurtosis. After some Google search, I am directed to Wilkins’s paper : A Note On Skewness And Kurtosis [PDF] in which he gave an new proof of the following inequality:
However, he only proved it for random variables with finite values. It’s quite natural to extend his proof to any real-valued random variable. Here is the proof I give only involving fundamental concept and definition from probability theory and quadratic form which is exactly the remarkable idea from Wilkins’s original proof.
Let be a real-valued random variable defined on probability space . Let be the mean of . Then,
Also, denote the central moment of by . Then,
And, the standard deviation of is defined as
Define the standard moment of as
Here, is called skewness and is called kurtosis. Note that
and
Now, consider the quadratic form
.
Since for all ( is semi-definite), we shall have its discriminant larger than or equal to . That is,
For any real-valued random variable whose standard deviation is not zero, we have
Note that standard deviation if and only if is constant almost everywhere.