Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
We have two operations that can be performed in a number:

1. n = n + d.[ both n and d are given as input ]
2. n = sumofdigits(n)

We have to find the minimum value of n and minimum number of steps to get to that value. Now, I thought of an algorithm that stores the minimum values of n.

Now the problem is, I can't comprehend where to stop, or any smaller algorithm can be there? Thanks.

What I have tried:

<pre>def someFunction(value, d):
      if value > 10:
          minValue = sumOfdigits(value)
          stepCount = 1 // 1 , as one operation is already performed here.
      else:
          minValue = value
          stepCount = 0
      while(true):
          if value < 10:
             value = value + d
             stepCount+=1
          else:
             value = value + d
             if min > sumOfdigits(value):
                   min = sumOfdigits(value)
             stepCount+=2 // as two operations are being performed
Posted
Comments
tugrulGtx 10-Feb-19 11:29am    
What are you asking? Number of digits of a variable's 10s representation?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900