
For this lab we will be solving the same problem we did in Lab one with C++.
A digital river is a sequence of numbers where every number is followed by the same number plus the sum of its digits. In such a sequence 123 is followed by 129 (since 1 + 2 + 3 = 6), which again is followed by 141.
We call a digital river river K, if it starts with the value K.
For example, river 7 is the sequence beginning with {7, 14, 19, 29, 40, 44, 52, … } and river 471 is the sequence beginning with {471, 483, 498, 519, … }.
Digital rivers can meet. This happens when two digital rivers share the same values. River 32 meets river 47 at 47, while river 471 meets river 480 at 519.
Given two meeting digital rivers print out the meeting point.
You start with two numbers. Say 1078 and 1069. What you would do is find the successor of each:
- 1078 + 1 + 7 + 8 = 1094
- 1069 + 1 + 6 + 9 = 1085
Since 1085 is the least, we replace the 1069 with 1085, giving us the pair 1078 and 1085 and go again:
- 1078 + 1 + 7 + 8 = 1094
- 1085 + 1 + 8+ 5 = 1099
So we now replace the 1078 giving us the pair 1094 and 1099. Next:
- 1094 + 1 + 9 + 4 = 1108
- 1099 + 1 + 9+ 9 = 1118
So next is 1108 and 1099:
- 1108 + 1 + 1 + 8 = 1118
- 1099 + 1 + 9 + 9 = 1118
Since BOTH end on the same number, the rivers join, and we are done!
How to get the digits?
We can get the lowest digit with the following: n%10. E.g. 1078%10 = 8
We can remove the low digit with n//10. E.g. 1078//10 = 107
So we just need a while loop that:
- gets the lowest digit
- adds the digit to a sum
- removes the the lowest digit
- loops until the we get a zero after we remove the lowest digit (simple while loop)
Hints
- you should write code the sums up the digits of a number. This can be done as follows:
n = number to sum digits ofsum = 0while n > 0d = low digitsum = sum + digitdivide d by 10sum now hold the sum of the digits
- You will need a while loop that runs until the two rivers meet.
r_1 and _r2 are the starting valueswhile r_1 != r_2 for r_1 and r_2add each to the sum of the digits.whichever is less, replace the value (r_1 or r_2) with the next valueprint the now matching value
Example input 1
3247
Example output 1
47
Example input 2
75
Example output 2
620
Turn in:
- A text file showing several runs of the program.
- Finish, run the tests, and submit lab to codeboard.io.
Grading
Turn in a Repl.it link, as well as screenshots of the runs
| Requirements | Grading Comments | Points | Score |
|---|---|---|---|
| Program passes all tests | 80 | ||
| Program is documented with comments | 10 | ||
| Meaningful identifier names (variables, functions) | 10 | ||
| Total | 100 |
