determine number n , know X is n th ugly number : > "Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The
> sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly
> numbers. By convention, 1 is included."

Given number X, determine the order of X in that sequence.
Example : X = 12, output : 10.

I have an algorithm O(X) but X might be 1e18, what should I do? 

There might be 10^5 queries, each query gives us a number X.

Thank you.