Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1.
You start to eat the chocolates. After eating a chocolate you leave only a wrapper.
You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one.
More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).
You stop eating when you encounter an empty wrapper.
For example, given integers N = 10 and M = 4. You will eat the following chocolates: 0, 4, 8, 2, 6.
The goal is to count the number of chocolates that you will eat, following the above rules.
Write a function:
def solution(N, M)
that, given two positive integers N and M, returns the number of chocolates that you will eat.
For example, given integers N = 10 and M = 4. the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..1,000,000,000].
Invalid result type, int expected, <class 'float'> found.
Invalid result type, int expected, <class 'NoneType'> found.stdout:
5.0
Invalid result type, int expected, <class 'NoneType'> found.stdout:
5
The following issues have been detected: runtime errors.
For example, for the input (1000000000, 1) the solution terminated unexpectedly.
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( N, M ) File "/tmp/solution.py", line 6, in solution gcf = gcd(N, M) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) [Previous line repeated 992 more times] File "/tmp/solution.py", line 14, in gcd if M == N: RecursionError: maximum recursion depth exceeded in comparison
maximal and minimal values
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( N, M ) File "/tmp/solution.py", line 6, in solution gcf = gcd(N, M) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) [Previous line repeated 992 more times] File "/tmp/solution.py", line 14, in gcd if M == N: RecursionError: maximum recursion depth exceeded in comparison
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( N, M ) File "/tmp/solution.py", line 6, in solution gcf = gcd(N, M) File "/tmp/solution.py", line 19, in gcd return gcd(N - M, M) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) File "/tmp/solution.py", line 17, in gcd return gcd(M - N, N) [Previous line repeated 991 more times] File "/tmp/solution.py", line 14, in gcd if M == N: RecursionError: maximum recursion depth exceeded in comparison