what is the most efficient way to find no. of factors of a number?

@vijju123

Let me tell you the complete question.

I actually Want to find greatest factor of number whose no. of factors is power of 2. So what I was doing is for each no. I precomputed no. of factors then I looped for(i=n;i>0;i–) and if (i%n==0) then I checked if i is power of 2 is yes then returned i otherwise complete the loop.

Now, tell me how your approach would fit in this. Give me little idea about implementation details.

I didnt read ur new comment while posting answer. I will look into it if no one else answers ur Q before my next sitting. :slight_smile:

@vijju123

Shall I precompute the no. of factors for every single no. using this approach, you mean. Did you read my comment on your above answer.

I did not read the comment dear ( i mentioned in my above comment) . Apologies for inconvenience. I think neil answered the Q very nicely :slight_smile:

1 Like

@neilit1992

If possible code would be really appreciated.

7HB6SB - Online C++0x Compiler & Debugging Tool - Ideone.com This is the code, please upvote n mark as legit answer, though vijjus code is same

1 Like

@neilit1992

Let me tell you the complete question.

I actually Want to find greatest factor of number whose no. of factors is power of 2. So what I was doing is for each no. I precomputed no. of factors then I looped for(i=n;i>0;i–) and if (i%n==0) then I checked if i is power of 2 is yes then returned i otherwise complete the loop.

@neilit1992

This is link to my code, please tell me how I can improve it. I am not getting accepted with this code. I am getting TLE.

Please help me out @neilit1992 @vijju123

In your code you can change solve part from i=1 to n^(1/2) and factor are usually of form i and n/i so if 2 is factor of 12 12/2 = 6 is also a factor of 12 so you can reduce the solve part to O(n^(1/2) time complexity and to count factors better use my code, its efficient than your naive algo, and I couldn’t get why you are using initfactor method?

Wait…i didn’t understand. You have to find the greatest factor which is power of 2? Then well…isn’t 2^x the answer (where x is exponent we calculated above)?

Any link to Q?

And if the Q is to find greatest factor, then also, nothing complicated. First check if number prime or not (Sieve might help), check if I%2==0. If yes, then I/2 s greatest factor. If no, check I%3,i%5,i%7…all primes. We can mathematically prove that if I%n==0 (from start). then I/n is greatest factor (for first n enocountered as such).

He needs to find greatest factor whose number of factor is of power 2, this what i understood

As in that factor as 2^k factors, or the factor’s index is 2^k?

@vijju123

for n=15 ans=4

for n=48 ans=8

for n=60 ans=8

for n=15 factors are {1,3,5,15} i.e, 4 factors which is power of 2 so the answer is 4.

for n=48 factors are {1,2,3,4,6,8,12,16,24,48} i.e 10 factors in total which is not power of 2. But the factor of 24 has 8 factors which is power of 2, so the o/p is 8 fir second test case.

for n=60 factors are {1,2,3,4,5,6,12,15,20,30,60}i., 11 factors in total which is not power of 2 but the factor of 30 has 8 factors in total which is power of 2 so the answer is 8.

I think it makes point clear, if you still don’t understand, please write.

@hemanth_1

I actually need to find no. of factors of the any factor of N such that no. of factors should be maximized and also no of factors should be power of 2.

@hemanth_1

for n=15 ans=4

for n=48 ans=8

for n=60 ans=8

for n=15 factors are {1,3,5,15} i.e, 4 factors which is power of 2 so the answer is 4.

for n=48 factors are {1,2,3,4,6,8,12,16,24,48} i.e 10 factors in total which is not power of 2. But the factor of 24 has 8 factors which is power of 2, so the o/p is 8 fir second test case.

for n=60 factors are {1,2,3,4,5,6,12,15,20,30,60}i., 11 factors in total which is not power of 2 but the factor of 30 has 8 factors in total which is power of 2 so the answer is 8.

I think it makes point clear, if you still don’t understand, please write.

@neilit1992

in initFactors() I am precomputing smallest prime factor for every no. and this helps me to count no. of factors efficiently. and I think for counting factors I think runtime of my algorithm is as good as yours if you observe closely. May be I am wrong.

I have coded an efficient algo, 7HB6SB - Online C++0x Compiler & Debugging Tool - Ideone.com If you can provide me link to the question it would be better, anyway check it…

@neilit1992

Can you brief, what exactly you are doing.

7HB6SB - Online C++0x Compiler & Debugging Tool - Ideone.com I have commented the code, upvote the ans, and mark as correct Thank you, if you need further clarification request

2 Likes

@neilit1992

I understand your code and logic but can you please tell how complexity of your code is better than that of mine. I feel complexity of my code is same as that of yours.

This is final version of my code. SCF7NM - Online Java Compiler & Debugging Tool - Ideone.com