• + 0 comments

    O(1) Solutions

    1. C One liner: with inline comments

    // a: an integer, the lower range boundary
    // b: an integer, the upper range boundary
    int squares(int a, int b) {
    
        // floor(sqrt(b)) - sqrt of perfect square within upper limit
        // ceil(sqrt(a)) - square root of perfect square above lower limit
        // +1 - to make the range inclusive
        return floor(sqrt(b)) - ceil(sqrt(a)) + 1;
    }
    

    2. Python 3 One liner: with inline comments

    def squares(a, b):
    
        # math.floor(math.sqrt(b)) - sqrt of perfect square within upper limit
        # math.ceil(math.sqrt(a)) - square root of perfect square above lower limit
        # +1 - to make the range inclusive
        return math.floor(math.sqrt(b)) - math.ceil(math.sqrt(a)) + 1
    

    Feel free to ask your questions or concerns - by Danie