Sort by

recency

|

1568 Discussions

|

  • + 0 comments

    JS

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

    c++

    int squares(int a, int b) { int result = 0;

    for(int i=1; i<=b;i++){
        int square = pow(i, 2);
        if(square<= b and square>=a){
            result++;
        }else if (square > b) {
            break;
        }
    }
    
    return result;
    

    }

  • + 0 comments

    c++

    int squares(int a, int b) { int result = 0;

    for(int i=1; i<=b;i++){
        int square = pow(i, 2);
        if(square<= b and square>=a){
            result++;
        }else if (square > b) {
            break;
        }
    }
    
    return result;
    

    }

  • + 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

    function squares(a: number, b: number): number { // Write your code here let squaresCount: number = 0

    let lower: number = Math.ceil(Math.sqrt(a))
    let upper: number = Math.floor(Math.sqrt(b))
    
    let squaresSet: Set<number> = new Set()
    
    for (let i = lower; i <= upper; i++) {
        squaresSet.add(i * i)
    }
    
    console.log(`lower: `${lower}, upper: $`{upper}, squareSet: ${Array.from(squaresSet)}`)
    
    return squaresSet.size
    

    }