Sort by

recency

|

1039 Discussions

|

  • + 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 + " ");
                }
            }
        }
    
  • + 0 comments

    Hi, this is my solution in C# 🐿️

        public static void kaprekarNumbers(int p, int q)
        {
            List<int> numbers = new List<int>();
            for (int i = p; i <= q; i++)
            {
                var digits = i.ToString().ToCharArray().Count();
                var square = Math.Pow(i, 2);
                var square_digits = square.ToString().ToCharArray().Count();
                int left = 0;
                int right = 0;
                if (square_digits > 1)
                {
                    int digitsForLeft = square_digits - digits;
                    left = Convert.ToInt32(square.ToString().Substring(0, digitsForLeft));
                    right = Convert.ToInt32(square.ToString().Substring(digitsForLeft));
                }
                else
                {
                    right = (int)square;
                }
                if (left + right == i)
                    numbers.Add(i);
            }
            Console.WriteLine(numbers.Count > 0 ? string.Join(" ", numbers) : "INVALID RANGE");
        }
    
  • + 0 comments

    Solution of Kaprekar Numbers in Python :

    def kaprekarNumbers(p, q):
        # Write your code here
        li=[]
        for i in range(p , q+1):
            if(i ==1): 
                li.append(i)
                    
            elif(i == 2 or i==3 ):
                continue
    
            elif(i>3):
                num= i*i
                digitsize = len(str(i))
                strnum=str(num)
                leftsize =  len(strnum)-digitsize
                left = int(strnum[:leftsize])
                right= int(strnum[leftsize:])
                add = left + right
                if( add == i ):
                    li.append(i)
                                         
        if(len(li) >0):
            for i in range(0,len(li)):
                print(li[i] ,end=" ")
        else:
            print("INVALID RANGE")