0

How to create a itterative number 10digit long by taking a base number

Alex S
  • 15
  • 1
  • 1
  • 6
  • first, provide your code what you tried – Kallz Aug 12 '17 at 03:31
  • When does it stop? In your example, does it stop because `(7 + 3) % 10` would be `0`? Or are you aiming for an ID of a set length? – ChristianFigueroa Aug 12 '17 at 03:50
  • Welcome to Stack Overflow At this site you are expected to try to write the code yourself. After doing more research, if you have a problem you can post **what you've tried with a clear explanation of what isn't working** and providing a Minimal, Complete, and Verifiable example. I suggest reading [How to Ask](https://stackoverflow.com/help/how-to-ask) a good question and the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour](https://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/6676466). – Vishal Chhodwani Aug 12 '17 at 05:53

3 Answers3

0
n= 16

while(len(str(n)) < 10):
    print(n)
    a = n %10 + (int(n/10))%10 # sum of last two digits
    n *= 10
    n += a%10   #adding the last

print(n)
Anonta
  • 2,500
  • 2
  • 15
  • 25
0

Based on your example, I'm assuming you want to stop once you reach a 0.

Certain numbers, like 18 wouldn't reach a 0 ever, so also want to add a meximum length that the ID can be.

The following code runs the math until you reach a 0, or until it reaches 40 digits.

def getID(number)
    maxlength = 40 - len(str(number))
    while maxlength:
        string = str(number)
        answer = (int(string[-2]) + int(string[-1])) % 10
        if answer == 0:
            break
        number = number * 10 + answer
        maxlength -= 1
    return number


getID(14) == 1459437
getID(15) == 15617853819
getID(16) == 1673
getID(18) == 17853819
getID(18) == 189763921347189763921347189763921347189763

18 is a number that repeats forever, so instead the loop ends once it reaches a set length.

0

A Simple approach for Solution:

a=input()
while len(a)<10:
    b=int(a[len(a)-1])+int(a[len(a)-2])
    a=a+str(b%10)
print (a)

May This one helpful for you.

Rohit-Pandey
  • 2,039
  • 17
  • 24