Sort by

recency

|

1041 Discussions

|

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

    JavaCode

    public static void kaprekarNumbers(int p, int q) {
            List<Integer> list = new ArrayList<>();
            for (int i = p; i <= q; i++) { 
                long sqr = (long) i * i;
                String str = Long.toString(sqr);
                int len = str.length();
                int numLen = Integer.toString(i).length();
                
                String r = str.substring(len - numLen);    
                String l = str.substring(0, len - numLen);  
                if (l.isEmpty()) {
                    l= "0";
                }
                int sum = Integer.parseInt(l) + Integer.parseInt(r);
                if (sum == i) {
                    list.add(i);
                }
            }
            if (list.isEmpty()) {
                System.out.print("INVALID RANGE");
            } else {
                for (int num : list) {
                    System.out.print(num + " ");
                }
            }
        }