Finding all Kaprekar numbers in the range [p, q]

In summary, the conversation discusses a solution for the Kaprekar Numbers problem on HackerRank and identifies an issue with the algorithm for certain test cases. The main problem is that the algorithm is incorrectly evaluating some numbers as Kaprekar when they should not be, due to not following the necessary criteria for the right and left pieces.
  • #1
SlurrerOfSpeech
141
11
What am I doing wrong here? The prompt is https://www.hackerrank.com/challenges/kaprekar-numbers and my well-commented solution is

Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
    // Returns the number represented by the digits in the array
    // within the range of indices [i, j)
    static long NumberInRange(long[] arr, long i, long j)
    {
        long result = 0;
        for(; i < j; ++i)
        {
            result *= 10;
            result += arr[i];
        }
        return result;
    }
   
    // Returns true or false depending on whether k is Kaprekar
    static bool IsKaprekar(int k)
    {
        // Must use long to hold square of ints
        long square = k * k;
       
        // Get all digits of square in an array of longs
        long[] digits = square.ToString().Select(c => (long)Char.GetNumericValue(c)).ToArray();
       
        // Edge case: If there is 1 or fewer digits, return true or false depending on whether
        // the square is equal to the original number.
        if(digits.Length < 2)
            return square == k;
       
        // Get all combinations of array divided into non-empty left and right sides. 
        // Return early with true if the sum of the left and right equal k. 
        for(int i = 1; i < digits.Length; ++i)
        {
            // Get number represented by digits in the range of indices [0,i)
            long left = NumberInRange(digits, 0, i);
           
            // Get number represented by digits in the range of indices [i, digits.Length)
            long right = NumberInRange(digits, i, digits.Length);
           
            // "By convention, the second part may start with the digit 0, but must be nonzero."
            // https://en.wikipedia.org/wiki/Kaprekar_number
            if(right == 0)
                continue;
           
            // If made it here, we can do the check.
            if((right + left) == k)
                return true;
        }
       
        // If made it here, k is not Kaprekar
        return false;
    }
   
    static void Main(String[] args)
    {
        int p = int.Parse(Console.ReadLine());
        int q = int.Parse(Console.ReadLine());
        IEnumerable<int> kaprekars = Enumerable.Range(p, q - p + 1).Where(k => IsKaprekar(k));
        Console.WriteLine(kaprekars.Any() ? string.Join(" ", kaprekars) : "INVALID RANGE");
    }
}

I am passing most of the test cases but am failing the following 3.

(1)

1000
10000

(2)

1
10000

(3)

1
99999
 
Technology news on Phys.org
  • #2
Ah, ok. On the [1000,10000] one the expected output is

Code:
2223 2728 4950 5050 7272 7777 9999

and my output is

Code:
2223 2728 4879 4950 5050 5292 7272 7777 9999

So my algorithm is incorrectly evaluating 4879 and 5292 as Kaprekar, for example.
 
  • #3
Wait ... what?

48792 = 23804641 and 238 + 04641 = 4879

Aren't I right?
 
  • #4
SlurrerOfSpeech said:
Wait ... what?

48792 = 23804641 and 238 + 04641 = 4879

Aren't I right?

You seem to be trying all the possible sizes for the left and right pieces. The definition on the website says that the right piece should have the same number of digits ##d## as the number ##n## itself, and the left piece is made up of the digits left over. 4879 is a four-digit number, so that means you should take the right piece to be 4641 and the left piece as 2380.
 

1. What is a Kaprekar number?

A Kaprekar number is a special type of number where its square can be split into two parts that when added together, equal the original number. For example, 45 is a Kaprekar number because its square (2025) can be split into 20 and 25, which when added together, equal the original number (45).

2. How many Kaprekar numbers are there in a given range?

This can vary depending on the range, but generally there are a finite number of Kaprekar numbers in any given range. For example, in the range of 1 to 100, there are 9 Kaprekar numbers (1, 9, 45, 55, 99, 297, 703, 999, 2223).

3. How do you find all Kaprekar numbers in a given range?

To find all Kaprekar numbers in a given range, you can use a simple algorithm. First, square all the numbers in the range. Then, split each square into two parts and add them together to see if they equal the original number. If they do, then that number is a Kaprekar number. Repeat this process for all numbers in the range to find all the Kaprekar numbers.

4. Can Kaprekar numbers be negative?

No, Kaprekar numbers are always positive integers. This is because squaring a negative number would result in a positive number, and the algorithm to find Kaprekar numbers only works with positive integers.

5. What is the significance of Kaprekar numbers?

Kaprekar numbers have been studied and named after Indian mathematician D. R. Kaprekar. They have interesting properties and have been used in puzzles and games. They also have applications in number theory and can be used to generate other interesting sequences of numbers.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
22
Views
755
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
872
  • Programming and Computer Science
Replies
10
Views
1K
Back
Top