Direct translate from a math perspective in C


  • 0
    G

    so the idea here is that N=[N/5]+[N/25]+[N/125]+....[N/5^huge number]
    N is the given number, [x] indicates the greatest integer not exceeding x and [N/5^huge number] indicating the [] value at that point = 0 (so that [N/5^huge number-1]=1)

    here is the naive solution

    int trailingZeroes(int n) {
        if(n<5)
            return 0;
        else if(n<10)
            return 1;
        else if(n<15)
            return 2;
        else{
            n=n/5;
            return n+ trailingZeroes(n);
        }    
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.