Sep-28-2020, 09:12 PM
I think your logic is wrong. If the input unit is 3, the values for unit as your program loops are:
3, 6, 18, 72, 360, 2160
Don't you want them to be:
3, 6, 9, 12, 15, 18, 21?
Also, you program may run forever if unit and mode are not relatively prime. For example, if unit is 3 and mod is 9, this code will print an infinite sequence:
3, 6, 18, 72, 360, 2160
Don't you want them to be:
3, 6, 9, 12, 15, 18, 21?
Also, you program may run forever if unit and mode are not relatively prime. For example, if unit is 3 and mod is 9, this code will print an infinite sequence:
unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) i = 1 while (unit*i)%mod != 1: print((unit*i)%mod, end=' ') i += 1 else: print("Inverse =", i)
Output:3 6 9 0 3 6 9 0 3 6 9 0 ...
I think the result has to be in the range 1 to mod, so I would rewrite the code like this:unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) for i in range(1, mod): if (unit*i)%mod == 1: print("The inverse is: ", i) break else: print('There is no solution')