Saturday, September 5, 2015

#codingexercise
Generate n numbers in ascending orders which are having given k factors.
For example, if we are given {2,3,4,7}
The numbers generated would be 2,3,4,6,7,8,9,10...
Let us assume that there are no duplicates in the number.

# http://www.cs.utexas.edu/users/EWD/ewd07xx/EWD792.PDF

n = 15
bases = [2, 3, 4, 7]

nums = [1] * n
candidates_indexes = [0 for _ in bases]
candidates = [base for base in bases]

for i in range(1, n):
    nextn = min(candidates)
    nums[i] = nextn

    for index, val in enumerate(candidates):
        if val == nextn:
            candidates_indexes[index] += 1
            candidates[index] = bases[index] * nums[candidates_indexes[index]]


print(nums)
Start:
candidates=[]
candidate_indexes []
nums[2]
From Stackoverflow
The original design talked about prime factors. Here we have liberally used non primes.

No comments:

Post a Comment