human
What is a the Python code you requested given the following:
I found an interesting problem on https://leetcode.com/problems/nth-magical-number/:
A positive integer is magical if it is divisible by either A or B.
Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.
 


Example 1:
Input: N = 1, A = 2, B = 3
Output: 2

Example 2:
Input: N = 4, A = 2, B = 3
Output: 6

Example 3:
Input: N = 5, A = 2, B = 4
Output: 10

Example 4:
Input: N = 3, A = 6, B = 4
Output: 8

 
Note:

1 <= N <= 10^9
2 <= A <= 40000
2 <= B <= 40000

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is the Python code you requested.

class Solution:
    def _lcm(self, a, b):
        return a * b // math.gcd(a, b)
    
    def _nth_magical_number_slow(self, n: int, a: int, b: int) -> int:
        if n == 0:
            return 0
        else:
            i, j = a, b
            count = 1
            while count < n:
                if i <= j:
                    i = i + a
                else:
                    j = j + b

                if i == j:
                    if a < b:
                        i = i + a
                    else:
                        j = j + b

                count += 1

            return min(i, j) % 1000000007
    
    def nthMagicalNumber(self, n: int, a_orig: int, b_orig: int) -> int:
        a, b = min(a_orig, b_orig), max(a_orig, b_orig)
        if a == b:
            return a * n % 1000000007
        else:
            lcm = self._lcm(a, b)
            k = lcm // a + lcm // b - 1
            return ((n // k) * lcm + self._nth_magical_number_slow(n % k, a, b)) % 1000000007