Sort by

recency

|

1050 Discussions

|

  • + 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";
    }
    
  • + 0 comments
    def kaprekarNumbers(p, q):
        # Write your code here
        exists = False
        for n in range(p, q+1):
            d = len(str(n))
            n_quare = str(n * n)
            left, right = n_quare[:-d], n_quare[-d:]
            
            if int(left or 0) + int(right) == n:
                print(n, end=" ")
                exists = True
        if not exists:
            print("INVALID RANGE")
    
  • + 0 comments

    Using solution of yashdeora98294 makes all the tests passed. However, it looks like there are errors. For example, the function shows that 4879 is not Kaprekar number while wikipedia say it is. To be more specific: 4879*4879 = 23804641 and 238 + 4641 = 4879