hamming-numbers

Find the smallest regular number that is not less than N

穿精又带淫゛_ 提交于 2019-12-18 23:13:20
问题 Regular numbers are numbers that evenly divide powers of 60. As an example, 60 2 = 3600 = 48 × 75, so both 48 and 75 are divisors of a power of 60. Thus, they are also regular numbers. This is an extension of rounding up to the next power of two. I have an integer value N which may contain large prime factors and I want to round it up to a number composed of only small prime factors (2, 3 and 5) Examples: f(18) == 18 == 2 1 * 3 2 f(19) == 20 == 2 2 * 5 1 f(257) == 270 == 2 1 * 3 3 * 5 1 What

Generating a sequence using prime numbers 2, 3, and 5 only, and then displaying an nth term (C++)

时间秒杀一切 提交于 2019-12-18 09:49:07
问题 I'm working on a problem that asks to generate a sequence using prime numbers 2, 3, and 5, and then displaying then nth number in the sequence. So, if I ask the program to display the 1000th number, it should display it. I can't be using arrays or anything like that, just basic decisions and loops. I started working on it and hit a wall... here's what I got: #include <iostream> using namespace std; int main() { unsigned int n=23; for(int i=2; i<n; i++){ if(i%2==0){ cout<<i<<", "; }else if(i%3

New state of the art in unlimited generation of Hamming sequence

北城余情 提交于 2019-12-17 07:34:49
问题 (this is exciting!) I know, the subject matter is well known. The state of the art (in Haskell as well as other languages) for efficient generation of unbounded increasing sequence of Hamming numbers, without duplicates and without omissions, has long been the following (AFAIK - and btw it is equivalent to the original Edsger Dijkstra's code too): hamm :: [Integer] hamm = 1 : map (2*) hamm `union` map (3*) hamm `union` map (5*) hamm where union a@(x:xs) b@(y:ys) = case compare x y of LT -> x

Generating integers in ascending order using a set of prime numbers

…衆ロ難τιáo~ 提交于 2019-12-17 05:06:06
问题 I have a set of prime numbers and I have to generate integers using only those prime factors in increasing order. For example, if the set is p = {2, 5} then my integers should be 1, 2, 4, 5, 8, 10, 16, 20, 25, … Is there any efficient algorithm to solve this problem? 回答1: The basic idea is that 1 is a member of the set, and for each member of the set n so also 2 n and 5 n are members of the set. Thus, you begin by outputting 1, and push 2 and 5 onto a priority queue. Then, you repeatedly pop

How to display first N natural numbers, knowing the divisors in Lisp

房东的猫 提交于 2019-12-02 02:31:53
Display first N natural numbers, the divisors of which are only 2, 3 and 7. I wrote something like that. I am a beginner in Lisp. Thank you! defvar x 1 (defun numbers(n) if(mod x 2 ) (loop for x from 1 to n do(print x) ) ) print(numbers()) Because I just had some time, you could have a look at this. Might not be the perfect solution but should be a good starting point for a beginner. Check out the books in the info tab to get into the syntax etc. (defun divisible-by (n m) "Returns T if N is evenly divisible by M." (zerop (mod n m))) (defun numbers (n) "Print all number upto N which are

Find the smallest regular number that is not less than N

好久不见. 提交于 2019-11-30 18:59:24
Regular numbers are numbers that evenly divide powers of 60. As an example, 60 2 = 3600 = 48 × 75, so both 48 and 75 are divisors of a power of 60. Thus, they are also regular numbers. This is an extension of rounding up to the next power of two . I have an integer value N which may contain large prime factors and I want to round it up to a number composed of only small prime factors (2, 3 and 5) Examples: f(18) == 18 == 2 1 * 3 2 f(19) == 20 == 2 2 * 5 1 f(257) == 270 == 2 1 * 3 3 * 5 1 What would be an efficient way to find the smallest number satisfying this requirement? The values involved

Hamming numbers for O(N) speed and O(1) memory

喜欢而已 提交于 2019-11-30 05:44:42
问题 Disclaimer: there are many questions about it, but I didn't find any with requirement of constant memory. Hamming numbers is a numbers 2^i*3^j*5^k , where i, j, k are natural numbers. Is there a possibility to generate Nth Hamming number with O(N) time and O(1) (constant) memory? Under generate I mean exactly the generator, i.e. you can only output the result and not read the previously generated numbers (in that case memory will be not constant). But you can save some constant number of them

Tricky Google interview question

青春壹個敷衍的年華 提交于 2019-11-29 18:31:12
A friend of mine is interviewing for a job. One of the interview questions got me thinking, just wanted some feedback. There are 2 non-negative integers: i and j. Given the following equation, find an (optimal) solution to iterate over i and j in such a way that the output is sorted. 2^i * 5^j So the first few rounds would look like this: 2^0 * 5^0 = 1 2^1 * 5^0 = 2 2^2 * 5^0 = 4 2^0 * 5^1 = 5 2^3 * 5^0 = 8 2^1 * 5^1 = 10 2^4 * 5^0 = 16 2^2 * 5^1 = 20 2^0 * 5^2 = 25 Try as I might, I can't see a pattern. Your thoughts? user515430 Dijkstra derives an eloquent solution in "A Discipline of

1 billionth ugly or hamming number?

≡放荡痞女 提交于 2019-11-29 18:15:19
Is this the 1 billionth ugly/hamming number? 62565096724471903888424537973014890491686968126921250076541212862080934425144389 76692222667734743108165348546009548371249535465997230641841310549077830079108427 08520497989078343041081429889246063472775181069303596625038985214292236784430583 66046734494015674435358781857279355148950650629382822451696203426871312216858487 7816068576714140173718 Does anyone have code to share that can verify this? Thanks! Will Ness this SO answer shows code capable of calculating it. the test entry on ideone.com takes 1.1 0.05 sec for 10 9 (2016-08-18: main speedup

Generating a sequence using prime numbers 2, 3, and 5 only, and then displaying an nth term (C++)

爱⌒轻易说出口 提交于 2019-11-29 17:41:11
I'm working on a problem that asks to generate a sequence using prime numbers 2, 3, and 5, and then displaying then nth number in the sequence. So, if I ask the program to display the 1000th number, it should display it. I can't be using arrays or anything like that, just basic decisions and loops. I started working on it and hit a wall... here's what I got: #include <iostream> using namespace std; int main() { unsigned int n=23; for(int i=2; i<n; i++){ if(i%2==0){ cout<<i<<", "; }else if(i%3==0){ cout<<i<<", "; }else if(i%5==0){ cout<<i<<", "; } } return 0; } Unfortunately, that code doesn't