Question

Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence when the last two numbers repeat the values of a and b for the first time. For example, if a = 1 and b = 1, the function should return 62.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code screenshot

Python sample output screenshot

Python code in text

def mystery(a,b):

#initialize resulting list with a,b
resultinglist=[a,b]
#store initial value of a
inita=a
#store initial value of b
initb=b
#keep computing while until termination condition is reached with break
while True:

#the new element is ones place of summation of a and b, ones place of x is obtained by x%10
newelement=(a+b)%10
resultinglist.append(newelement)
#next iteration is to be taken for the b and the new element
a = b
b = newelement
#when last 2 elements are inital a and initial b respectively, termination condition is reached
if resultinglist[len(resultinglist)-2]==inita and resultinglist[len(resultinglist)-1]==initb:

break

print("Sequence is:-")
print(resultinglist)
#len(resultinglist) gives the length of this list
return len(resultinglist)

print(mystery(1,1))
print(mystery(2,2))
  

Add a comment
Know the answer?
Add Answer to:
Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT