Question

Use pseudocode to describe an algorithm for the procedure: int sum_digits( int number ). The procedure...

Use pseudocode to describe an algorithm for the procedure: int sum_digits( int number ). The procedure will recursively sum the individual digits in number. For example, when number=123, the procedure will return 6. State the time complexity of your solution and clearly identify your base case(s) and recursive case(s).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
The program is written in python 3.x 

The time complexity of this program is O(n) where is the number of digits present in the number. This is because the recursion tree here is a linear tree with no branches (as we are only calling one recursion function at each step)so the height of the tree will be as same as the number of digits present in the number.


def sum_digits(number):
    # this is the base condition
    # this condition tells the program to stop when the number is a single digit number
    if(number<10):
        return number
    else:
        # this is the recursive step
        # Here we will take each and every single digit from the number and add it
        # the number%10 will give us the unit digit of the number 
        # the sum_digits(int(number/10)) will call the recursive function on number except the unit place and make the tens place as a new units place
        return (number%10) + sum_digits(int(number/10))

print(sum_digits(123456789))

def sum_digits(number): # this is the base condition # this condition tells the program to stop when the number is a single d

Add a comment
Know the answer?
Add Answer to:
Use pseudocode to describe an algorithm for the procedure: int sum_digits( int number ). The procedure...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

  • Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function...

    Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsorted integer array A of size n. For example, given the array A=[6,-6,3,2,1,2,0,4,3,5] and k=3, the function should return -5. a. [3 marks) Write an algorithm in pseudocode for SumKSmallest using the brute force paradigm. Indicate and justify (within a few sentences) the time complexity of your algorithm. b....

  • [10 marks] consider the following pseudocode: p := 0 x := 2fori:= 2ton p := (p...

    [10 marks] consider the following pseudocode: p := 0 x := 2fori:= 2ton p := (p + i ) * x a) How many addition(s) and multiplication(s) are performed by the above pseudocode?b) Express the time-complexity of the pseudocode using the big-Θ notation.) Trace the algorithm, below, then answer (c) and (d):   Procedure xyz(n: integer) s := 0, for i:= 1 to n, for j:= 1to i, s:= s+ j*(i− j+ 1) return s c) What is the time-complexity for...

  • Discrete Mathematics 5. i) Describe an algorithm that, upon input of a number n given in...

    Discrete Mathematics 5. i) Describe an algorithm that, upon input of a number n given in base 10, outputs the digits of n in base 8, starting from the rightmost. Esrample: If you are given the number 156, the output will be 432, because (156) 10 = (234)s. ii) What will be the complexity of the algorithm? You may assume that performing the division algorithm upon two numbers to find the quotient and remainder is the basic operation. (As a...

  • 1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and...

    1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm: If x, y are two adjacent elements in a sequence, with x before y, we say that the pair x, y is in order when x <= y and the pair is out of order when x > y. For example, in the string “BEGGAR” the pair G, A are out of order, but all the...

  • Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits...

    Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits in the integer n using a recursive process. For example, (digits 42) should return 2 and (digits 13579) should return 5. You may make use of the built in floor predicate for truncating decimals to whole numbers. b. Rewrite your procedure from part (a) using an iterative process. Call the function (digits-iter n).

  • What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1...

    What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-

  • Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree....

    Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree. In particular, your algorithm must return 0 and 1 for the empty and single-node trees, respectively. What is the time efficiency class of your algorithm?

  • Q2. Answer the following questions assignment, you will develop a well-documented pseudocode that generates all possibl...

    Q2. Answer the following questions assignment, you will develop a well-documented pseudocode that generates all possible subsets of a given set T (i.e. power set of T) containing n elements with the following requirements: your solution must be non-recursive, and must use a stack and a queue to solve the problem. For example: ifT- {2, 4, 7,9; then your algorithm would generate: U, t2), (4;, {7), {9;, 12,4), 12,7;, 12.9;, 14,7), 14,9), 17,9;, ...etc. (Note: your algorithm's output need not...

  • Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular,...

    Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular, the algorithm should return 0 and 1 for the empty and single-node trees respectively. Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O() notation? Design a divide-and-conquer algorithm for computing the number of levels in a COMPLETE binary tree. In particular, the algorithm should return 0 and 1 for the empty and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT