Sort by

recency

|

1042 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can what the explanation here : https://youtu.be/MttrQCHGu3w

    int getSum(long a){
        long sq = a * a;
        int digit = (int)log10(a) + 1;
        int result = sq % (int)pow(10, digit);
        int rest = (int)log10(sq) + 1 - digit;
        if(rest > 0) result += stoi(to_string(sq).substr(0, rest));
        return result;
    }
    
    void kaprekarNumbers(int p, int q) {
        bool valid_range = false;
        for(int i = p; i <= q; i++){
            int s = getSum(i);
            if(s == i){
               cout << i << " ";
               valid_range = true; 
            }  
        }
        if(!valid_range) cout << "INVALID RANGE";
    }
    
  • + 0 comments

    RUST:

    fn kaprekarNumbers(p: i32, q: i32) {
        let mut results: Vec<String> = Vec::new();
        for n in p..=q {
            let square = (n as u128).pow(2).to_string();
            let middle = square.len() - n.to_string().len();
    
            let (left, right) = square.split_at(middle);
            let left_int = left.parse::<i32>().unwrap_or(0);
            let right_int = right.parse::<i32>().unwrap_or(0);
            if left_int + right_int == n {
                results.push(n.to_string());
            }
        }
        if results.len() > 0 {
            println!("{}", results.join(" "))
        } else {
            println!("INVALID RANGE")
        };
    }
    
  • + 0 comments

    Here is my rather long Python solution! The first function checks if a number is a modified kaprekar number, and the second function puts all numbers in the specified into the first function, and returns the results.

    def isk(num):
        d = len(str(num))
        num = str(num ** 2)
        if len(num) == 2 * d:
            if int(num[-d:]) + int(num[:d]) == math.sqrt(int(num)):
                return True
        elif len(num) == 2 * d - 1:
            if d != 1:
                if int(num[-d:]) + int(num[:d - 1]) == math.sqrt(int(num)):
                    return True
            elif num == "1":
                return True
        return False
    
    def kaprekarNumbers(p, q):
        knums = []
        for num in range(p, q + 1):
            if isk(num):
                knums.append(str(num))
        if len(knums) == 0:
            print("INVALID RANGE")
            return
        print(" ".join(knums))
    
  • + 0 comments

    in c++ code hackerrank.com

    in engligh

    void kaprekarNumbers(int p, int q) { for(int i;i

  • + 0 comments

    Here is my python code (simple brute force):

    def kaprekarNumbers(p, q):
        arr = []
        for i in range(p, q + 1):
            curr = str(i ** 2)
            d1 = curr[:len(curr) // 2] if len(curr) > 1 else "0"
            d2 = curr[len(curr) // 2:] if len(curr) > 1 else curr
            if int(d1) + int(d2) == i:
                arr.append(str(i))
        print(" ".join(arr) if arr != [] else "INVALID RANGE")