Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Vampire Java

By: a guest on Jul 17th, 2013  |  syntax: Java  |  size: 0.99 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class Vampire {
  2.  
  3.     public static void main(String[] args) {
  4.         printVampires(8);
  5.     }
  6.  
  7.     private static void printVampires(int n) {
  8.         int start = (int) Math.pow(10, n-1);
  9.         int end = start * 10;
  10.         int low = (int) Math.pow(10, (n/2) - 1);
  11.         for (int i=start; i<end; i++) {
  12.             int high = (int) Math.sqrt((double)i);
  13.             for (int j=low; j<=high; j++) {
  14.                 if (i % j == 0) {
  15.                     int k = i / j;
  16.                     if (areVampires(i, j, k)) {
  17.                         System.out.println(i + " = " + j + " x " + k);
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.     }
  23.    
  24.     private static boolean areVampires(int i, int j, int k) {
  25.         char[] vamp = Integer.toString(i).toCharArray();
  26.         char[] fangs = (Integer.toString(j) + Integer.toString(k)).toCharArray();
  27.         Arrays.sort(vamp);
  28.         Arrays.sort(fangs);
  29.         return Arrays.equals(vamp, fangs);
  30.     }
  31. }
clone this paste RAW Paste Data