Skip to main content

6174


I recently saw a Numberphile video about Kaprekar's constant. Kaprekar, an Indian mathematician defined a very simple yet very interesting iterative operation on a four digit number (with at least one distinct digit) which would always result in a constant (6174). It truly brings out the beauty in mathematics. 
This app takes a four digit number input and goes through the intermediary steps to finally output the constant.
The most significant part of the project was figure out an implementation to keep track of the leading zeros. It was a fun little friday night coding project. The workings of the operation is described in the following picture.



The algorithm:

// ......................... //
while (!number.equals("6174")) {
 ascending = ascending(number);
 decending = reverse(ascending);
 difference = Integer.parseInt(decending) - Integer.parseInt(ascending);
 String format = String.format("%%0%dd", 4);
 differenceString = String.format(format, difference);
 result = result + (count + ". " + (decending) + " - " + (ascending) + " = " + (differenceString) + "\n");
 count++;
 number = differenceString;
}
// ......................... //

Functions:


public static String ascending(String number) {
 char[] asc = number.toCharArray();
 Arrays.sort(asc);
 return (new String(asc));
}

public static String reverse(String ascendingNumber) {
 return (new StringBuilder(new String(ascendingNumber)).reverse().toString());
}

public static boolean valid(String number) {
 if (number.equals("")) {
  return false;
 }
 return (!(Integer.parseInt(number) % 1111 == 0) && number.length() == 4);
}

Popular posts from this blog

SalsaNight

Zorgania

The Zorganian Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family's wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Zorgania?
The ratio of girls to boys in Zorgania is 1:1. This might be a little counter-intuitive at first. Here are some ways of tackling this problem. 1. Monte Carlo Simulation: Although, Monte Carlo simulation does not necessarily show why the result is 1:1, it is appropriate because of the very counter-intuitive nature of the problem. At the very least, it helps us see that the result is indeed 1:1. Therefore, this is a good start.
The following R code estimates the probability of a child being a boy in Zorgania. 
couples <-100000 boycount <-0for (i in1:couples){ # 0: boywhile (sample(c(0,1),1) ==0) { boycount=boycount+1 } } probability <- boycount/(co…

Simple Launcher

A simple minimal launcher application for Android devices that shows battery percentage using lzyzsd's CirclProgress library (ArchProgress used in this case) and BroadcastReciever for battery state, Android's clock widgets, a built-in flash light switch and an app list view that can be toggled. Currently, the toggle simply filters all the app that I am working on at present. Future implementation can allow users to select their favorite apps or populate second toggle based on the most used applications.