def root(draw, max_value=None): return draw(st.integers( min_value=2, max_value=max_value ).filter( lambda x: not is_square(x) ))
def test_square_and_square_free(number): factorization = factor(number) square, square_free = square_and_square_free(factorization) square_number = number_from_factorization(square) square_free_number = number_from_factorization(square_free) if square != dict(): assert set(map(lambda v: v % 2, square.values())) == set([0]) if square_free != dict(): assert set(square_free.values()) == set([1]) assert combine_counters(square, square_free) == factorization assert is_square(square_number) assert square_number * square_free_number == number
@given(st.integers(min_value=1)) def test_bakshali(number): bak = bakhshali_gen(number) bab = babylonian_gen(number) for i in range(4): assert next(bab) == next(bak) next(bab) #----------------------------- @given( st.integers(min_value=2, max_value=10**5).filter(lambda x: not is_square(x))) def test_continued_fraction_convergent(number): gen = continued_fraction_convergent_gen(number) pell_numbers = continued_fraction_pell_numbers(number) last = pell_numbers[-1] period = len(pell_numbers) prev = next(gen) curr = next(gen) diff = abs(curr - prev) counter = 1 for i in range(50): pell = curr.numer**2 - number * curr.denom**2 expected = last**(counter // period) * pell_numbers[counter % period] assert pell == expected
def root_filter(x): return x < 0 or not is_square(x)
st.integers(min_value=0, max_value=5), ) def test_pow(a, m, n): mth_power = a**m nth_power = a**n sum_power = a**(m + n) assert type(a**0) is Polynomial assert type(a**1) is Polynomial assert type(mth_power) is Polynomial assert a**0 == 1 assert a**1 == a assert a**2 == a * a assert mth_power * nth_power == sum_power assert sum_power.div_with_remainder(mth_power) == (nth_power, 0) # ============================= @given(*(4 * [rational(nonzero)]), st.integers().filter(lambda x: x < 0 or not is_square(x))) def test_quadratic(a1, b1, a2, b2, d): g1 = Quadratic(a1, b1, d) g2 = Quadratic(a2, b2, d) p1 = Polynomial({0: a1, 1: b1}) p2 = Polynomial({0: a2, 1: b2}) m = Polynomial({0: -d, 2: 1}) g = g1 * g2 p = Polynomial({0: g.real, 1: g.imag}) assert (p1 * p2) % m == p