Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public class Armstrong {
  2. private static long[][] degree = new long[10][19];
  3.  
  4. public static ArrayList<Long> getNumbers(long N) {
  5. long x1 = N;
  6. long start = 0L;
  7. long strt_incr = 0L;
  8. boolean sw = false;
  9. byte count = 19;
  10. ArrayList<Long> a_nums = new ArrayList<>();
  11. while (true) {
  12. byte[] array = new byte[19];
  13. byte position = 18;
  14. while (start > 0) {
  15. array[position--] = (byte) (start % 10);
  16. start = start / 10;
  17. }
  18. for (int i = 0; i < array.length; i++) {
  19. if (array[i] != 0 || sw) {
  20. sw = true;
  21. } else {
  22. count--;
  23. }
  24. }
  25. if (count < 2) {
  26. a_nums.add(strt_incr);
  27. }
  28. boolean is_incr = false;
  29. for (int i = array.length - count; i < array.length - 1; i++) {
  30. if (array[i] == array[i + 1] || array[i] < array[i + 1] || count == 1 || array[i + 1] == 0) {
  31. is_incr = true;
  32. } else {
  33. is_incr = false;
  34. break;
  35. }
  36. }
  37. if (is_incr) {
  38. long check = 0L;
  39. for (int i = array.length - count; i < array.length; i++) {
  40. if (degree[array[i]][count] == 0) {
  41. degree[array[i]][count] = (long) Math.pow(array[i], count);
  42. }
  43. check += degree[array[i]][count];
  44. }
  45. boolean found = false;
  46. for (Long l : a_nums
  47. ) {
  48. if (l == check) {
  49. found = true;
  50. break;
  51. }
  52. }
  53.  
  54. if (!found && checkSelf(check))
  55. a_nums.add(check);
  56. }
  57. sw = false;
  58. count = 19;
  59. if (strt_incr < x1 + 1) {
  60. strt_incr++;
  61. start = strt_incr;
  62. } else {
  63. break;
  64. }
  65. }
  66. return a_nums;
  67. }
  68.  
  69. private static boolean checkSelf(long value) {
  70. byte[] array = new byte[19];
  71. long to_check = value;
  72. byte count = 19;
  73. byte position = 18;
  74. boolean sw = false;
  75. while (to_check > 0) {
  76. array[position--] = (byte) (to_check % 10);
  77. to_check = to_check / 10;
  78. }
  79. for (int i = 0; i < array.length; i++) {
  80. if (array[i] != 0 || sw) {
  81. sw = true;
  82. } else {
  83. count--;
  84. }
  85. }
  86. long check = 0L;
  87. for (int i = array.length - count; i < array.length; i++) {
  88. if (degree[array[i]][count] == 0) {
  89. degree[array[i]][count] = (long) Math.pow(array[i], count);
  90. }
  91. check += degree[array[i]][count];
  92. }
  93. return check == value;
  94. }
  95. }
Add Comment
Please, Sign In to add comment