Function F(n)
if n > 1 then
for i = 1 to n
Print “Hello”
for i = 1 to 10
F(n/3)
How often is written "Hello" as a function of n? Set up as an equation and solve it
assuming T is a function of number of times Hello is printed T(n) = 10T(n/3) + n and T(1) = 0
To determine how often "Hello" is written as a function of n, let's set up the equation based on the given function:
Function F(n):
If n > 1, then
For i = 1 to n, Print "Hello"
For i = 1 to 10, F(n/3)
Now, let's analyze how many times "Hello" is printed at each level of recursion:
For the initial call, n is the input value.
The first loop (i = 1 to n) prints "Hello" n times.
The second loop (i = 1 to 10) calls the function F(n/3) ten times.
So, we can set up the equation as follows:
Total "Hello" prints = n + 10 * F(n/3)
Now, let's consider the recursive nature of the function. Each recursive call reduces the value of n to one-third of its previous value (n/3). The process continues until n becomes less than or equal to 1 (the base case).
If we solve the equation recursively, we get:
F(n) = n + 10 * F(n/3) F(n/3) = (n/3) + 10 * F(n/9) F(n/9) = (n/9) + 10 * F(n/27) ...
We can observe a pattern emerging in the recursive calls. Each time we reduce n by dividing it by 3, the coefficient in front of F(n/3^k) is (n/3^k).
Eventually, we reach a point where n/3^k is less than or equal to 1. Let's find the value of k when this happens:
n/3^k ≤ 1 n ≤ 3^k log base 3 (n) ≤ k
So, the recursive calls continue until k = log base 3 (n).
Now, let's sum up the terms:
Total "Hello" prints = n + 10 * F(n/3) + 10 * F(n/9) + ... + 10 * F(n/3^k)
Total "Hello" prints = n + 10 * (n/3) + 10 * (n/3^2) + ... + 10 * (n/3^k)
This is a geometric series with the first term (a) as n and the common ratio (r) as 1/3.
Total "Hello" prints = n * (1 + 1/3 + 1/3^2 + ... + 1/3^k)
The sum of an infinite geometric series is given by the formula:
Sum = a / (1 - r)
Total "Hello" prints = n / (1 - 1/3)
Total "Hello" prints = n / (2/3)
Total "Hello" prints = (3n) / 2
So, the number of times "Hello" is printed as a function of n is (3n) / 2.
Explain please
II. (7 points) Consider the following bit of pseudocode: for (int k = 1; ks Ign; k++) (for (int i = 1; i r: i++) Print "Hello World"; for (int j-1;jsn:j++) Print "Hello World" when n = 2, how many times will "Hello World" be printed? When n 4, how many times will "Hello World be printed? O Assuming that the print is the basic operation, what is the complexity function of this pseudocode?
II. (7 points) Consider...
What is wrong with the following code snippet? print("Hello") print("World!") The print function cannot be called twice The print function is missing an argument Nothing, the program prints Hello World on the same line The second line should not be indented
Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...
hello sir, solve both questions
Problem 5: Let f : A → B be a function, and let X-A and Y-B. Show that X S(x)) Problem 6: Recall that BA denot es the set of all functions A the function f : P(A) → {0,1}A by B. Fix a set A and defi ne f (X)Xx (the charact erist ic function), VX EP(A) Prove that f is a bijection
Hello please assist me with this question. 1. Let the function f be defined by y = f(x), where x and f(x) are real numbers. Find f(2), f(−7), f(k), and f(k2 − 1). f(x) = 3 x − 5 2. Evaluate the expression. P(12, 2) · C(12, 2) 3. Find an equation in standard form of the parabola described. Vertex (8, 3); focus at (8, 10) 4. The population of Eagle River is growing exponentially according to the model P(t)...
Hello, I need help with the function below, The language I am using is Ocaml open Printf let () = for i = 1 to Array.length Sys.argv - 1 do if i + 1 <> Array.length Sys.argv then ( if Sys.argv.(i) > Sys.argv.(i+1) then ( --> printf "%s\n" Sys.argv.(i+1); ) else printf "%s\n" Sys.argv.(i); ) else printf "%s\n" Sys.argv.(i) done;; the function is taking command arguments and print them in...
python how can I print "Hello 55!" how can i add the exclamation mark at the end using a comma to add my variable? name = 55 print( "Hello", name )
2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn<2 else f(n-1) + f(n-2) end If m=0 else B. function g(m,n) g(m-1,m'n) C. function h(n) if n 100 else 3 h(n+5) end D. function j(m.n) IT m=n 100 j(m-n,n) 10 j(n,n-m) elseif mn else
2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
Can I please get help with this question?
Problem 3. How many lines, as a function of n (in 0(.) form), does the following program print? Write a recurrence and solve it. You may assume n is a power of 2. function f(n) { If (n>1) { print.line ("still going");/ f(n/2); f(n/2); }