Polydivisible Calculator Fails, Despite Previous Implementation Working

Viewed 83

To begin, a definition:

A polydivisible number is an integer number where the first n digits of the number (from left to right) is perfectly divisible by n. For example, the integer 141 is polydivisible since:

  • 1 % 1 == 0
  • 14 % 2 == 0
  • 141 % 3 == 0

I'm working on a recursive polydivisible checker, which, given a number, will check to see if that number is polydivisible, and if not, recursively check every other number after until it reaches a number that is polydivisible. Unfortunately, my code doesn't work the way I want it to. Interestingly, when I input a number that is already polydivisible, it does its job and outputs that polydivisible number. The problem occurs when I input a non-polydivisible number, such as 13. The next polydivisible number should be 14, yet the program fails to output it. Instead, it gets stuck in an infinite loop until the memory runs out. Here's the code I have:

def next_polydiv(num):
    number = str(num)

    if num >= 0:

        i = 1
        print(i)
        while i <= len(number):

            if int(number[:i]) % i == 0:
                i += 1
                print(i)
            else:
                i = 1
                print(i)
                num += 1
                print(num)
        else:
            return num

    else:
        print("Number must be non-negative")
        return None

I'm assuming the problem occurs in the else statement inside the while loop, where, if the number fails to be polydivisible, the program resets i to 0, and adds 1 to the original number so it can start checking the new number. However, like I explained, it doesn't work the way I want it to. Any idea what might be wrong with the code, and how to make sure it stops and outputs the correct polydivisible number when it reaches one (like 14)?

(Also note that this checker is only supposed to accept non-negative numbers, hence the initial if conditional)

2 Answers

The mistake is that you are no updating number after incrementing num.

Here is working code:

def next_polydiv(num):
    number = str(num)

    if num >= 0:

        i = 1
        print(i)
        while i <= len(number):

            if int(number[:i]) % i == 0:
                i += 1
                print(i)
            else:
                i = 1
                print(i)
                num += 1
                print(num)
                number = str(num) # added line
        else:
            return num

    else:
        print("Number must be non-negative")
        return None

I have a similar answer to @PranavaGande, the reason is I did not find any way to iterate an Int. Probably because there isn't one...Duh !!!

def is_polydivisible(n):

    str_n = str(n)


    list_2 = []
    for i in range(len(str_n)):
        list_2.append(len(str_n[:i+1]))
    print(list_2)

    list_1 = []
    new_n = 0
    for i in range(len(str_n)):
        new_n = int(str_n[:i+1])
        list_1.append(new_n)
    print(list_1)

    products_of_lists = []

    for n1, n2 in zip(list_1, list_2):
        products_of_lists.append(n1 % n2)
    print(products_of_lists)

    for val in products_of_lists:
        if val != 0:
        return False
    return True

Now, I apologise for this many lines of code as it has to be smaller. However every integer has to be split individually and then divided by its index [Starting from 1 not 0]. Therefore I found it easier to list both of them and divide them index wise.

There is much shorter code than mine, however I hope this serves the purpose to find if the number is Polydivisible or Not. Of-Course you can tweak the code to find the values, where the quotient goes into decimals, returning a remainder which is Non-zero.

Related