def test_add(self): collection = MpNumberCollection() # Test add variant variant = MpNumberVariant(27648, (10, 3, 0, 0)) collection.add(variant) assert variant.digit_count in collection.mp_numbers assert collection.get(variant.digit_count).variants == [variant] # Test when there is an existing mp_number variant2 = MpNumberVariant(84672, (6, 3, 0, 2)) collection.add(variant2) assert variant2 in collection.get(variant.digit_count).variants assert collection.count() == 1 # Ensure error is thrown when overwriting a mp_number # Test add number collection = MpNumberCollection() mp_number = MpNumber(variants=[variant]) collection.add(mp_number) assert collection.count() == 1 assert collection.mp_numbers == {mp_number.digit_count: mp_number} # Ensure that error is raised with unknown add type with pytest.raises(Exception): collection.add(1)
def test_collapse_prime_factor(self): prime_factorization = MpNumberVariant.prime_factor(27648) assert MpNumberVariant._collapse_prime_factor(prime_factorization) == [ 10, 3, 0, 0, ]
def test_str(self): variant = MpNumberVariant(27648, (10, 3, 0, 0)) number = MpNumber(variants=[variant]) assert str(number) == "1,0,1,0,1,1,1,0: [2^10 * 3^3 * 5^0 * 7^0]" variant2 = MpNumberVariant(84672, (6, 3, 0, 2)) number.add_variant(variant2) assert ( str(number) == "1,0,1,0,1,1,1,0: [2^10 * 3^3 * 5^0 * 7^0, 2^6 * 3^3 * 5^0 * 7^2]")
def explore(self, start, end): """There are two cubes that need to be expanded: 2^x, 3^y, 7^z and 3^x, 5^y, and 7^z""" assert start < end, "start value must be less than end value" assert 0 <= start < end, "start and end values must be greater than 0" tic = time.time() # The 2, 3, 7 search space. prisms = [ # [2 range, 3 range, 7 range], # expanded cube [[start, end], [start, end], [start, end]], # longs [[start, end], [start, end], [0, start]], [[0, start], [start, end], [start, end]], [[start, end], [0, start], [start, end]], # faces [[start, end], [0, start], [0, start]], [[0, start], [start, end], [0, start]], [[0, start], [0, start], [start, end]], ] for prism in prisms: for t in range(*prism[0]): tp = 2**t for th in range(*prism[1]): thp = 3**th for s in range(*prism[2]): sp = 7**s num = tp * thp * sp if "0" not in str(num): self.collection.add( MpNumberVariant(num, [t, th, 0, s])) ## # The 5, 3, 7 search space. # We've already searched when 5 to the zeroeth power, so we can skip it here. for prism in prisms: # If the five line (first index 0) starts (second index 0) at zero, set it to 1. if prism[0][0] == 0: prism[0][0] = 1 for prism in prisms: for t in range(*prism[0]): tp = 5**t for th in range(*prism[1]): thp = 3**th for s in range(*prism[2]): sp = 7**s num = tp * thp * sp if "0" not in str(num): self.collection.add( MpNumberVariant(num, [0, th, t, s])) self.run_time = round(time.time() - tic, 2)
def test_add_variant(self): # Ensure that only variants with identical digit counts can be added variant = MpNumberVariant(27648, (10, 3, 0, 0)) number = MpNumber(variants=[variant]) variant2 = MpNumberVariant(84672, (6, 3, 0, 2)) number.add_variant(variant2) assert variant2 in number.variants assert len(number.variants) == 2 variant3 = MpNumberVariant(5, (0, 0, 1, 0)) with pytest.raises(Exception): number.add_variant(variant3)
def read_json(self, path): with open(path, "r") as f: mp_numbers = json.load(f) for digit_count, variant_list in mp_numbers.items(): for variant_l in variant_list: self.add(MpNumberVariant(*variant_l))
def test_base_10(self): variant = MpNumberVariant(5, (0, 0, 1, 0)) assert variant.base_10 == 5 assert variant.two_power == 0 assert variant.three_power == 0 assert variant.five_power == 1 assert variant.seven_power == 0
def test_contains(self): collection = MpNumberCollection() assert not collection.contains("1,0,1,0,1,1,1,0") variant = MpNumberVariant(27648, (10, 3, 0, 0)) collection.add(variant) assert collection.contains("1,0,1,0,1,1,1,0")
def test_init(self): variant = MpNumberVariant(27648, (10, 3, 0, 0)) number = MpNumber(variants=[variant]) assert number.variants == [variant] assert number.digit_count == variant.digit_count # Test blank init empty_number = MpNumber() assert empty_number.digit_count == None assert empty_number.variants == []
def test_prime_factor(self): assert MpNumberVariant.prime_factor(n=27648) == [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, ] assert MpNumberVariant.prime_factor(n=27648, collapse=True) == [10, 3, 0, 0] assert MpNumberVariant.prime_factor(n=4723) == []
def test_to_list(self): variant = MpNumberVariant(5, (0, 0, 1, 0)) assert variant.to_list() == [5, (0, 0, 1, 0)]
def test_digit_count(self): assert MpNumberVariant.digit_count(1) == "0,0,0,0,0,0,0,0" assert MpNumberVariant.digit_count(3268642167) == "2,1,1,0,3,1,1,0" variant = MpNumberVariant(27648, (10, 3, 0, 0)) variant.digit_count == "1,0,1,0,1,1,1,0"
def test_str(self): variant = MpNumberVariant(5, (0, 0, 1, 0)) assert str(variant) == "2^0 * 3^0 * 5^1 * 7^0"