Int h(int n)
{
if (n == 0)
return 0;
else
return h(n/10) + n%10;}
What is the output for each function call
(a) Function call h(736). Output:
(b) Function call h(12345). Output:
Answer options:
16,15 |
|
26,25 |
|
36,35 |
|
46,45 |
Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What...
#include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else /* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */ return (josephus(n - 1, k) + k-1) % n + 1; } // Driver Program to test above function int main() { int n = 14; int k = 2; printf("The chosen place...
How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 12. G[2]3 13. G[3]4 14. int i:-4 15. while (i<n) t 16. if (i mod 20) else ( 20. return G[n] 1. if (n 0) 2. return 1 3. else if (n1) f...
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
What does this function return if it is called with n = 4? int f(int n) { if (n == 0) return 1; else return f(n-1) + 1; } Can you show the steps as well please?
Given these methods: METHOD math1: public int math1( int n ) { if (n <= 1) { return 1; } // if else { return ( n * 2 ) + math1( n-1 ); } // else } // math1 METHOD math2: public int math2( int n ) { if (n <= 1) { return 1; } // if else { return n + math1( n ) * math2( n/2 ); } // else } // math2 (a) Set up...
int is_prime(int n) { if (n <= 1) return 0; if (n % 2 == 0 && n > 2) return 0; for (int i = 3; i < n / 2; i += 2) if (n % i == 0) return 0; return 1; } What do the three lines in this function mean?
def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without typing the code into Python, what are the values of the input arguments of the second recursive call if the function gcd is called from the main program as gcd(537,44)? (Hint: do not count the initial call to the function from the main program) Om = 44, n = 9 Om = 44, n = n = 537 Om = 9, n = 8...
Consider the following method: Linel: public static int mystery(int n) { Line2: if (n < 10) { ine3: return n; Line4: } else { Line5: int a = n/10; Line 6: int b = n % 10; Line 7: return mystery(a + b); Line 8: } Line 9: } What is the result of the following call? System.out.println(mystery(648)); 18 8 12
What is the output of the following java function when called as f(1,25,25)? Int f(int a, int b, int n){ int mid =(a+b)/2; if ((mid*mid <=n) && (n< (mid+1)*(mid+1)) return mid; else If (mid*mid>n) return f(a,mid-1,n); else return f(mid+1,b,n); } B.) Is the following function tail-recursive? Acker(m,n){ if(m=0) n+1 else if(n=0) Acker(m-1,1) else Acker(m-1,Acker(m,n-1)) }
C language int is_prime(int n) { if (n <= 1) return 0; if (n % 2 == 0 && n > 2) return 0; for (int i = 3; i < n / 2; i += 2) if (n % i == 0) return 0; return 1; } What do the three lines in this function mean?