Ugly number

设计一个算法,找出只含素因子357 的第 k 大的数。

符合条件的数如:3,5,7,9,15......

class Solution {
public:
    /*
     * @param k: The number k.
     * @return: The kth prime number as description.
     */
    long long kthPrimeNumber(int k) {
        // write your code here
        if( k == 1 ) return 3;
        long long last = kthPrimeNumber(k-1), m = last;
        long long res = LONG_MAX;
        int i3 = 0;
        while(true)
        {
            long long s3 = pow(3, i3++);
            if(s3 > last ){ res = min(res, s3); break; }
            int i5 = 0;
            while(true)
            {
                long long s5 = pow(5, i5++);
                if( s3*s5 > last ){ res = min(res, s3*s5); break; }
                double tmp = last*1.0 / (s3*s5);
                int i7 = log(tmp) / log(7) + 1;
                long long s7 = pow(7, i7);
                res = min(res, s3*s5*s7);
            }
        }
        
        return res;
    }
};


你可能感兴趣的:(算法,leetcode系列)