hamming-numbers

Merge of lazy streams (using generators) in Python

喜夏-厌秋 提交于 2019-11-29 16:33:23
I'm playing with functional capacities of Python 3 and I tried to implement classical algorithm for calculating Hamming numbers. That's the numbers which have as prime factors only 2, 3 or 5. First Hamming numbers are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 18, 20 and so on. My implementation is the following: def scale(s, m): return (x*m for x in s) def merge(s1, s2): it1, it2 = iter(s1), iter(s2) x1, x2 = next(it1), next(it2) if x1 < x2: x = x1 it = iter(merge(it1, s2)) elif x1 > x2: x = x2 it = iter(merge(s1, it2)) else: x = x1 it = iter(merge(it1, it2)) yield x while True: yield next(it) def

Find the Kth least number for expression (2^x)*(3^y)*(5^z)

醉酒当歌 提交于 2019-11-28 18:19:25
In the expression 2 x * 3 y * 5 z The x , y and z can take non negative integer value (>=0). So the function would generate a series of number 1,2,3,4,5,6,8,9,10,12,15,16.... I have a brute force solution. I would basically iterate in a loop starting with 1 and in each iteration I would find if the current number factors are only from the set of 2,3 or 5. What I would like to have is an elegant algorithm. This is an interview question. This can be solved using a priority queue, where you store triplets (x, y, z) sorted by the key 2 x 3 y 5 z . Start with only the triplet (0, 0, 0) in the queue

1 billionth ugly or hamming number?

泄露秘密 提交于 2019-11-28 14:00:52
问题 Is this the 1 billionth ugly/hamming number? 62565096724471903888424537973014890491686968126921250076541212862080934425144389 76692222667734743108165348546009548371249535465997230641841310549077830079108427 08520497989078343041081429889246063472775181069303596625038985214292236784430583 66046734494015674435358781857279355148950650629382822451696203426871312216858487 7816068576714140173718 Does anyone have code to share that can verify this? Thanks! 回答1: this SO answer shows code capable of

Tricky Google interview question

家住魔仙堡 提交于 2019-11-28 13:07:58
问题 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

Merge of lazy streams (using generators) in Python

坚强是说给别人听的谎言 提交于 2019-11-28 11:18:50
问题 I'm playing with functional capacities of Python 3 and I tried to implement classical algorithm for calculating Hamming numbers. That's the numbers which have as prime factors only 2, 3 or 5. First Hamming numbers are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 18, 20 and so on. My implementation is the following: def scale(s, m): return (x*m for x in s) def merge(s1, s2): it1, it2 = iter(s1), iter(s2) x1, x2 = next(it1), next(it2) if x1 < x2: x = x1 it = iter(merge(it1, s2)) elif x1 > x2: x = x2 it =

Find the Kth least number for expression (2^x)*(3^y)*(5^z)

故事扮演 提交于 2019-11-27 11:15:39
问题 In the expression 2 x * 3 y * 5 z The x , y and z can take non negative integer value (>=0). So the function would generate a series of number 1,2,3,4,5,6,8,9,10,12,15,16.... I have a brute force solution. I would basically iterate in a loop starting with 1 and in each iteration I would find if the current number factors are only from the set of 2,3 or 5. What I would like to have is an elegant algorithm. This is an interview question. 回答1: This can be solved using a priority queue, where you

New state of the art in unlimited generation of Hamming sequence

ε祈祈猫儿з 提交于 2019-11-27 04:54:57
(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 : union xs b EQ -> x : union xs ys GT -> y : union a ys The question I'm asking is, can you find the

nth ugly number

我们两清 提交于 2019-11-27 00:05:30
Numbers whose only prime factors are 2, 3 or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large. I wrote a trivial program that computes if a given number is ugly or not. For n > 500 - it became super slow. I tried using memoization - observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that it is slow. I tried using some properties of log - since that will reduce this problem from

Generating integers in ascending order using a set of prime numbers

这一生的挚爱 提交于 2019-11-26 20:48:43
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? 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 the front item of the priority queue, output it if it is different from the previous output, and push 2

nth ugly number

情到浓时终转凉″ 提交于 2019-11-26 09:19:56
问题 Numbers whose only prime factors are 2, 3 or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large. I wrote a trivial program that computes if a given number is ugly or not. For n > 500 - it became super slow. I tried using memoization - observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that