Sort by

recency

|

1037 Discussions

|

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

    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") 
                
                    
    
  • + 0 comments
    g=[]
        for i in range(p,q+1):
          a=str(i**2)
          b=a[:len(a)//2]
          c=a[len(a)//2:]
          if c=='':
            c=0
          elif b=='':
            b=0
          if int(b)+int(c)==i:g.append(i)
        print('INVALID RANGE')if g==[]else print(*g)
    
  • + 0 comments

    def kaprekarNumbers(p, q): arr = [] if p == 1: arr.append(1) start = p if p >= 9 else 9 for i in range(start, q+1): left = str(i**2)[:-len(str(i))] right = str(i**2)[-len(str(i)):] if int(left) + int(right) == i: arr.append(i) print(*arr) if arr else print("INVALID RANGE")