Sort by

recency

|

1052 Discussions

|

  • + 0 comments

    Here is problem solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-modified-kaprekar-numbers-problem-solution.html

  • + 0 comments
    found = False
        for i in range(p, q+1):
            new = str(i*i)
            right = new[:-len(str(i))] or 0
            left = new[-len(str(i)):]
            
            if int(left) + int(right) == i:
                print(i, end=" ")
                found = True
                
        if not found:
            print("INVALID RANGE")
    
  • + 0 comments
    def kaprekarNumbers(p, q):
        # Write your code here
        list1=[]
        for i in range(p,q+1):
            x=len(str(i))
            y=str(i**2)
            left=y[:len(y)-x]
            right=y[len(y)-x:]
    
            if left=="":
                left="0"
    
            z=int(left)+int(right)
            if z==i:
                list1.append(i)
        if list1:
            for i in list1:
                print(i, end=" ")
        else:
            print("INVALID RANGE")
    
  • + 0 comments

    My solution, if you see any improvements in the code, plase let me know:

    def kaprekarNumbers(p, q):
        count = 0
        for i in range(p,q+1):
            op = str(i**2)
            count_digit = str(i)
            count_digit = len(count_digit)
            right = op[-count_digit:]
            left = op[:(len(op)-count_digit)]
    
            if len(right)== 0:
                left = int(left)
                right = 0
            elif len(left)== 0:
                right = int(right)
                left=0    
            else:    
                right = int("".join(str(num) for num in right))
                left = int("".join(str(num) for num in left))
    
            if right + left == i:
                print(i,end=' ')
                count+=1
    
        if count == 0:
            print('INVALID RANGE')   
    
  • + 0 comments
    void kaprekarNumbers(int p, int q) {
        bool found = false;
    
        for (int i = p; i <= q; i++) {
            long long sq = 1LL * i * i;
            string num_str = to_string(sq);
    
            int len = num_str.length();
            int mid = len / 2;
    
            long long left = (mid == 0) ? 0 : stoll(num_str.substr(0, mid));
            long long right = stoll(num_str.substr(mid));
    
            if (left + right == i) {
                found = true;
                cout << i << " ";
            }
        }
    
        if (!found) cout << "INVALID RANGE";
    }