def filter_correct_digits(products, target): if isinstance(target, INT_TYPES): target = IntegerBase.from_int(target) legit = [] for product in products: fixed_digits = product.factor_num_digits prod_to_match = product.as_integer_base.digits target_to_match = target.digits # Pad the digits so we have enough to compare with while len(prod_to_match) < len(target_to_match): prod_to_match.append(0) prod_to_match = prod_to_match[:fixed_digits] target_to_match = target_to_match[:fixed_digits] #print prod_to_match, target_to_match if prod_to_match == target_to_match: legit.append(product) return legit
def filter_digit_length(products, target): if isinstance(target, INT_TYPES): target = IntegerBase.from_int(target) legit = [] max_fact_digits = (target.num_digits / 2) + 1 for product in products: if product.num_digits > target.num_digits: continue if product.factor_num_digits > max_fact_digits: continue legit.append(product) return legit
def product_to_error(product, target): if isinstance(target, INT_TYPES): target = IntegerBase.from_int(target) # Extract the digits and pad product_digits = product.as_integer_base.digits[:] target_digits = target.digits[:] while len(product_digits) < len(target_digits): product_digits.append(0) while len(product_digits) > len(target_digits): target_digits.append(0) rev_prod = sum([digit * (BASE ** ind) for ind, digit in enumerate(product_digits[::-1])]) rev_targ = sum([digit * (BASE ** ind) for ind, digit in enumerate(target_digits[::-1])]) diff = abs(rev_prod - rev_targ) #print '\n'.join(map(str, [product, target, diff])) #print return diff