Results 1 to 2 of 2

Math Help - An algorithm problem Discrete Mathematics class

  1. #1
    Newbie
    Joined
    Aug 2008
    Posts
    1

    An algorithm problem Discrete Mathematics class

    This is from the first day of class. We were given problems we should be able to do. The only one I had a problem with was this one as I have never really learned any programming yet nor modulus's.
    Input: A positive integer number a.
    Output: A positive integer number s.

    Let s = 0
    While a > 0
    Let s = s + (a mod 10)
    Let a = a/10

    In this algorithm, the word while indicates a loops, meaning that following indented steps are repeated as long as the loop condition of a > 0 holds. Also, note the operation a/10 is integer division, meaning that the fractional portion of the result is simply dropped (e.g. 10/4 = 2).

    (a) What is the output of the algorithm if the input is a = 432765891
    (b) One important characteristic of an algorithm is efficiency. Typically, this is measured by the number of operations the algorithm performs. How many additions does this algorithm perform given the input of some positive integer a? Express your answer as a function of a.
    If someone could show me the steps to solving this problem and explain how to go about solving it I would be very pleased. I wish to be able to do these kind of things on my own and I am guessing it is probably quite simple.

    Thanks so much for any help!
    -CSF
    peace.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor

    Joined
    Aug 2006
    Posts
    18,649
    Thanks
    1596
    Awards
    1
    Quote Originally Posted by CaptSamuelFrank View Post
    This is from the first day of class. We were given problems we should be able to do. The only one I had a problem with was this one as I have never really learned any programming yet nor modulus's.
    If someone could show me the steps to solving this problem and explain how to go about solving it I would be very pleased. I wish to be able to do these kind of things on my own and I am guessing it is probably quite simple
    I will tell you that I do not know anything past a basic knowledge of programming.
    But I am sure that the second line is incorrect. It must be While a >1.
    Otherwise, we are in an infinite loop.
    If that correction is made, then the program adds then the digits in the number a.
    So that is the answer to part a.

    For part (b), the number pf ‘sums’ is the number of places in the number a.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Discrete Mathematics - Algorithm output
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: March 7th 2010, 09:43 AM
  2. Discrete mathematics set theory problem!
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: February 3rd 2010, 09:27 AM
  3. Discrete Mathematics Problem (Proofs)
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: October 10th 2009, 06:52 AM
  4. Practice Problem (Intro to Discrete Math Class)
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: January 15th 2009, 09:38 AM
  5. Mathematics: Discrete-Mathematics (Algorithems)
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: November 2nd 2008, 06:27 AM

Search Tags


/mathhelpforum @mathhelpforum