Sort by

recency

|

1572 Discussions

|

  • + 0 comments

    Here are my c++ solutions, you can watch the explanation here : https://youtu.be/LtU0ItsvbMI

    Solution 1 O(√n)

    int squares(int a, int b) {
        int i = ceil(sqrt(a)), result = 0;
        for(; i * i <= b; i++) result++;
        return result;
    }
    

    Solution 2 O(1)

    int squares(int a, int b) {
        int i = ceil(sqrt(a)), j = floor(sqrt(b));
        return j - i + 1;
    }
    
  • + 0 comments

    python solution with O(1).

    import math
    def squares(a, b):
        #pow(a, 0.5) ~ pow(b, 0.5)
        start = math.ceil(pow(a, 0.5))
        end = math.floor(pow(b, 0.5))
        return end-start+1
    
  • + 0 comments

    Here is my Python solution! The highest and lowest variables are the highest and lowest square root that are between a and b. If the lowest is greater than b, we know that there are no perfect squares. Otherwise, it is simply the difference between the highest and lowest plus 1.

    def squares(a, b):
        highest = math.floor(math.sqrt(b))
        lowest = math.ceil(math.sqrt(a))
        print(lowest, highest)
        if lowest > b:
            return 0
        return highest - lowest + 1
    
  • + 0 comments

    Python

    import math
    def squares(a, b):
        # Write your code here
        c=0
        for each in range(math.floor(math.sqrt(a)), math.floor(math.sqrt(b))+1):
            if a<=math.pow(each, 2)<=b:
                c+=1
        return c
    
  • + 0 comments

    JS

    let amount = 0;
        
    let number = Math.ceil(Math.sqrt(a));
        
    while(number*number <= b) {
    	amount += 1;
    	number += 1;
    }
        
    return amount;