4

Given a number k and a set of sorted numbers. Find if there is any number in the set which divides this number.

For example if k = 8, and set is { 3, 4, 5}, 4 will divide 8. 4 is the answer.

Worst case solution is O(n).

Can we do it better?

sunmoon
  • 1,448
  • 1
  • 15
  • 27

3 Answers3

2

How about factorize the number (8 gives us 4 2 1) then search for the factors in your given set? You can use set intersections or bisection search your list of factors. I think it will give you a quicker answer for large sets.

Lmwangi
  • 2,486
  • 1
  • 19
  • 26
  • 1
    What is the best way to factorize a number? – sunmoon Mar 01 '11 at 09:50
  • ... but you will have to factorize the numbers in the set to, to make a proper intersection - unless I'm missing something. – ltjax Mar 01 '11 at 10:00
  • Regarding this problem, I don' think so since he said "Find if there is any number in the set which divides this number.". So we are looking up whether our factors are in the set. Nothing more. Coming to think about set intersections, I think they would would perform badly since by definition, they are unordered and an intersection is O(n). Ouch! So i'd go with a binary search of the factors - http://en.wikipedia.org/wiki/Binary_search_algorithm – Lmwangi Mar 01 '11 at 10:11
  • Oh, I was thinking you wanted to do an intersection on just the prime factors. But all the factors? that's insane for large numbers. The number of primes below a value `v` is approximately `v/ln(v)`, and all their combinations will give you a lot more. – ltjax Mar 01 '11 at 10:23
  • @Lmwangi: The question is, how large are the numbers and the sets? For very larger numbers and small sets of candidates, i believe the cost of factorization would exceed the cost of simply trying all factors. I am not sure about this though, so feel free to correct me. – Björn Pollex Mar 01 '11 at 10:40
  • +1, I think this is a good solution as long as the queried numbers aren't too big and the set if very large in comparison. For a very big value and a small set, a O(n) search would be faster. – MAK Mar 01 '11 at 10:46
  • @Space_cowboy: True, for large numbers and small sets, factoring the number would be expensive. Maybe a hybrid approach would help where we (1) Pick the largest integer (X) from the sorted set (2) Stop factoring the provided number if our we are getting factors larger than X (3) Compare our list of factors with our sorted list. What do you think? – Lmwangi Mar 01 '11 at 10:49
0

If k is prime, it has no factors in the set and you're done. Otherwise, k = p*q where p is k's smallest factor. Do a binary search for q. If found, you're done. Otherwise, refactor k=p'*q', where p' is the next largest factor of k after p -- if none, you're done. Otherwise, continue the binary search for q' -- note that q' < q, so you can continue the search with the high bound used for q. Continue until a factor is found or you've searched for k's largest factor. This is O(logn). In the concrete case of k = 8, you would search first for 4, then for 2 ... if neither is found then the set does not contain a divisor of k.

EDIT: Hmmm ... I guess this isn't O(logn). If, e.g., the list contained f-1 for every factor f of k, then you would have to search for each f in succession, hitting f-1 each time ... that would be O(n).

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0

Calculate the gcd of k and the product of the members of the set. For the example, gcd(3*4*5,8) = 4.

user448810
  • 17,381
  • 4
  • 34
  • 59