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

  • Context:
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Numbers Range
Click For Summary

Discussion Overview

The discussion revolves around the implementation of an algorithm to find all Kaprekar numbers within a specified range [p, q]. Participants are analyzing a code solution and identifying issues related to the evaluation of specific test cases, focusing on the definition and properties of Kaprekar numbers.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code solution for identifying Kaprekar numbers and notes that it fails certain test cases.
  • Another participant points out the expected output for the range [1000, 10000] and highlights discrepancies in the output generated by the provided code.
  • One participant argues that their interpretation of the Kaprekar condition for the number 4879 is correct, citing the calculation of its square and the sum of its left and right parts.
  • A later reply challenges this interpretation, emphasizing that the right piece should have the same number of digits as the original number, suggesting that the left and right parts are incorrectly assigned in the original analysis.

Areas of Agreement / Disagreement

Participants express disagreement regarding the correct interpretation of the Kaprekar number definition, particularly in how to split the digits of the squared number into left and right parts. No consensus is reached on the validity of the outputs generated by the code.

Contextual Notes

There are unresolved aspects regarding the handling of digit splitting in the Kaprekar condition, and the discussion reflects varying interpretations of the definition as applied to specific examples.

SlurrerOfSpeech
Messages
141
Reaction score
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
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.
 
Wait ... what?

48792 = 23804641 and 238 + 04641 = 4879

Aren't I right?
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
55
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K