Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. package labbar;
  2.  
  3. public class Lifelength {
  4. public static int f1(int a0) {
  5. int sum = 0;
  6. if(a0 == 1) {
  7. return 1;
  8. }
  9. if(a0 % 2 == 0) {
  10. sum = a0/2;
  11. return sum;
  12. }
  13. else {
  14. sum = a0* 3 + 1;
  15. return sum;
  16. }
  17. }
  18. public static int f2(int a1) {
  19. int sum = f1(f1(a1));
  20. return sum;
  21. }
  22. public static int f4(int a2) {
  23. int sum = f2(f2(a2));
  24. return sum;
  25. }
  26. public static int f8(int a3) {
  27. int sum = f4(f4((a3)));
  28. return sum;
  29. }
  30. public static int f16(int a4) {
  31. int sum = (f8(f8(a4)));
  32. return sum;
  33. }
  34. public static int f32(int a5) {
  35. int sum = f16(f16(a5));
  36. return sum;
  37. }
  38. public static int iterateF(int a0, int n) {
  39. while(n > 0) {
  40. a0 = f1(a0);
  41. n = n - 1;
  42. }
  43. return a0;
  44. }
  45. public static int iterLifeLength(int a0) {
  46. int count = 0;
  47. while(a0 != 1) {
  48. a0 = f1(a0);
  49. count = count + 1;
  50. }
  51. return count;
  52. }
  53. public static String intsToString(int a0) {
  54. int X = a0;
  55. int Y = iterLifeLength(a0);
  56. return "The life length of " + X + " is " + Y + " and by recursion "
  57. + "";
  58. }
  59. public static int recLifeLength(int a0) {
  60. //int recsum = 0;
  61. if(a0 == 1) {
  62. return 0;
  63. }
  64. else {
  65. //recsum = recsum + 1;
  66. return recLifeLength(f1(a0)) + 1;
  67.  
  68. }
  69. }
  70. public static int task1(int a0) {
  71. return f1(a0);
  72. }
  73.  
  74.  
  75. public static int task2() {
  76. int first = Integer.parseInt(args[0]);
  77. System.out.print("f1=" + f1(first) + " ");
  78. System.out.print("f2=" + f2(first) + " ");
  79. System.out.print("f4=" + f4(first) + " ");
  80. System.out.print("f8=" + f8(first) + " ");
  81. System.out.print("f16="+ f16(first) + " ");
  82. System.out.print("32=" + f32(first));
  83. }
  84.  
  85. public static int task3(int a0, int n ) {
  86. return iterateF(a0, n);
  87. }
  88. public static String task4(int a0) {
  89. String s4 = "";
  90. for(int i = 1; i <=15; i++) {
  91. s4 = s4 + intsToString(a0);
  92. return s4;
  93.  
  94. }
  95. }
  96. public static String task6(int a0) {
  97. for(int i = 1; i <=15; i++) {
  98. return (intsToString(i) + recLifeLength(i));
  99. }
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106. public static void main(String[] args) {
  107. System.out.println(task1(42));
  108. System.out.println(task3(42,3));
  109. System.out.println(task4(42));
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement