Lab 5

General instructions:-

  1. Make a directory named esc101-lab05 before starting. You should put all the programs in this directory.

Monday[10 Sep]


You must solve all 4 problems for full credit. You must attempt problem 4.
  1. Write Java methods as follows:
    1. A method to calculate a*x^n, which takes two double arguments and one integer argument and returns a double value.
    2. Use the method in i) to compute the value of a polynomial of degree 5 (i.e. a0 + a1*x +a2*x^2 ... + a5*x^5) in the main method of class.

    Note: x^2 means x*x.
  2. Find the root of an algebraic equation f(x) as follows by using a technique called regula-falsi.

    1. Write a method to calculate the value of the algebraic function f(x) (with one argument).
    2. Write two methods the first should return a value of x (say x1) for which f(x) is > 0 and the second one (say x2) for which f(x) is < 0. Then we know that a root of f(x) lies between x1 and x2.
    3. Write a method to calculate the value of x for which the line joining (x1,y1), (x2, y2) intersects the x-axis (say x3). This should be closer to the root and will replace either (x1, y1) or (x2, y2) depending on whether f(x3) is > 0 or < 0. If f(x3) is zero that is within epsilon of 0 then we have found the root.
    4. Use the above methods in a method, say findRoot(epsilon) that finds the root of f(x) with an accuracy of epsilon.
    Test your program on at least 3 different kinds of f.
  3. Write a Java method to compute the sin(x) with in a given accuracy epsilon. It should have two double arguments and return a double value.
    sin(x) is given by:
    sin(x)=x - x^3/3! + x^5/5! - x^7/7! ....
    Do this by using separate methods for x^n and for factorial.

  4. Write the following two recursive methods to check divisibility of a positive integer n by 3 and 11. Both methods should return boolean values.

    1. he rule for 3 is that n is divisible by 3 if the sum of the digits making up n is divisible by 3. For example 28761 is divisible since 2+8+7+6+1=24 is divisible by 3.
    2. The rule for 11 is that n is divisible by 11 if the difference of the sums of the digits in the odd position and even positions are equal. For example if n=28754 is divisible by 11 since 2+7+4=8+5.
    (Hint: For b) it will be easier if you write a helper function which is recursive and does all the work).


Tue[11 Sep]


  1. Write a program to compute the Ackermann function.

    A(m,n) = n+1, if m=0;
    A(m,n) = A(m-1,1) , if m > 0 and n= 0;
    A(m,n) = A( m-1 , A(m,n-1)) , if m > 0 and n > 0 ;
  2. Write a program to solve the following puzzle:-

    Suppose you are standing at a point,(x,y) in 2-D co-ordinate and want to go to the origin (0,0)[ Assume both x and y are positive here]. You are allowed to move either horizontally or vertically by 1 unit at a time i.e., if you are standing at (x,y) then two possible movements are either to (x-1,y) or ( x, y-1). Find the total number of possible shortest paths from (x,y) to origin (0,0). [Hint: Use recursive function].

  3. Write a method to compute factorial of an integer. Verify your result in problem 2 by computing (x+y)!/( x! * y!) using this function.

  4. Write a program that implements this definition of cube numbers:

    cube(1) = 1
    cube(N) = cube(N-1) + 3*(square(N)) - 3*N + 1

    Implement the square() method using this definition

    square(1) = 1
    square(N) = square(N-1) + 2*N - 1

  5. Write a program to compute the roots of a given quadratic equation a*x^2+b*x+c. Your program should take a, b and c as inputs. Write a separate method to compute the discriminant and use it to find the roots in case they are real. Otherwise it should print "imaginary roots".

  6. Wed[12 Sep]


    1. Write four Java methods to compute the sum, difference, product, and division of two double numbers. Decide the arguments and return types of the methods. Use these methods to write a fifth method that computes the expression (ax^2+by^2+cxy+dx+ey+f)/(gx^2+hy^2-kxy-mx-ny+p). This method should take a, b, c, d, e, f, g, h, k, m, n, p, x, y as double arguments. Finally, use this method from main method to compute and print the values of the following expressions for x=-3, y=2.1.

      (2x+5y)/(-4x^2+7)
      2/(9y^2+7xy-2)
      y^2-3xy

      Note: x^2 means x*x.
    2. Write three Java methods to compute the sum, product, and sum of reciprocals of two double numbers. Using these methods write three more methods to compute the arithmetic mean, geometric mean, and harmonic mean of the first n natural numbers. Write another method to compute the maximum and minimum of three double numbers. Use these methods in main to compute and print the three means and the maximum and minimum of the three means computed above. Properly decide the arguments and return types of the methods.

    3. Write a Java method to compute the expression 1/sqrt(1-x^2). From main use this method to print the value of the expression for x=1.3, -0.4, 0.8, 0, 2, -1. Your program should use the method only if x is in the proper range; otherwise it should only print an appropriate message on the screen.

    4. Write a method to compute n choose r, given n and r. Use this method from main to compute the sum of the binomial coefficients for various values of n.


    5. Write a recursive method to compute if a given positive integer n is a palindrome. It may help to pass the number of digits in n as an argument of the method (in addition to n). To start with, you can compute the number of digits in n within main before the first call to the recursive method. The return type of the method should be boolean.

    6. Thu[13 Sep]


      1. Write a program that uses recursion to compute the product of two non-negative integers.
      2. A Mersenne number is a number that is one less than a power of two, ie, a number of the form 2^n - 1. A Mersenne number that is also prime is called a Mersenne prime. It has the property that the exponent n is also prime, ie, a Mersenne prime has the form 2^p - 1, where p is prime. For example, taking p=2, 2^2 - 1 = 3, which is prime, and therefore a Mersenne prime. However, not every number of the form 2^p - 1 (p prime) is a prime number. Write a program that generates Mersenne primes for increasing prime numbers p, till you find a prime number for which the corresponding Mersenne number is not a Mersenne prime. Report this prime number. Your program should have the following methods:

        (i) A method named generatePrime() that generates prime numbers.
        (ii) A method named mersenneNumber() that takes a number n as argument and returns the corresponding Mersenne number 2^n - 1.
        (iii) A method named isPrime() that takes a number n as argument and returns true if it is prime, false if it is composite.

        Use method (i) to generate prime numbers using a loop. For each prime number generated, compute the corresponding Mersenne number using method (ii). Use method (iii) to check if the Mersenne number computed is also a Mersenne prime.

      3. Write a program to convert a decimal number to a hexadecimal number. Your program should use the following two methods : The first method converts the given decimal number to binary. The second method converts the binary number obtained from the first method to hexadecimal.

      4. The exponential function has the following Maclaurin series:
        e^x = summation over k=0 to infinity { x^k / k! }.
        Write a program to compute the value of e^(-1) using the above series, upto an error of 0.001%. To calculate the error, you will need a standard value of e^(-1), which you can obtain using the method Math.exp(). Your program should have a method that computes x^k / k!. This method will be called for increasing values of k from inside a loop in the main method. The sum of the return values of these calls will give an approximation for e^(-1). Exit from the loop when the error becomes less than 0.001%.


      5. Fri[14 Sep]


        1. Write Java methods to compute product of two integers and square of an integer. Use these methods to implement method power(a, b) which computes a^b. Write a Java class with a main method to test power(a, b). You should try to minimize number of method calls. Use binary representation of b to do this.
        2. Write a Java program which computes sinh (x) where
          sinh (x) = {summation over k = 0 to infinity } (x^(2k+1)) / (2k+1)!.
          Use separate methods for calculating factorial and power (You can use Math.pow). x can be any real number (double).
          Use first 10 terms of the expansion to approximate the value of sinh (x).

        3. Write a program to compute cube root of a number x using the fact that if p is an approximation for cube root of X then q = (2p+x/(p*p))/3 is a better one. Final cube-root may differ by 0.001. Write a separate method to compute q.

        4. Write a Java program with a main method and a method f. Your program should call f at least 10 times and using recursion, method argument and return value, it should print the number of times f is called.

        5. Write a Java program to compute value of a expression (ax+b)^c. Use a method nCr which uses recursion to compute binomial coefficients.