Part 1
The function 'vbrf' (for very bad recursive function) is defined as follows:
vbrf(0) = 2
vbrf(1) = 3
vbrf(2) = 4
vbrf(3) = 5
vbrf(n) = vbrf(n-1) - vbrf(n-2) + 3*vbrf(n-3) - 2*vbrf(n-4) if n > 3
Your job for this problem is to implement the function vbrf as a method in Java. Then write a program that uses this method to compute the function for various values of the argument and time how long it takes to compute. Also, include in this program a part that computes the function for the arguments from 1 to 12 and displays the argument and the results. Once you have the program working and can examine the results, report the following in a comment near the beginning of your program file:
To determine how long it takes to compute vbrf, use code something like the following:
long t0 = System.nanoTime();
int x = vbrf(15);
long t1 = System.nanoTime();
double timeInSeconds = (t1-t0)*1.0e-9;
Be aware, the time it takes to compute vbrf(n+1) is almost twice the time it takes to compute vbrf(n). Start with small values of n and work your way up.
Part 2
We will be studying one aspect of permutations in Assignment #5. We will make use of a function that is defined recursively. Your job for this part of the assignment is to implement this function and print the first few values. Implement the method as a static method that takes a long for an argument and returns a long. The method itself should do no printing.
We'll call the function h. It is defined by these conditions:
h(0) = 1
h(1) = 0
h(n) = (n-1)*(h(n-1) + h(n-2)) for n > 1
To check your work, here are the first few values of h:
| n | h(n) |
|---|---|
| 0 | 1 |
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 9 |
| 5 | 44 |
| 6 | 265 |
| 7 | 1854 |
| 8 | 14833 |
| 9 | 133496 |
| 10 | 1334961 |
| 11 | 14684570 |
| 12 | 176214841 |
| 13 | 2290792932 |
| 14 | 32071101049 |
| 15 | 481066515734 |
Your program should print the first 10 values or so of the function. Your program should not be interactive!
public class h{
public static void main(String[] args) {
int b = 6;
int c = h(b);
System.out.println("answer= " + c);
}
/** returns the minimum of two numbers */
public static int h(int n) {
int val;
val=(n-1)*(h(n-1)+h(n-2));
return val;
}
}
Part 1 The function 'vbrf' (for very bad recursive function) is defined as follows: vbrf(0) =...
Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...
The formula for the nth Tetranacci number ?? is defined as follows: ?0 = 0 ?1 = 1 ?2 = 1 ?3 = 2 ?? = ??−1 + ??−2 + ??−3 + ??−4 Your task is to implement a recursive function which accepts an integer n (you may assume that n >= 0), and computes the n-th Tetranacci number (don’t worry about efficiency, only about making the definition as simple as possible).
Problemi Consider the plecewise function sit) defined as follows: -(+2 when -2sts2 0.5 elsewhere s(t)- Write a MATLAB user-defined function called aystfun.n that has one input argument to, and two output arguments: s0 and vsO. Your function must satisfy all the following requirements: 1. Your function must calculate and return the vector so - s(t0) for any supplied numerical vector to. We assume that the function is always called with a vector t0 whose elements are all in [-4,4]. 2....
Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...
Suppose that a Tibonacci sequence is defined as follows: Tibo(0) = 1, Tibo(1) = 2, Tibo(2) = 3 Tibo(n) = Tibo(n-1) + Tibo(n-3), when n ≥ 3 Write a recursive method Tibo, that takes some integer n as a parameter and returns the n-th Tibonacci number. You DO NOT need to write the main function.
(a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...
The indicator function of a set, A, is denoted by l and is defined as follows (a) Show that if A and B are any two sets, then the indicator function, ln, of (b) IfA and B are disjoint (i.e An B is empty), show that luB is the sum, 1, if x E A A n B, is the product lalB.i.e Show that lanB (x) =ム(x)h(x) for every x. ム+1B. İ.e Show that 1AUB (x)-4(x) + 1B (x) for...
IN MATLAB
RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up the factorial of the natural numbers from 1 to N can be expressed as The recursive algorithm: N-1 N-2 N-3 Write independent matlab code to solve the above problem with the following methods: 1. 2. 3. A monolithic program (with no functions) A standard (non-recursive) user defined function (an a program to call it) A recursive function (an a program to call it) Test...
Suppose that the function h is defined on the interval (-2, 2] as follows. -1 if -2<xs-1 0 if - 1<xs0 h(x) = 3 1 if 0<xs1 2 if 1<xs2 Find h(-1), h(0.25), and h (2). h (0.25) = | х ó ? h (2) = 0 Egnation Check Mac
Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of 1's in the given number. For example: 1111 0000 should return 4 j main ############################################################### # Data Section .data # new_line: .asciiz "\n" space: .asciiz " " double_range_lbl: .asciiz "\nDouble range (Decimal Values) \nExpected output:\n1200 -690 104\nObtained output:\n" swap_bits_lbl: .asciiz "\nSwap bits (Hexadecimal Values)\nExpected output:\n75757575 FD5775DF 064B9A83\nObtained output:\n" count_bits_lbl: .asciiz "\nCount bits \nExpected output:\n20 24 13\nObtained output:\n" swap_bits_test_data: .word 0xBABABABA, 0xFEABBAEF, 0x09876543 swap_bits_expected_data: .word...