0% found this document useful (0 votes)
19 views2 pages

Curious Recursion p1

Uploaded by

griffin.a.oneill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Curious Recursion p1

Uploaded by

griffin.a.oneill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class ExploreFunctionH {

� � // Recursive function H

� � public static int H(int n, int a, int b) {


� � � � if (n==0){
� � � � � � return b+1; // base case for H(0, a, b)
� � � � }
� � � � else if (n==1 && b==0) {
� � � � � � return a; // base case for H(1, a, 0)
� � � � }
� � � � else if (n==2 && b==0) {
� � � � � � return 0; // base case for H(2, a, 0)
� � � � }
� � � � else if (n>=3 && b==0) {
� � � � � � return 1; // base case for H(n, a, 0) when n >= 3
� � � � }
� � � � else if (b>=1) {
� � � � � � return H(n-1, a, H(n, a, b-1)); // recursive call when b >= 1
� � � � }
� � � � return 0; // fallback case (although it shouldn't reach here)

� � }

� � // Main method to test the behavior of the function H

� � public static void main(String[] args) {


� � � � // Test cases for different values of n, a, and b
� � � � system.out.println("H(0, a, b) behaves as: ");
� � � � for (int a = 0; a < 3; a++) {
� � � � � � for (int b = 0; b < 3; b++) {
� � � � � � � � System.out.println("H(0, " + a + ", " + b + ") = " + H(0, a, b));
� � � � � � }
� � � � }

� � � � System.out.println("\nH(1, a, b) behaves as: ");


� � � � for (int a = 0; a < 3; a++) {
� � � � � � for (int b = 0; b < 3; b++) {
� � � � � � � � System.out.println("H(1, " + a + ", " + b + ") = " + H(1, a, b));
� � � � � � }
� � � � }

� � � � System.out.println("\nH(2, a, b) behaves as: ");


� � � � for (int a = 0; a < 3; a++) {
� � � � � � for (int b = 0; b < 3; b++) {
� � � � � � � � System.out.println("H(2, " + a + ", " + b + ") = " + H(2, a, b));
� � � � � � }
� � � � }

� � � � System.out.println("\nH(3, a, b) behaves as: ");


� � � � for (int a = 0; a < 3; a++) {
� � � � � � for (int b = 0; b < 3; b++) {
� � � � � � � � System.out.println("H(3, " + a + ", " + b + ") = " + H(3, a, b));
� � � � � � }
� � � � }

� � }
� �
}
/*
� � What common functions are computed by H(n, a, b):

� � 1. H(0, a, b):
� � � �- This computes b + 1.
� � � �- It behaves as the "successor function," simply returning b + 1 regardless
of a.

� � 2. H(1, a, b):
� � � �- This computes a + b.
� � � �- It behaves as addition of a and b. For example, H(1, a, b) = a + b.

� � 3. H(2, a, b):
� � � �- This computes a * b.
� � � �- It behaves as multiplication of a and b. For example, H(2, a, b) = a * b.

� � 4. H(3, a, b):
� � � �- This computes a^b (exponentiation).
� � � �- It behaves as exponentiation, where a is raised to the power of b. For
example, H(3, a, b) = a^b (a to the power of b).
*/

You might also like