Table of Contents
Description: The following programs finds out all the 4 digit Vampire numbers defined as follows:
What are Vampire numbers?
A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed.
Example include:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81
Primary Inputs: None
Primary Output: all the 4 digit Vampire numbers.
Platform Used: JDK 1.5 with Dr. Java.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package com.coddicted; class Vampire { void findVampire() { int arr[] = new int[4]; for (int i = 1001; i < 10000; i++) { arr = getDigitCombo(i); rotateCombo(arr, i, 1); } } boolean vampireStraightCombo(int[] arr, int num) { if (checkIfVampire(arr[0], arr[1], arr[2], arr[3], num)) { System.out.println(num + " = " + (arr[0] * 10 + arr[1]) + " * " + (arr[2] * 10 + arr[3])); return true; } else if (checkIfVampire(arr[0], arr[2], arr[1], arr[3], num)) { System.out.println(num + " = " + (arr[0] * 10 + arr[2]) + " * " + (arr[1] * 10 + arr[3])); return true; } else if (checkIfVampire(arr[0], arr[3], arr[1], arr[2], num)) { System.out.println(num + " = " + (arr[0] * 10 + arr[3]) + " * " + (arr[1] * 10 + arr[2])); return true; } return false; } boolean vampireReverseCombo(int[] arr, int num) { if (checkIfVampire(arr[3], arr[2], arr[1], arr[0], num)) { System.out.println(num + " = " + (arr[3] * 10 + arr[2]) + " * " + (arr[1] * 10 + arr[0])); return true; } else if (checkIfVampire(arr[3], arr[1], arr[2], arr[0], num)) { System.out.println(num + " = " + (arr[3] * 10 + arr[1]) + " * " + (arr[2] * 10 + arr[0])); return true; } else if (checkIfVampire(arr[3], arr[0], arr[2], arr[1], num)) { System.out.println(num + " = " + (arr[3] * 10 + arr[0]) + " * " + (arr[2] * 10 + arr[1])); return true; } return false; } boolean checkIfVampire(int a, int b, int c, int d, int num) { int num1 = a * 10 + b; int num2 = c * 10 + d; if ((num1 * num2) == num) return true; return false; } void rotateCombo(int[] arr, int num, int count) { if (count == 5) return; if (vampireStraightCombo(arr, num)) return; else if (vampireReverseCombo(arr, num)) return; else { arr = rotateArray(arr); rotateCombo(arr, num, count + 1); } } int[] rotateArray(int[] arr) { int[] temp = new int[4]; for (int i = 1; i < 4; i++) temp[i] = arr[i - 1]; temp[0] = arr[3]; return temp; } int[] getDigitCombo(int num) { int[] arr = new int[4]; for (int i = 3; i >= 0; i--) { arr[i] = num % 10; num = (num - num % 10) / 10; } return arr; } public static void main(String[] args) { System.out.println("The vampire numbers are: "); Vampire v = new Vampire(); v.findVampire(); } } |
One comment on “Vampire Numbers”