Sort by

recency

|

1033 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

    Haskell:

    Weird one.

    module Main where
    
    import Data.List (splitAt)
    
    test :: Int -> Bool
    test n = do
        let sn = show $ n ^ 2
        let len = length sn
        let (l, r) = splitAt (len `div` 2) sn
        let l' = if null l then 0 else read l :: Int
        let r' = if null r then 0 else read r :: Int
        n == l' + r'
    
    solve :: Int -> Int -> [Int]
    solve p q = filter test [p .. q]
    
    main :: IO ()
    main = do
        p <- readLn :: IO Int
        q <- readLn :: IO Int
        putStrLn $ do
            let ks = solve p q
            if null ks
                then "INVALID RANGE"
                else unwords $ map show $ solve p q
    
  • + 0 comments

    why 1 is consider kaperkar number ????

  • + 0 comments

    for Python3 Platform

    def kaprekarNumbers(p, q):
        flag = 0
        
        for i in range(p, q+1):
            d = 0
            temp = i
            
            while (temp > 0):
                d += 1
                temp = temp//10
            
            square = i*i
            
            if (square//pow(10, d) + square % pow(10, d) == i):
                flag = 1
                print(i, end=" ")
        
        if (flag == 0):
            print("INVALID RANGE")
    
    p = int(input())
    q = int(input())
    
    kaprekarNumbers(p, q)
    
  • + 0 comments

    my js solution

    function kaprekarNumbers(p, q) {
        // Write your code here
        let arr = []
        if(p <= 1) arr.push(1)
        for(let i = p; i <= q; i++){
            let square = String(i * i);
            let first = square.slice(0, square.length/2)
            let second = square.slice(square.length/2, square.length)
            parseInt(first) + parseInt(second) == i && arr.push(i)
        }
        arr.length > 0 ? console.log(arr.toString().split(',').join(' ')) : console.log("INVALID RANGE")
    }