简体   繁体   中英

10 digit number whose first n digits are divisible by n

So I came upon this little problem and I challenged myself to write my first program to solve it. The problem is to find a 10 digit number, for which if you take the first n digits, the resulting number must be divisible by n (eg. 1236, where 1 is divisible by 1, 12 by 2, 123 by 3 and 1236 by 4). My code is a little clumsy which i don't mind, but I'm getting error messages I don't understand.

from itertools import permutations

oddperm = permutations([1,3,7,9])
evenperm = permutations([2,4,6,8])


for odd in oddperm:
    for even in evenperm:
        num1 = (even[0]*(10**7)) + (even[1]*(10**5)) + (even[2]*10**3) + (even[3]*10)
        num2 = (odd[0]*10**8 )+ (odd[1]*10**6) + (5*10**4) + (odd[2]*10**2) + (odd[3])
        num = str((num1+num2)*10)
        if (num[0]*10 + num[1]) % 2 == 0 and #etc etc etc and (num[0]*10**8 + num[1]*10**7 + num[2]*10**6 + num[3]*10**5 + 5*10**4 + num[5]*10**3 + num[6]*10**2 + num[7]*10 + num[8]) % 9 == 0:
            print(num)
            break
    else:
        continue

The trouble is im getting

TypeError                                 Traceback (most recent call last)
<ipython-input-75-cb75172b012c> in <module>
     10         num2 = (odd[0]*10**8 )+ (odd[1]*10**6) + (5*10**4) + (odd[2]*10**2) + (odd[3])
     11         num = str((num1+num2)*10)
---> 12         if (num[0]*10 + num[1]) % 2 == 0 and ... and (num[0]*10**8 + num[1]*10**7 + num[2]*10**6 + num[3]*10**5 + 5*10**4 + num[5]*10**3 + num[6]*10**2 + num[7]*10 + num[8]) % 9 == 0:
     13             print(num)
     14             break

TypeError: not all arguments converted during string formatting

Also if someone has an idea on how to make that line a touch more elegant I'm all ears.

Thanks in advance for any and all contributions!

It looks to me like the error you describe is coming from a type conversion. You are converting num to a string, and then using indexing to get a certain digit of the number (which is fine), but before you can do any math with the digit, you need to convert it back into an int.

# num gets converted to a string
num = str((num1+num2)*10)
# num's digits get converted back into integers
if (int(num[0])*10 + int(num[1])) % 2 == 0:
    print(num)

Additionally, to make your checking of each digit more elegant, you can use a for loop and check for failure rather than success. This is an interesting problem so I spent a bit of time on it, haha. The following function can be called in place of the long if (int(num[0])*10 + int(num[1])) % 2 == 0 and... etc: , changing it to simply if check_num(num): .

def check_num(num:str):
    # define powers in advance for convenience
    powers = [10**p for p in range(len(num))]
    # check that the number satisfies the desired property
    place = 1
    while place < len(num):
        sum = 0
        # check each digit
        for i in range(place+1):
            sum += int(num[i]) * powers[place - i]
        # check for failure
        if sum % (place+1) != 0:
            return False
        # check the next place
        place += 1
    # we made it all the way through
    return True

Hope this is enlightening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM