Question

In: Computer Science

Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....

Java Recursion (Timelimit: 3 seconds)

Problem Description

Write a Java program to solve the following problem.

Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:

              |0,if m = 0 or n = 0

              | X(m,n−1),if n is odd and m is even

X(m,n) = | X(m−1,n),if m is odd and n is even

              | X(m−1,n−1) + m + n, otherwise.

Input:

The input is in ‘recursion.txt’. The first line of the input contains the line count c (1 ≤ c ≤ 1,000), which is the number of lines that follows the first line. Each of the following lines contains a pair of integers m,n (1 ≤ m,n ≤ 10,000).

Sample input: (with periods at the end)

3.

102,21.

10000,10000.

Output:

The output consists of c lines. Each line prints X(m,n) to a file ‘output.txt’.

Sample output:

2060.

100010000.

Information:

1. The expected complexity is O(1).

2. Write a code ‘A.java’ that reads ‘recursion.txt’ and writes the output in a file called ‘output.txt’.

3. The score will be 0 if your program does not terminate within 3 seconds.

Solutions

Expert Solution

//----------- A.java ------------

import java.io.*;
class A
{
   //function recursion definded in code.
   public static int X(int m,int n)
   {
       //System.out.println(m+" "+n);
       if(m == 0 || n== 0)
       {
           return 0;
       }
       //if m is even and n is odd
       if(m % 2==0 && n % 2 == 1)
       {
           return X(m,n-1);
       }
       //if m is odd and n is even
       else if(m % 2==1 && n % 2 == 0)
       {
           return X(m-1,n);
       }
       else
       {
           int res = X(m-1,n-1) + m + n;
           //System.out.println("--> "+res);
           return res;
       }
   }
   //answer for given recursion in terms of Time complexity of O(1)
   //function to return the sum of first n natural numbers.
   public static int sumOfFirstN(int n)
   {
       return n * (n+1) /2;
   }
   //equivalent function for X(m,n)
   public static int getRes(int m,int n)
   {
       //if m is less than n
       if(m<n)
       {
           //then the result will be sum of below terms.
           return sumOfFirstN(m) + sumOfFirstN(n) - sumOfFirstN(n-m);
       }
       //if n is less than m.
       else
       {
           //then the result will be sum of below terms.
           return sumOfFirstN(n) + sumOfFirstN(m) - sumOfFirstN(m-n);
       }
   }
   public static void main(String[] args)
   {
       //file input
       File f = new File("recursion.txt");
       //if the file exists are not.
       if(!f.exists())
       {
           System.out.println("recursion.txt is not found");
           return ;
       }
      
       try
       {
           //open file for reading
           //another file for writing.
           BufferedReader br = new BufferedReader(new FileReader(f));
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt")));
           //read a line from input file.
           String line = br.readLine();
           int m,n;
           //read number of lines to be read.
           int len = Integer.parseInt(line.trim());
           //read next line.
           line = br.readLine();
           //till file ended or length > 0
           while(line!=null && len>0)
           {
               line = line.trim();
               //split the data.
               String[] data = line.split(",");
               try
               {
                   //convert m,n
                   m = Integer.parseInt(data[0]);
                   n = Integer.parseInt(data[1]);
                   //call function that takes the O(1) time complexity to
                   //complete the task done by X(m,n) method.
                   bw.write(getRes(m,n)+"\n");
               }
               catch(Exception e)
               {
                   System.out.println("Invalid numbers m,n in line: "+line);
               }
               line = br.readLine();
               len--;
           }
           bw.close();
           br.close();
       }
       catch(Exception e)
       {
           System.out.println("Error occured while reading recursion.txt"+e);
       }
      
   }
}

//----------- recursion.txt -------
3
102,21
10000,10000
10,20
//output.txt ------------

2163
100010000
210


Related Solutions

Java Collatz Conjecture (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following...
Java Collatz Conjecture (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. The Collatz conjecture is about a sequence: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what...
Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
Write a complete Java Program to solve the following problem. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is a number character 1, 2, 3 or 4, which indicates whether a student is a freshman, a sophomore, junior or senior. Suppose the following characters are used to denote the majors: M: Mathematics C: Computer Science I: Information Technology Here is...
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT