fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. /**
  11.   *
  12.   * @param numberOfDigits {int}
  13.   * @return ArrayList of Integer
  14.   */
  15. public ArrayList<Integer> vdf(int numberOfDigits) {
  16.  
  17. if ((numberOfDigits % 2) == 1) {
  18. //or throw Exception of unrecognised format/variable?
  19. System.out.println("cant operate on odd argument");
  20. return new ArrayList<>();
  21. }
  22. long maxRange = 9;
  23.  
  24. for (int i = 1; i < numberOfDigits; i++) {
  25. maxRange *= 10;
  26. maxRange += 9;
  27. }//numberOfDigits==4 then maxRange==9999, nOD==5 then maxRange==99999,..
  28.  
  29. long minRange = 1;
  30.  
  31. for (int i = 1; i < numberOfDigits; i++) {
  32. minRange *= 10;
  33. }//nOD==4 then minRange==1000, nOD==5 then minRange==10000, ..
  34.  
  35. ArrayList<Integer> ret = new ArrayList<>();
  36. for (long i = minRange; i < maxRange; i++) {
  37.  
  38. long a = i;
  39.  
  40. long[] b = new long[numberOfDigits];
  41.  
  42. for (int j = numberOfDigits-1; j >= 0 ; j--) {
  43. long c = a % 10;
  44. a = a / 10;
  45. b[j] = c;
  46. }
  47.  
  48. int x = 0;
  49. int y = 0;
  50. ArrayList<long[]> list = permutations(b);
  51. b = null; //dont need now
  52.  
  53. for(long[] s : list) {
  54. for (int j = 0; j < numberOfDigits/2; j++) {
  55. x += s[(numberOfDigits/2)-j-1] * Math.pow(10, j);
  56. y += s[numberOfDigits-j-1] * Math.pow(10, j);
  57. }
  58. StringBuilder builder = new StringBuilder();
  59. for (long t : s) {
  60. builder.append(t);
  61. }
  62. String v = builder.toString();
  63.  
  64. if ((v.charAt((v.length()/2)-1) != '0'||
  65. v.charAt(v.length()-1) != '0') &&
  66. x * y == i) {
  67. ret.add(x);
  68. ret.add(y);
  69. System.out.println(x*y+" "+x+" "+y);
  70. break;
  71. }
  72. x = y = 0;
  73. }
  74. }
  75. System.out.printf("%d vampire numbers found\n", ret.size()/2);
  76. return ret;
  77. }
  78.  
  79. /**
  80.   *
  81.   *@return vdf(4)
  82.   */
  83. public ArrayList<Integer> vdf() {
  84. return vdf(4);//without trailing zeros
  85. }
  86.  
  87. /* permutation code copied from
  88.   * johk95
  89.   * http://stackoverflow.com/a/20906510
  90.   */
  91. private static ArrayList<long[]> permutations(long[] lol) {
  92. ArrayList<long[]> ret = new ArrayList<>();
  93. permutation(lol, 0, ret);
  94. return ret;
  95. }
  96.  
  97. private static void permutation(long[] arr, int pos, ArrayList<long[]> list){
  98. if(arr.length - pos == 1)
  99. list.add(arr.clone());
  100. else
  101. for(int i = pos; i < arr.length; i++){
  102. swap(arr, pos, i);
  103. permutation(arr, pos+1, list);
  104. swap(arr, pos, i);
  105. }
  106. }
  107.  
  108. private static void swap(long[] arr, int pos1, int pos2){
  109. long h = arr[pos1];
  110. arr[pos1] = arr[pos2];
  111. arr[pos2] = h;
  112. }
  113.  
  114. public static void main(String[] args) {
  115. Ideone a = new Ideone();
  116. try{
  117. a.vdf(4);
  118. }catch (java.lang.OutOfMemoryError e){
  119. System.err.println(e.getMessage());
  120. }
  121. }
  122.  
  123. }
Success #stdin #stdout 0.35s 320576KB
stdin
Standard input is empty
stdout
1260  21 60
1395  15 93
1435  41 35
1530  51 30
1827  87 21
2187  27 81
6880  86 80
7 vampire numbers found