Lab 1 – Digital Rivers (Sp 25)

Project start code link – Start with this code and fork.

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, and 123+6=129), which 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(n)
sum = sum + d
n = n // 10 
sum now holds 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
if r_1<r_2, then replace r_1 with r_1+sum of the digits of r_1
otherwise, replace r_2 with r_2+sum of the digits of r_2

print the 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 pair, copy output, place in file “runs.txt”)

NameRiver 1River 2
We will meet at 473247
r1 < r25778
r1 > r275
Hundreds483456
Thousands11582085
River 248950262489
River 131314689
Primes991997
Even Bigger1548586313000000

Submit

  1. Your project on onlineGDB
  2. Turn in the URL of your program to Moodle
  3. Your project must include commented lines at the top that include your name and your email addrress.

Grading

RequirementsGrading CommentsPointsScore
Program passes all tests above80
Program is documented with comments10
Meaningful identifier names (variables, functions)10
Total100

Scroll to Top