Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package seelman;
  2.  
  3. public class Seelman {
  4.  
  5. // 500 million (max_int is only 2.1b)
  6. public static final int SEQUENCE_MAX = 500000000;
  7.  
  8. public static final int SEQUENCE_LENGTH = SEQUENCE_MAX;
  9.  
  10. // sequence[i] is the sequence, i.e. sequence[1] = 1, sequence[2] = 3
  11. int[] sequence = new int[SEQUENCE_LENGTH];
  12.  
  13. // is the value (already) in the sequence?
  14. boolean[] sequenced = new boolean[SEQUENCE_MAX];
  15.  
  16. public void checkForEmpties() {
  17. /*
  18. * If any sequence from n to (2n-1) is all sequenced while any value x <
  19. * n is not sequenced, then that value x will be unreachable.
  20. */
  21.  
  22. int firstUnsequenced = SEQUENCE_MAX;
  23. for (int i = 1; i < SEQUENCE_MAX; i++) {
  24. if (!sequenced[i]) {
  25. firstUnsequenced = i;
  26. break;
  27. }
  28. }
  29.  
  30. for (int start = firstUnsequenced + 1; start <= SEQUENCE_MAX / 2; start++) {
  31. boolean broken = false;
  32. for (int i = start; i < start * 2; i++) {
  33. if (!sequenced[i]) {
  34. broken = true;
  35. break;
  36. }
  37. }
  38. if (!broken) {
  39. System.out.println("The value " + firstUnsequenced + " is not in the sequence, but every value from "
  40. + start + " to " + (start * 2 - 1) + " is.");
  41. return;
  42. }
  43. }
  44.  
  45. }
  46.  
  47. public void createSequence() {
  48. String str = "";
  49.  
  50. for (int i = 1; i < SEQUENCE_LENGTH; i++) {
  51. int value;
  52. if (i == 1) {
  53. value = 1;
  54. } else {
  55. int prev = sequence[i - 1];
  56. value = prev / 2 + prev % 2;
  57. if (sequenced[value]) {
  58. value = prev * 3;
  59. }
  60. }
  61.  
  62. if (value > SEQUENCE_MAX) {
  63. System.out.println(str);
  64. System.out.println("Exceeded sequence max after " + i + " entries.");
  65. checkForEmpties();
  66. return;
  67. }
  68.  
  69. if (sequenced[value]) {
  70. System.out.println(str);
  71. System.out.println("Sequence repeat.");
  72. return;
  73. }
  74.  
  75. sequence[i] = value;
  76. sequenced[value] = true;
  77.  
  78. str += value + " ";
  79. if (str.length() > 64) {
  80. System.out.println(str);
  81. str = "";
  82. }
  83. }
  84. }
  85.  
  86. public static void main(String[] args) {
  87. Seelman s = new Seelman();
  88.  
  89. s.createSequence();
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement