Skip to content

Instantly share code, notes, and snippets.

@hueitan
Last active August 29, 2015 14:20
Show Gist options
  • Save hueitan/6eaad3406ca84d19ec28 to your computer and use it in GitHub Desktop.
Save hueitan/6eaad3406ca84d19ec28 to your computer and use it in GitHub Desktop.
trailing zero problem (superfactorial) - https://codefights.com/feed/ZKW6FzW8Rjj9wTLWo
function superfactorialZeros(N) {
    var result = 0;
    for (i = 1; i <= N; i++) {
        result += zeroes(i);
    }

    return result;
}

function zeroes(n) {
    var i = 1;
    var result = 0;
    while (n >= i) {
        i *= 5;
        result += Math.floor(n / i);
    }
    return result;
}

// alternative of zeroes
var zeroes = function z(n)  {
    k = n/5 | 0
    return k ? k + f(k) : 0
}
// or
function zeroes (n) {
     s = 0
     while (n/=5) s += n|0
     return s
}

79 char

function superfactorialZeros(N) {
    R = 0

    for (; N ; N--)
        for (j = 5; N >= j; j *= 5) 
            R += N / j | 0
        
    return R
}

76 char

function superfactorialZeros(N) {
    R = 0

	while (n=N--)
        while(n>=5)
            R += (n/=5)|0
        
    
    return R
}

well... 71 cahr

function superfactorialZeros(N) {
    R = 0

    while (j = N--)
        while (j /= 5)
            R += j & j

    return R
}
@hueitan
Copy link
Author

hueitan commented May 6, 2015

Really learn a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment