ecrire un programme python qui permet de verifier si un nombre est divisible par 137 ou non en utilisant le principe suivant:
il suffit de séparer ce nombre par tranche de 4 chiffres en partant des unités (droite du nombre) et d’insérer alternativement des - et des + entre les tranches à partir du début du nombre (gauche du nombre) en commençant par un -, On effectue l’opération ainsi écrite et si le résultat est divisible par 137, alors le nombre considéré est divisible par 137.

Is there any requirement on the specific method for how to test for divisibility by `137`, when the number has no more than 4 digits?

The number formed by the last four decimal digits of an `int` `x` can be obtained as `x % 10000`.

The number obtained by removing the last four digits can be computed as `x // 10000`.

So, you can, while `x != 0` accumulate the result of `x % 10000` alternating sign and replace the current `x` with `x // 10000`.

If the result is greater than or equal to `10000` recursively return the result of calling the same function on that.

If the result is less than `10000` and is one of the numbers in

``````small_multiples_of_137 = {137 * k for k in range(10000 // 137 + 1)}
``````

which are the multiples of `137` with no more than four digits, you return `True`. If it is less than `10000` and not on that set, return `False`. The test of being on that set or not can be replaced by some other way to test divisibility by `137` for small numbers. It depends on what is required. Maybe they are OK with doing `result % 137 == 0`, for small numbers.

2 Likes

I’d use the `divmod()` function, but maybe I’m not fully understanding the request for ‘help’.