Sunday, January 2, 2011

Kaprekar-Numbers

Kaprekar-numbers are interesting. For eg.: 703 is a kaprekar-number. Reason: 7032 = 494209 and 494 + 209 = 703. It's also been proven that one can get all the Kaprekar-numbers by prime factorization of 10n-1. However, I wanted to list all the Kaprekar-numbers by solving it in its pure form! A number 'k' (in base 10) is called as Kaprekar-number if it can be expressed in the form:
k = q + r   and   k2 = q * 10n + r
Where, q >= 1, 0 < r < 10n
I've hosted this program on github at the following location (read-only): git://github.com/teju85/Kaprekar-Numbers.git. If you are interested in this, please download the program and follow the instructions inside the 'README'.
I profiled this program and here are the results:
$ make profile
printCpuInfo.sh
Processor Information:
processor       : 0
vendor_id       : GenuineIntel
model name      : Intel(R) Xeon(R) CPU           X5355  @ 2.66GHz

kaprekarnumber -nooutput -profile -limit 10000
Found 17 kaprekar-numbers among first '10000' integers.
Took 0.001309 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 100000
Found 24 kaprekar-numbers among first '100000' integers.
Took 0.014230 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 1000000
Found 54 kaprekar-numbers among first '1000000' integers.
Took 0.188414 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 10000000
Found 62 kaprekar-numbers among first '10000000' integers.
Took 1.616492 seconds to search for self-numbers among these integers.

Not sure whether the timings are 'optimal' or not. Still looking for possible optimizations...

No comments:

Post a Comment