Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > College/University Maths Help > Discrete Math
Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2008, 12:53 PM
Newbie
 
Join Date: Aug 2008
Posts: 1
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
CaptSamuelFrank is on a distinguished road
Default 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.
Quote:
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.
Reply With Quote
Advertisement
 
  #2  
Old 08-26-2008, 06:34 PM
MHF Contributor


 
Join Date: Aug 2006
Posts: 3,705
Thanks: 28
Thanked 1,260 Times in 1,157 Posts
Plato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud ofPlato has much to be proud of
Default

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.
Reply With Quote
Reply

Tags
algorithm, discrete, easy

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 05:35 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2008 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.