about summary refs log tree commit diff
path: root/daily/287easy/kaprekar.c
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2016-10-13 09:44:32 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2016-10-13 09:44:32 +0700
commit8754e6d5a13072eaa7647ad03c71614d5ec0cc0a (patch)
tree69ebaa8c0e7663d1bea7b013841b8f9440a31f8c /daily/287easy/kaprekar.c
parentcf7269205b346cf16b35d258989bb19819427e9b (diff)
downloadcp-8754e6d5a13072eaa7647ad03c71614d5ec0cc0a.tar.gz
daily/287easy/kaprekar.{c,pas}: Use more maths for a cleaner (?) solution
Diffstat (limited to 'daily/287easy/kaprekar.c')
-rw-r--r--daily/287easy/kaprekar.c13
1 files changed, 4 insertions, 9 deletions
diff --git a/daily/287easy/kaprekar.c b/daily/287easy/kaprekar.c
index 3121d88..0cf1bf6 100644
--- a/daily/287easy/kaprekar.c
+++ b/daily/287easy/kaprekar.c
@@ -40,19 +40,14 @@ short desc_digits(short n)
 	return a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
 }
 
-short asc_digits(short n)
-{
-	sort_digits(n);
-
-	return a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
-}
-
 unsigned long long kaprekar(short n)
 {
 	char i;
 
-	for (i = 0; n ^ 6174; i++)
-		n = desc_digits(n) - asc_digits(n);
+	for (i = 0; n ^ 6174; i++) {
+		sort_digits(n);
+		n = (a[0] - a[3]) * 999 + (a[1] - a[2]) * 90;
+	}
 
 	return i;
 }