V = 2^i * 3^j * 5^k, how 2 work out the problem efficiently?
Posted: Mon Jan 07, 2008 12:20 pm
Recently, I met an interesting problem, I don't know whether VOJ has a similar problem.
V = 2^i * 3^j * 5^k
i, j, k all belong to natural number, i.e. , 0, 1, 2...
so, V can be 1, 2, 3, 4, 5, 6, 8, 9, 10,..... in ascending order.
Input is n, which represents the order number of V
Output is the value of V.
Sample Input
7
Sample Output
8
=============================================
Well, I've got an idea which is simple but has low efficiency.
The benefit of this idea is no need to consider the ascending order, but
there're so many invalid numbers need to be checked, such as 7 11 13
17 19...
So I wonder if there's a rule, by which V varies in ascending order with
i,j,k. Then we can only consider i, j, k, and n, without invalid V values.
Has anybody got a smart idea, thanks!
V = 2^i * 3^j * 5^k
i, j, k all belong to natural number, i.e. , 0, 1, 2...
so, V can be 1, 2, 3, 4, 5, 6, 8, 9, 10,..... in ascending order.
Input is n, which represents the order number of V
Output is the value of V.
Sample Input
7
Sample Output
8
=============================================
Well, I've got an idea which is simple but has low efficiency.
The benefit of this idea is no need to consider the ascending order, but
there're so many invalid numbers need to be checked, such as 7 11 13
17 19...
So I wonder if there's a rule, by which V varies in ascending order with
i,j,k. Then we can only consider i, j, k, and n, without invalid V values.
Code: Select all
int main(void)
{
int N;
while(scanf("%d", &N)){
if(N <= 0) continue;
int v, p, temp;
v = 1; p = 1;
while(p < N){
v++;
temp = v;
while(temp % 2 == 0) temp /= 2;
if(temp == 1) { p++; continue;}
while(temp % 3 == 0) temp /= 3;
if(temp == 1) { p++; continue;}
while(temp % 5 == 0) temp /= 5;
if(temp == 1) { p++; continue;}
}
printf("%d\n", v);
}
return 0;
}