Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. Collatz tester = new Collatz();
  9. /*List<Integer> test = new ArrayList<Integer>();
  10.  
  11. System.out.println("wtf");
  12. for(int i = 1;i < 11000;i++){
  13. test.add(i);
  14. }
  15. for(int x: test){
  16. System.out.println(x);
  17. }*/
  18.  
  19. tester.fillArray(10);
  20. tester.iterateCollatz();
  21. System.out.println("length: " + tester.getLength() + " given by seed " + tester.getLongestSeed());
  22.  
  23.  
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Random;
  28. import javafx.css.Size;
  29.  
  30. public class Collatz {
  31.  
  32. private int longestChain; // Length of the current longest chain
  33. private int seed; // The current seed value, i.e. the value at which the current chain starts
  34. private int longestSeed; // Number which generates the longest Collatz sequence
  35. private List<Integer> array; // ArrayList on which the Collatz sequence operates
  36.  
  37. public Collatz() {
  38. this.longestChain = 1;
  39. this.array = new ArrayList<>();
  40. this.seed = 1;
  41. this.longestSeed = 1;
  42. }
  43.  
  44. // Create an ArrayList with entries 1...n
  45. public void fillArray(int n) {
  46. for (int i = (n-1)/2; i < n; i = i++) {
  47. if(!((n%2 ==0) && (n%3 == 1)))
  48. this.array.add(i);
  49. }
  50. }
  51.  
  52. public void iterateCollatz() {
  53. int i = 1;
  54. while (this.array.size() > 0) {
  55. System.out.println("iteration " + i);
  56. this.setSeed();
  57. this.runCollatzOperation(this.seed);
  58. i++;
  59. }
  60. }
  61.  
  62. public void runCollatzOperation(int n) {
  63. int m = n;
  64.  
  65. int length = 1;
  66. while (n != 1) {
  67. if (this.array.contains(n)) {
  68. this.deleteEntry(n);
  69. }
  70.  
  71. if (n % 2 == 0) {
  72. n = n / 2;
  73. }
  74. else {
  75. n = 3 * n + 1;
  76. }
  77. length++;
  78.  
  79. }
  80. if (length > this.longestChain) {
  81. this.longestChain = length;
  82. this.longestSeed = m;
  83. }
  84.  
  85. }
  86. //Deletes the value n from this.array, if it exists
  87.  
  88. public void deleteEntry(int n) {
  89.  
  90. this.array.remove(this.array.indexOf(n));
  91.  
  92. }
  93.  
  94. /*Generate a random number to serve as the seed of the next chain
  95. from the remaining numbers in this.array */
  96. public void setSeed() {
  97. Random generator = new Random();
  98. int random = generator.nextInt(this.array.size());
  99. this.seed = this.array.get(random);
  100. }
  101.  
  102. public int getSeed() {
  103. return this.seed;
  104. }
  105.  
  106. public int getLongestSeed() {
  107. return this.longestSeed;
  108. }
  109.  
  110. public int getLength() {
  111. return this.longestChain;
  112. }
  113.  
  114. public int getMax() {
  115. return Collections.max(this.array);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement