Sort by

recency

|

1046 Discussions

|

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

  • + 0 comments

    Hello all! This is my code and it is giving the expected output(I run it in IDLE shell). But I don't know why some testcase(4/7) failed while submitting it in Hackerrank editor. If you spot any mistakes in my logic do comment.

    result=[]
    p=int(input())
    q=int(input())
    for i in range(p,q+1):
        if i==1:
            result.append(i)
        else:
            sq=i*i
            sq=str(sq)
            length=len(sq)
            if length>1:
                sq=list(sq)
                l=''
                r=''
                for j in range(length//2):
                    l,sq[j]=l+sq[j],''
                for k in range(length):
                    r=r+sq[k]
                l,r=int(l),int(r)
                if l+r==i:
                    result.append(i)
                else:
                    for m in range(len(str(l))-1):
                        l=l/10
                        if l+r==i:
                            result.append(i)
                            break               
    for i in result:
        print(i,end=' ')
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-modified-kaprekar-numbers-problem-solution.html

  • + 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
        m=[]
        for i in range(p,q+1):
            sq=str(i**2)
            if(len(sq)>1):
                if(int(sq[:len(sq)//2])+int(sq[len(sq)//2:])==i):
                    m.append(str(i)) 
            elif(i==1):
                m.append(str(i))
        print(" ".join(m) if m != [] else "INVALID RANGE")