from common import register D = 'number_theory' def add(n, c, t, s, e, **kw): register(D, n, c, t, s, e, **kw) add('gcd_lcm', [['Two positive integers have gcd {g} and lcm {l}; one of them is {a}.', 'The greatest common divisor is {g}, the least common multiple is {l}, and one integer is {a}.', 'For two positive integers, their gcd equals {g}, their lcm equals {l}, and the first equals {a}.']], ['What is the other integer?', 'Find the second integer.', 'Determine the remaining integer.'], lambda r: (lambda g, u, v: dict(g=g, l=g * u * v, a=g * u))(r.randint(2, 25), r.choice([4, 8, 16]), r.choice([9, 27, 81])), [('product', 'g*l'), ('answer', 'product/a')], level='easy') add('factorial_valuation', [['Let N = {n}! and let p = {p}, a prime.', 'Take the factorial {n}! and the prime {p}.', 'Use N = {n} factorial, with prime divisor p = {p}.']], ['What is the largest exponent e for which p^e divides N?', 'Find the exponent of p in the prime factorization of N.', 'Determine the p-adic valuation of N.'], lambda r: dict(n=r.randint(20, 180), p=r.choice([2, 3, 5, 7, 11])), [('answer', 'floor(n/p)+floor(n/p**2)+floor(n/p**3)+floor(n/p**4)+floor(n/p**5)+floor(n/p**6)+floor(n/p**7)')], level='medium', depth=3) add('divisor_count', [['Let N = 2^{a} * 3^{b} * 5^{c}.', 'The positive integer N has prime factorization 2^{a} * 3^{b} * 5^{c}.', 'Define N as the product 2^{a}, 3^{b}, and 5^{c}.']], ['How many positive divisors does N have?', 'Count all positive divisors of N.', 'Determine the number of positive factors of N.'], lambda r: dict(a=r.randint(1, 12), b=r.randint(1, 10), c=r.randint(1, 8)), [('two_prime_choices', '(a+1)*(b+1)'), ('answer', 'two_prime_choices*(c+1)')], level='easy') add('divisor_sum', [['Let N = 2^{a} * 3^{b}.', 'The prime factorization of N is 2^{a} * 3^{b}.', 'Define the integer N by multiplying 2^{a} and 3^{b}.']], ['What is the sum of all positive divisors of N?', 'Add all positive divisors of N.', 'Determine the sum of the positive factors of N.'], lambda r: dict(a=r.randint(1, 18), b=r.randint(1, 16)), [('sum_two_powers', '2**(a+1)-1'), ('answer', 'sum_two_powers*(3**(b+1)-1)/2')], level='medium', depth=3) add('factorial_base_zeros', [['Write {n}! in base {b}, where {b} = 2^{u} * 3^{v}.', 'Use base {b} = 2^{u} * 3^{v} to represent {n}!.', 'The factorial {n}! is expressed in the numeral base {b}, factored as 2^{u} * 3^{v}.']], ['How many trailing zero digits does it have?', 'Find the number of zeros at the end of this base representation.', 'Determine its trailing-zero count.'], lambda r: (lambda n, u, v: dict(n=n, u=u, v=v, b=2 ** u * 3 ** v))(r.randint(20, 180), r.randint(1, 4), r.randint(1, 4)), [('twos', 'floor(n/2)+floor(n/4)+floor(n/8)+floor(n/16)+floor(n/32)+floor(n/64)+floor(n/128)'), ('threes', 'floor(n/3)+floor(n/9)+floor(n/27)+floor(n/81)'), ('answer', 'Min(floor(twos/u),floor(threes/v))')], level='hard', depth=4, concepts=3) add('coprime_count', [['Let N = {p}^2 * {q}^2, with distinct primes {p} and {q}.', 'Use N = {p} squared times {q} squared, where {p} and {q} are distinct primes.', 'The integer N equals {p}^2 * {q}^2; its two distinct prime factors are {p} and {q}.']], ['How many integers from 1 through N are coprime to N?', 'Count positive integers at most N whose gcd with N is one.', 'Determine the number of integers in [1,N] relatively prime to N.'], lambda r: (lambda p, q: dict(p=p, q=q))(*r.sample([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53], 2)), [('N', 'p*p*q*q'), ('answer', 'p*q*(p-1)*(q-1)')], level='easy')