Created
December 8, 2011 15:21
-
-
Save spangenberg/1447274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Untitled { | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | |
| Integer number = scanner.nextInt(); | |
| kaprekar(number); | |
| } | |
| public static boolean kaprekar(Integer numberInteger) { | |
| String numberString = numberInteger.toString(); | |
| int[] digits = new int[4]; | |
| for (int i = 0; i < 4; i++) { | |
| digits[i] = Integer.parseInt("" + numberString.charAt(i)); | |
| } | |
| Arrays.sort(digits); | |
| String smallestString = "" + digits[0] + digits[1] + digits[2] + digits[3], | |
| biggestString = "" + digits[3] + digits[2] + digits[1] + digits[0]; | |
| int smallest = Integer.parseInt(smallestString), | |
| biggest = Integer.parseInt(biggestString); | |
| int difference = biggest - smallest; | |
| System.out.println("Differenz: " + difference); | |
| if (difference == 6174) { | |
| return true; | |
| } else { | |
| return kaprekar(difference); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment