Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

It is in Java. Please help me with these questions. 1-A strange function. Consider McCarthy's 91 function: public static int mcCarthy(int n) { if (n

It is in Java. Please help me with these questions.

1-A strange function. Consider McCarthy's 91 function: public static int mcCarthy(int n) { if (n > 100) return n - 10; else return mcCarthy(mcCarthy(n+11)); } Determine the value of mcCarthy(50) without using a computer. Give the number of recursive calls used by mcCarthy() to compute this result. Prove that the base case is reached for all positive integers n or find a value of n for which this function goes into a recursive loop.

2-Consider the following recursive functions. public static int square(int n) { if (n == 0) return 0; return square(n-1) + 2*n - 1; } public static int cube(int n) { if (n == 0) return 0; return cube(n-1) + 3*(square(n)) - 3*n + 1; } What is the value of square(5)? cube(5)? cube(123)?

3-Combinations. Write a program Combinations.java that takes an integer command-line argument n and prints all 2ncombinations of any size. A combination is a subset of the n elements, independent of order. As an example, when n = 3, you should get the following output:

 a ab abc ac b bc c 

Note that your program needs to print the empty string (subset of size 0).

4-Collatz function. Consider the following recursive function in Collatz.java, which is related to a famous unsolved problem in number theory, known as the Collatz problem or the 3n + 1 problem.

public static void collatz(int n) { StdOut.print(n + " "); if (n == 1) return; if (n % 2 == 0) collatz(n / 2); else collatz(3*n + 1); } 

For example, a call to collatz(7) prints the sequence

7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 

as a consequence of 17 recursive calls. Write a program that takes a command-line argument n and returns the value of i < n for which the number of recursive calls for collatz(i) is maximized. Hint: use memoization. The unsolved problem is that no one knows whether the function terminates for all integers (mathematical induction is no help because one of the recursive calls is for a larger value of the argument).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions