Lab 1 – Digital River

Project Link

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. We thus say that 519 is the meeting point of river 471 and river 880.

For this lab 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

  1. you should write code the sums up the digits of a number. This can be done as follows:
n = number to sum digits of
sum = 0
while n > 0
  d = low digit
  sum = sum + digit
  divide d by 10 
sum now hold the sum of the digits
  1. You will need a while loop that runs until the two rivers meet.
r_1 and _r2 are the starting values
while r_1 != r_2  for r_1 and r_2
  add each to the sum of the digits.  
  whichever is less, replace the value (r_1 or r_2) with the next value
print the now matching value

Example input 1

32
47

Example output 1

47

Example input 2

7
5

Example output 2

620

Test Data (run for each, copy output, place in file “runs.txt”)

NameRiver 1River 2
We will meet at 473247
r1 < r25778
r1 > r275
More than… i.483456
More than… ii.11582085
River 248950262489
River 131314689
Primes991997
Even Bigger1548586313000000

Submit on CodeBoard.io:

  1. A working program
  2. A text file “runs.txt” with all the sample runs from above. Create a new file “runs.txt” in your project. For set of sample input above, run the program, and copy the output into “runs.txt”. Save.

Grading

RequirementsGrading CommentsPointsScore
Program passes all tests above, with runs in “runs.txt”80
Program is documented with comments10
Meaningful identifier names (variables, functions)10
Total100

End

Sol

Scroll to Top