vikky
-Excellent programming skills with certification from Sun Microsystems.
-Ability to learn and implement new technologies and concepts very quickly.
-Seeking employment in an organization that would allow the implementation of academic and practical knowledge with dedication, team spirit and co-operation.
-Hoping to work under the most challenging circumstances with an organization that provides ample opportunities to learn and grow.
Initially we can check whether n is a factor of k or not
- vikky June 20, 2012if(k%n==0)
then only we need to check further, otherwise return false directly.
and if it returns true then we can check whether n is even or odd in that way we need to loop only for logarithmic time that is greater improvement .
if n is even then proceed in multiples of n
eg:
if(n%2==0){
int flag=n;
for(i=0;i<n;i++)
{
k=k/flag;
if(k==1) return true;
flag=flag*flag;
}
return false;
}
similar could be handled for n being odd only 1 more iteration required in that case.
so in this case we didn't need to check again and again for same value of n and value of n increases in each iteration.. that is n, n^2, n^4, n^16..... so the algorithm runs in logarithmic time, that is greater improvement than naive approach.