## Kaprekar Numbers

### September 21, 2010

Wolfram’s MathWorld describes Kaprekar numbers like this:

Consider an

n-digit numberk. Square it and add the rightndigits to the leftnorn-1 digits. If the resultant sum isk, thenkis called a Kaprekar number. For example, 9 is a Kaprekar number since 9^{2}= 81 and 8 + 1 = 9 and 297 is a Kaprekar number since 297^{2}= 88209 and 88 + 209 = 297.

Your task is to write a function that identifies Kaprekar numbers and to determine the Kaprekar numbers less than a thousand. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

#lang racket

(require (planet soegaard/math/math))

(for/fold ([result ‘()])

([x (in-range 1000)])

(let ([d (digits (* x x))])

(let-values ([(left right) (split-at d (quotient (length d) 2))])

(if (equal? x (+ (digits->number left)

(digits->number right)))

(cons x result)

result))))

This is a good problem for python.

def kaprekar(n):

l = str(n * n)

length = len(l)

return n == int(l[0:length/2]) + int(l[length/2:])

for i in range(4,10000000):

if kaprekar(i):

print i

A simple ruby version …

A haskell solution

which gives

*Main> kaprekarLessThan 1000

[1,9,45,55,99,297,703,999]

And yes, yes I did misspell Kaprekar.

Answered in Python, with some pre-computating values and working only with digits (as opposed to strings) for speed:

Oops! Here we go:

ruby-1.9.2-p0 > require "kaprekar"

=> true

ruby-1.9.2-p0 > kaprekar? 9

=> true

ruby-1.9.2-p0 > kaprekar? 297

=> true

ruby-1.9.2-p0 > kaprekar? 50

=> false

ruby-1.9.2-p0 > kaprekar_to 1000

=> [1, 9, 45, 55, 99, 297, 703, 999]

;; First time I ever use the prelude…

(define (kaprekar? n)

(let* ((n2 (ipow n 2))

(size (+ 1 (ilog 10 n2)))

(size-l (if (even? size) (/ size 2) (/ (- size 1) 2)))

(size-r (if (even? size) size-l (+ 1 size-l))))

(let* ((right (modulo n2 (ipow 10 size-r)))

(left (/ (- n2 right) (ipow 10 size-r))))

(= n (+ left right)))))

(define (kaprekar n)

(filter kaprekar? (range 0 n)))

;; err, that was the wrong version.

(define (kaprekar? n)

(let* ((n2 (ipow n 2))

(size (+ 1 (ilog 10 n2)))

(size-r (/ (if (even? size) size (+ 1 size)) 2)))

(let* ((mask (ipow 10 size-r))

(right (modulo n2 mask))

(left (/ (- n2 right) mask)))

(= n (+ left right)))))

Common Lisp solution. No strings manipulation, but arithmetic (log, expt, ceiling, floor).

Same as model solution in scheme.

A soultion in Haskell using Data.Digits

Anyone have an idea, how to implement it without reversing the list twice?

Hackish code will come back to it when I have time.

Mine in F #

C Implementation

This is in python:

[…] Below is my solution to the Programming Praxis problem from Sept. 21, 2010. The problem was to find all Kaprekar numbers less than 1000. I wrote my solution in C++ to refresh my skills a little. For more info, see this link. […]

[…] A few days ago I attempted to put my Ruby skills to use, given a programming puzzle centred on Kaprekar numbers. […]

Finally, I have understood kaprekar number. Thanks guys

C++:

using namespace std;

bool isKep(int num)

{

int n = num;

int digit = 1;

int right = 0;

int squere = n*n;

while (n)

{

right += digit*(squere%10);

n /= 10;

squere /= 10;

digit *= 10;

}

if (squere+right == num) {return true;}

else {return false;}

}

int main()

{

for (int i=1;i<1000;i++){

if (isKep(i)) {cout << i << " ";}

}

return 0;

}