Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Write a method ackermann( m, n), which solves Ackermanns function ( 5 poi

ID: 3564210 • Letter: P

Question

Part 1

Write a method ackermann( m, n), which solves Ackermanns function ( 5 points) . Please document pre and post conditions at the beginning of your method. If none exists then state none for each. Use the following logic in your method: If m = 0 then return n + 1 If n = 0 then return ackermann( m - 1, 1) Otherwise, return ackermann( m - 1, ackermann( m, n - 1) ) Test your method in a program that displays the return values of the following method calls: (5 points) ackermann(0,0) , ackermann(0,1), ackermann(1,1), ackermann(1,2),ackermann(1,3),ackermann( 2,2) ackermann( 3,2).

part 2

Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. For example, a population might begin with two organisms, have an average daily increase of 50 percent, and will be allowed to multiply for seven days. The program should use a loop to display the size of the population for each day.         Input Validation: ? Do not accept a number less than 2 for the starting size of the population. ? Do not accept a negative number for average daily population increase. ? Do not accept a number less than 1 for the number of days they will multiply. ? Your program must a recursive method instead of a loop to calculate the number of organisms. ? Please document pre and post conditions at the beginning of any method you are completing, using JAVADOC as demonstrated in class. If none exists then state none for each.

part 3

Write a program that will find the sum of the digits in the number 100! n! means n (n ? 1) ... 3 2 1
For example, 10! = 10 9 ... 3 2 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!

Explanation / Answer

   public static class childprogs
    {
   /*
        pre conditions: m,n should be integer
    post conditions: returns ackermann calculation of m & n*/
        public int ackermann(int m, int n)
        {
            if(m==0)
                return n+1;
            else if(n==0)
                return ackermann(m-1,1);
            else
                return ackermann(m-1, ackermann(m, n-1));
        }
    }
    public static void main(String Args[])
    {
        childprogs obj = new childprogs();
        System.out.println("ackermann(0,0): "+ obj.ackermann(0,0));
        System.out.println("ackermann(0,1): "+ obj.ackermann(0,1));
        System.out.println("ackermann(1,1): "+ obj.ackermann(1,1));
        System.out.println("ackermann(1,2): "+ obj.ackermann(1,2));
        System.out.println("ackermann(1,3): "+ obj.ackermann(1,3));
        System.out.println("ackermann( 2,2): "+ obj.ackermann(2,2));
        System.out.println("ackermann( 3,2): "+ obj.ackermann(3,2));
      
    }


OUTPUT:
ackermann(0,0): 1
ackermann(0,1): 2
ackermann(1,1): 3
ackermann(1,2): 4
ackermann(1,3): 5
ackermann( 2,2): 7
ackermann( 3,2): 29


import java.util.Scanner;

public class progs {
    public static class childprogs
    {
/*precondition- enter valid integer values in input
post condition - desired population of week is displayed*/
    public static void main(String Args[])
    {
        Scanner sc = new Scanner(System.in);
        int start=0,avgincrease=0,noofdays=0;
        do
        {
            System.out.println("Enter starting population");
            start = sc.nextInt();
            if(start <2)
               System.out.println("Number of starting population cannot be <0 or negative. Please retry.");
            else
                break;
        }while(true);
        System.out.println("Enter average daily population increase percentage");
        avgincrease = sc.nextInt();
        do
        {
            System.out.println("number of days of multiplication");
            noofdays = sc.nextInt();
            if(noofdays <1)
               System.out.println("number of days of multiplication cannot be less than 1");
            else
                break;
        }while(true);
      
        System.out.println();
        System.out.println("Increase in population: ");
        progs obj = new progs();
        obj.calcpopulationincrease(start, avgincrease, noofdays, noofdays);
    }

/*pre conditions: startpopulation,percent,daycount,remaindays should be integer
post condition: population of the day is displayed*/
    public void calcpopulationincrease(int startpopulation,int percent,int daycount,int remaindays)
    {
        startpopulation = startpopulation + ((startpopulation*percent)/100);
        System.out.println("Population on day "+ (daycount - remaindays +1) +": "+ startpopulation);
        if(remaindays-1>0)
            calcpopulationincrease(startpopulation, percent, daycount, remaindays-1);
    }
  
}


OUTPUT:
Enter starting population
1
Number of starting population cannot be <0 or negative. Please retry.
Enter starting population
2
Enter average daily population increase percentage
50
number of days of multiplication
0
number of days of multiplication cannot be less than 1
number of days of multiplication
7

Increase in population:
Population on day 1: 3
Population on day 2: 4
Population on day 3: 6
Population on day 4: 9
Population on day 5: 13
Population on day 6: 19
Population on day 7: 28


import java.util.Scanner;

public class progs {
    public static class factsum
    {
        public long sumofterms(int num)
        {
            long sum=0;
        
            for(char c: String.valueOf(factorial(num)).toCharArray())
            {
                sum += Integer.parseInt(String.valueOf(c));
            }
            return sum;
        }
        long factorial(long num)
        {
            if(num>0)
                return num * factorial(num-1);
            else
                return 1;
        }
    }
    public static void main(String Args[])
    {
        Scanner sc = new Scanner(System.in);
        int number=0;
        factsum obj = new factsum();
        System.out.println("Enter number to calculate sum of digits");
        number = sc.nextInt();
        System.out.println("Sum of digits of "+ number + ": "+ obj.sumofterms(number));
    }
  
}

OUTPUT:
Enter number to calculate sum of digits
10
Sum of digits of 10: 27