diff options
| author | Syndamia <kami02882@gmail.com> | 2019-07-29 11:46:36 +0300 |
|---|---|---|
| committer | Syndamia <kami02882@gmail.com> | 2019-07-29 11:46:36 +0300 |
| commit | bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23 (patch) | |
| tree | c66cebc02aac30ff859c06ca462f3dd58b6809b0 /Java/Beginer training/src/VampireNumber.java | |
| parent | 65edf7296baf48aad1b4e0c09b57f1a7f48791a8 (diff) | |
| download | Self-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.tar Self-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.tar.gz Self-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.zip | |
Did some more work in Python and started officially learning Java
Diffstat (limited to 'Java/Beginer training/src/VampireNumber.java')
| -rw-r--r-- | Java/Beginer training/src/VampireNumber.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Java/Beginer training/src/VampireNumber.java b/Java/Beginer training/src/VampireNumber.java new file mode 100644 index 0000000..63d7085 --- /dev/null +++ b/Java/Beginer training/src/VampireNumber.java @@ -0,0 +1,45 @@ +public class VampireNumber { + public static void main(String args[]){ + //NOTE: Works ONLY with 4 digit numbers, for bigger use BigVampireNumber + System.out.println(FindVampireNumber(6880)); + } + + public static String FindVampireNumber(int result){ + String num = result + ""; + int first = num.charAt(0) - 48, second = num.charAt(1) - 48, third = num.charAt(2) - 48, fourth = num.charAt(3) - 48; + + for (int i = 0; i < 4; i++){ + for (int j = 0; j < 3; j++){ + int first_half = Combine(first, second), second_half = Combine(third, fourth); + int swapped_first_half = Combine(first, fourth), swapped_second_half = Combine(third, second); + + if (first_half * second_half == result) { + return result + " = " + first_half + " * " + second_half; + } + else if (swapped_first_half *swapped_second_half == result){ + return result + " = " + swapped_first_half + " * " + swapped_second_half; + } + + switch (j) { + case 0: + case 1: + int temp = fourth; + fourth = second; second = temp; + temp = third; + third = fourth; fourth = temp; + + break; + case 2: + temp = second; + second = first; first = temp; + break; + } + } + } + return result + " isn't a vampire number"; + } + + private static int Combine(int first, int second) { + return (first * 10) + second; + } +} |
