kaprekar number calculation fails for more than two digits


kaprekar number calculation fails for more than two digits



why is the code not working? i cant get a valid output. Please help me to find whether the input is a Kaprekar number or not.I think i made it too complicated. Any suggestions for a simple method??



For example:



Here is the code:


#include <stdio.h>

int main() {
int n, a, temp, length, sum = 0, s, i, rem, temp1, length1, sum1[10], sum2[10];

printf("n");
scanf("%d", &n);

a = n * n;
printf("%dn", a);

temp = a;
length = 0;

while (temp > 0) {
length++;
temp = temp / 10;
}

temp1 = n;
length1 = 0;

while (temp1 > 0) {
length1++;
temp1 = temp1 / 10;
}

if (length1 == 1) {
for (i = 0; i < length; i++) {
rem = a % 10;
sum = sum + rem;
a = a / 10;
}
if (sum == n) {
printf("Kaprekar number");
} else {
printf("not a kaprekar number");
}
}

if (length1 > 1) {
s = length / 2;

for (i = 0; i < s; i++) {
printf("%dn", sum1[i]);
}

for (i = s; i < length; i++) {
printf("%dn", sum2[i]);
}

for (i = 0; i < length; i++) {
sum3[i] = sum1[i] + sum2[i];
}
}
return 0;
}





Please format your code. It literally makes no sense to look at...
– DeiDei
Jun 30 at 13:56





@DeiDei In the context of stackoverflow.blog/2018/04/26/… it is appreciated to tell new posters about formatting and indentation after fixing it for them. There is a "try your best, we will help you with the rest" kind of thinking.
– Yunnosch
Jun 30 at 14:00





El Khan A decent indentation is really helping with understanding your code and thereby with helping you. Also, it is in your interest to keep potential answerers in as good a mood as possible.
– Yunnosch
Jun 30 at 14:01





Can you provide a definition of a kaprekar number? You gave examples, but that is not sufficient for making or understanding an algorithm.
– Yunnosch
Jun 30 at 14:02





What is sizeof(int) in your environment? If it is 2, then you probably just have a range problem. In that case try using long int.
– Yunnosch
Jun 30 at 14:11


sizeof(int)


long int




3 Answers
3



Here's my suggestion to simplify your code, and hopefully make it easier to debug.


int getNumberOfDigits(int n)
{
if ( n == 0 )
{
return 1;
}

int count = 0;
while ( n / 10 > 0 )
{
n /= 10;
++count;
}

return count;
}

void divideSquare(int square, int originalDigitCount, int out[2])
{
int left = square;
int right = 0;
for ( int = 0; i < originalDigitCount; ++i )
{
right = right*10 + left%10;
left /= 10;
}
out[0] = left;
out[1] = right;
}

int main()
{
for (int n = 0; n < 10000; ++n )
{
int square = n * n;
int out[2];
int count = getNumberOfDigits(n);
divideSquare(square, count, out);
if ( out[0] + out[1] == n )
{
printf("%d is a Kaprekar numbern", n);
}
}
}



According to this Wikipedia article, Kaprekar numbers are defined this way:



In mathematics, a non-negative integer is called a Kaprekar number for a given base if the representation of its square in that base can be split into two parts that add up to the original number, with the proviso that the part formed from the low-order digits of the square must be non-zero, although it is allowed to include leading zeroes. For instance, 45 is a Kaprekar number, because 452 = 2025 and 20 + 25 = 45. The number 1 is Kaprekar in every base, because 12 = 01 in any base, and 0 + 1 = 1. Kaprekar numbers are named after D. R. Kaprekar.



Your method seems too complicated and broken beyond repair. Here is a simpler approach:


unsigned long long



Here is a simple implementation:


#include <stdio.h>

int test_kaprekar(unsigned long long n) {
unsigned long long quo, rem, pow10, square = n * n;
int i;

for (i = 1, pow10 = 10;; i++, pow10 *= 10) {
quo = square / pow10;
rem = square % pow10;
if (quo + rem == n && rem != 0) {
printf("%llu is a Kaprekar number: %llu*%llu = %llu, %llu+%.*llu = %llun",
n, n, n, square, quo, i, rem, n);
return 1;
}
if (quo < 10)
break;
}
return 0;
}

int main() {
unsigned long long i, start = 0, stop = 4294967295, count = 0;
for (i = start; i <= stop; i++) {
count += test_kaprekar(i);
}
printf("%llu Kaprekar numbers between %llu and %llun", count, start, stop);
return 0;
}



Output:


1 is a Kaprekar number: 1*1 = 1, 0+1 = 1
9 is a Kaprekar number: 9*9 = 81, 8+1 = 9
45 is a Kaprekar number: 45*45 = 2025, 20+25 = 45
55 is a Kaprekar number: 55*55 = 3025, 30+25 = 55
99 is a Kaprekar number: 99*99 = 9801, 98+01 = 99
297 is a Kaprekar number: 297*297 = 88209, 88+209 = 297
703 is a Kaprekar number: 703*703 = 494209, 494+209 = 703
999 is a Kaprekar number: 999*999 = 998001, 998+001 = 999
2223 is a Kaprekar number: 2223*2223 = 4941729, 494+1729 = 2223
2728 is a Kaprekar number: 2728*2728 = 7441984, 744+1984 = 2728
4879 is a Kaprekar number: 4879*4879 = 23804641, 238+04641 = 4879
4950 is a Kaprekar number: 4950*4950 = 24502500, 2450+2500 = 4950
5050 is a Kaprekar number: 5050*5050 = 25502500, 2550+2500 = 5050
5292 is a Kaprekar number: 5292*5292 = 28005264, 28+005264 = 5292
7272 is a Kaprekar number: 7272*7272 = 52881984, 5288+1984 = 7272
7777 is a Kaprekar number: 7777*7777 = 60481729, 6048+1729 = 7777
9999 is a Kaprekar number: 9999*9999 = 99980001, 9998+0001 = 9999
17344 is a Kaprekar number: 17344*17344 = 300814336, 3008+14336 = 17344
22222 is a Kaprekar number: 22222*22222 = 493817284, 4938+17284 = 22222
38962 is a Kaprekar number: 38962*38962 = 1518037444, 1518+037444 = 38962
77778 is a Kaprekar number: 77778*77778 = 6049417284, 60494+17284 = 77778
82656 is a Kaprekar number: 82656*82656 = 6832014336, 68320+14336 = 82656
95121 is a Kaprekar number: 95121*95121 = 9048004641, 90480+04641 = 95121
99999 is a Kaprekar number: 99999*99999 = 9999800001, 99998+00001 = 99999
...
86358636 is a Kaprekar number: 86358636*86358636 = 7457814011780496, 74578140+11780496 = 86358636
88888888 is a Kaprekar number: 88888888*88888888 = 7901234409876544, 79012344+09876544 = 88888888
91838088 is a Kaprekar number: 91838088*91838088 = 8434234407495744, 84342344+07495744 = 91838088
94520547 is a Kaprekar number: 94520547*94520547 = 8934133805179209, 89341338+05179209 = 94520547
99999999 is a Kaprekar number: 99999999*99999999 = 9999999800000001, 99999998+00000001 = 99999999
234567901 is a Kaprekar number: 234567901*234567901 = 55022100179545801, 55022100+179545801 = 234567901
243902440 is a Kaprekar number: 243902440*243902440 = 59488400237953600, 5948840+0237953600 = 243902440
332999667 is a Kaprekar number: 332999667*332999667 = 110888778222110889, 110888778+222110889 = 332999667
432432432 is a Kaprekar number: 432432432*432432432 = 186997808245434624, 186997808+245434624 = 432432432
567567568 is a Kaprekar number: 567567568*567567568 = 322132944245434624, 322132944+245434624 = 567567568
665188470 is a Kaprekar number: 665188470*665188470 = 442475700620940900, 44247570+0620940900 = 665188470
667000333 is a Kaprekar number: 667000333*667000333 = 444889444222110889, 444889444+222110889 = 667000333
765432099 is a Kaprekar number: 765432099*765432099 = 585886298179545801, 585886298+179545801 = 765432099
867208672 is a Kaprekar number: 867208672*867208672 = 752050880792003584, 75205088+0792003584 = 867208672
909090909 is a Kaprekar number: 909090909*909090909 = 826446280826446281, 82644628+0826446281 = 909090909
999999999 is a Kaprekar number: 999999999*999999999 = 999999998000000001, 999999998+000000001 = 999999999
1111111111 is a Kaprekar number: 1111111111*1111111111 = 1234567900987654321, 123456790+0987654321 = 1111111111
1776299581 is a Kaprekar number: 1776299581*1776299581 = 3155240201460775561, 315524020+1460775561 = 1776299581
2020202020 is a Kaprekar number: 2020202020*2020202020 = 4081216201612080400, 408121620+1612080400 = 2020202020
2646002646 is a Kaprekar number: 2646002646*2646002646 = 7001330002639001316, 7001330+002639001316 = 2646002646
3846956652 is a Kaprekar number: 3846956652*3846956652 = 14799075482367049104, 1479907548+2367049104 = 3846956652
3888938889 is a Kaprekar number: 3888938889*3888938889 = 15123845682376554321, 1512384568+2376554321 = 3888938889
4090859091 is a Kaprekar number: 4090859091*4090859091 = 16735128102417346281, 1673512810+2417346281 = 4090859091
4132841328 is a Kaprekar number: 4132841328*4132841328 = 17080377442424803584, 1708037744+2424803584 = 4132841328
110 Kaprekar numbers between 0 and 4294967295



All powers of 10 minus 1 are Kaprekar numbers in base 10, other Kaprekar numbers are quite rare around log10(n) between 10n and 10n+1.



There are a few mistakes with your code, for once you haven't initialized arrays of sum1 and sum2 which you use for printing and adding. secondly, array sum3 isn't defined. also if your numbers are more than 2,147,483,647 which means you get an overflow if a is more than about 46340.


sum1


sum2


sum3


2,147,483,647


a



I was interested in the problem, so here's my implementation


#include <stdio.h>
#include <math.h>

int get_length(long long n){
int s = 0;
while (n>0){
s++;
n /= 10;
}
return s;
}

int is_kaprekar (long long n){
long long t, n2=n*n;
int i,s = get_length(n2);
t = pow(10,s);
for (i=0 ; i<s ; i++){
long long left,right;
left = n2/t;
right = n2%t;
if ((long long)(left+right) == n && left != t){
printf("%lldn",n); // print n
printf("%lldn",n2); // print n^2
printf("%lldn",left);
printf("%lldn",right);
printf("Kaprekar numbern");
return 1;
}
t/=10;
}
printf("not a kaprekar number");
return 0;
}

int main()
{
long long n,i,n2;
printf("n ");
scanf("%d",&n); // get n
is_kaprekar(n);

return 0;
}





For 297 it has printed as not a kaprekar number but it is actually a kaprekar number.I think something gone wrong with the right_half part.could you please check on this??
– El Khan
Jun 30 at 16:04





@ElKhan Are you sure you ran this code? it's printing right_half and left_half correctly for me and answers Kaprekar number
– Alaleh A
Jun 30 at 16:13


Kaprekar number





@ElKhan by your description in the question I assumed you should split the number in half, but by definition you can split it from anywhere! my previous code didn't work for some above 1000 numbers, the edit fixes it.
– Alaleh A
Jun 30 at 16:30





Your program invokes undefined behavior at printf("%dn",n2); because n2 is a long long. It should be printf("%lldn",n2);.
– Pascal Cuoq
Jun 30 at 18:33


printf("%dn",n2);


n2


long long


printf("%lldn",n2);





weirdly it invoked no error in my compiler, thanks I fixed it
– Alaleh A
Jun 30 at 18:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”