Sort by

recency

|

1565 Discussions

|

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

    }

  • + 0 comments

    Here is the my Kotlin solution :

    fun squares(a: Int, b: Int): Int {

    val lowerLimit = ceil(sqrt(a.toDouble())).toInt()
    val upperLimit = floor(sqrt(b.toDouble())).toInt()  
    
    return  upperLimit-lowerLimit + 1
    

    }

  • + 0 comments
    s=int(a**(0.5))
    count=0
    while b>=(s**2):
            if ((s**2)<=b and (s**2)>=a):
                    count+=1
            s+=1
    return count
    

    `

  • + 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
    public static int squares(int a, int b) {
            int min = (int)Math.ceil(Math.sqrt(a));
            int max = (int)Math.floor(Math.sqrt(b));
            int i = min;
            int result = 0;
            while(i<=max){
                if(Math.pow(i,2)>=a || Math.pow(i,2)<=b)
                    result++;
                i++;
            }
            return result;
        }