Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labbar;
- public class Lifelength {
- public static int f1(int a0) {
- int sum = 0;
- if(a0 == 1) {
- return 1;
- }
- if(a0 % 2 == 0) {
- sum = a0/2;
- return sum;
- }
- else {
- sum = a0* 3 + 1;
- return sum;
- }
- }
- public static int f2(int a1) {
- int sum = f1(f1(a1));
- return sum;
- }
- public static int f4(int a2) {
- int sum = f2(f2(a2));
- return sum;
- }
- public static int f8(int a3) {
- int sum = f4(f4((a3)));
- return sum;
- }
- public static int f16(int a4) {
- int sum = (f8(f8(a4)));
- return sum;
- }
- public static int f32(int a5) {
- int sum = f16(f16(a5));
- return sum;
- }
- public static int iterateF(int a0, int n) {
- while(n > 0) {
- a0 = f1(a0);
- n = n - 1;
- }
- return a0;
- }
- public static int iterLifeLength(int a0) {
- int count = 0;
- while(a0 != 1) {
- a0 = f1(a0);
- count = count + 1;
- }
- return count;
- }
- public static String intsToString(int a0) {
- int X = a0;
- int Y = iterLifeLength(a0);
- return "The life length of " + X + " is " + Y + " and by recursion "
- + "";
- }
- public static int recLifeLength(int a0) {
- //int recsum = 0;
- if(a0 == 1) {
- return 0;
- }
- else {
- //recsum = recsum + 1;
- return recLifeLength(f1(a0)) + 1;
- }
- }
- public static int task1(int a0) {
- return f1(a0);
- }
- public static int task2() {
- int first = Integer.parseInt(args[0]);
- System.out.print("f1=" + f1(first) + " ");
- System.out.print("f2=" + f2(first) + " ");
- System.out.print("f4=" + f4(first) + " ");
- System.out.print("f8=" + f8(first) + " ");
- System.out.print("f16="+ f16(first) + " ");
- System.out.print("32=" + f32(first));
- }
- public static int task3(int a0, int n ) {
- return iterateF(a0, n);
- }
- public static String task4(int a0) {
- String s4 = "";
- for(int i = 1; i <=15; i++) {
- s4 = s4 + intsToString(a0);
- return s4;
- }
- }
- public static String task6(int a0) {
- for(int i = 1; i <=15; i++) {
- return (intsToString(i) + recLifeLength(i));
- }
- }
- public static void main(String[] args) {
- System.out.println(task1(42));
- System.out.println(task3(42,3));
- System.out.println(task4(42));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement