Advertisement
Alhiris

Untitled

Feb 11th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /**
  2. To get all the powers of a number till a n, for example 1*2*3*...*n , how many powers of 2 are there for n=10 :: ans=8 so we do this to find out fast
  3. pow=2,n=10,pows=2;
  4. while(pows<=n){
  5. ans+=n/pows;
  6. if(pows>n/pow) break;
  7. pows*=pow;
  8. }
  9.  
  10. So for 10 and 2, as we were saying we have 10/2+10/4+10/8 and if you think from a eratosthene's perspective you will realise it will overwrite all the places it needs
  11. for e.g. for 4 it is came upon once again in 10/4, and on 8=2^3 we have the 10/8, itself 10/4 and 10/2. On 6, we have only 10/2 because it has 2^1.
  12. This is the fastest way to calculate number of powers of a number, especially on the case of !n;
  13. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement