cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A243312 The total T(d,n) of expressions with n instances of a digit d between 6 and 9 (and using the four arithmetic operators) which have a defined value when evaluated.

This page as a plain text file.
%I A243312 #24 May 22 2025 10:21:38
%S A243312 1,4,31,305,3345,39505,487935,6245118
%N A243312 The total T(d,n) of expressions with n instances of a digit d between 6 and 9 (and using the four arithmetic operators) which have a defined value when evaluated.
%C A243312 Bounded above by A151403 (which is equal to the *total* number of expressions, not just the ones that evaluate to a valid numeric value).
%C A243312 An expression has an undefined value when it contains a "division by zero" operation.
%C A243312 For 0 <= d <= 5 and d' > 5, it is possible that T(d,n) < T(d',n). See link below.
%C A243312 T(6,10) = 1097128463 < T(7,10) = 1097153419 < T(8,10) = 1097155971 < T(9,10) = 1097157195. - _Hiroaki Yamanouchi_, Oct 01 2014
%H A243312 David Lyness, <a href="https://davidlyness.com/post/experimenting-bracketing-binary-operators">Experimenting with bracketing and binary operators</a>
%e A243312 For n = 2 and digit d != 0, there are four possible expressions:
%e A243312 (d + d)
%e A243312 (d - d)
%e A243312 (d * d)
%e A243312 (d / d)
%e A243312 None of these include a "division by zero" operation, and so all four of the above expressions can be considered valid. Therefore, T(d, 2) = 4 for d > 0.
%o A243312 (Python)
%o A243312 # coding=utf-8
%o A243312  
%o A243312 import itertools
%o A243312  
%o A243312  
%o A243312 def all_expressions(generic_expression, operation_combinations):
%o A243312     """
%o A243312     Merges a source expression and combinations of binary operators to generate a list of all possible expressions.
%o A243312     @param ((str,)) operation_combinations: all combinations of binary operators to consider
%o A243312     @param str generic_expression: source expression with placeholder binary operators
%o A243312     @rtype: (str,)
%o A243312     """
%o A243312     expression_combinations = []
%o A243312     for combination in operation_combinations:
%o A243312         expression_combinations.append(generic_expression.format(*combination))
%o A243312     return expression_combinations
%o A243312  
%o A243312  
%o A243312 def all_bracketings(expr):
%o A243312     """
%o A243312     Generates all possible permutations of parentheses for an expression.
%o A243312     @param str expr: the non-bracketed source expression
%o A243312     @rtype: str
%o A243312     """
%o A243312     if len(expr) == 1:
%o A243312         yield expr
%o A243312     else:
%o A243312         for i in range(1, len(expr), 2):
%o A243312             for left_expr in all_bracketings(expr[:i]):
%o A243312                 for right_expr in all_bracketings(expr[i + 1:]):
%o A243312                     yield "({}{}{})".format(left_expr, expr[i], right_expr)
%o A243312  
%o A243312  
%o A243312 def num_valid_expressions(num_digits):
%o A243312     """Perform all calculations with the given operations and in the range of digits specified.
%o A243312     @param int num_digits: the number of digits in the expression
%o A243312     @rtype: int
%o A243312     """
%o A243312     operations = ["+", "-", "*", "/"]
%o A243312     digit = 9
%o A243312     operation_iterable = itertools.product(*[operations] * (num_digits - 1))
%o A243312     operation_combinations = []
%o A243312     valid_expression_count = 0
%o A243312     template = " ".join(str(digit) * num_digits)
%o A243312     for operation in operation_iterable:
%o A243312         operation_combinations.append(operation)
%o A243312     for bracketed_expression in all_bracketings(template):
%o A243312         for expression in all_expressions(bracketed_expression.replace(" ", "{}"), operation_combinations):
%o A243312             try:
%o A243312                 eval(expression)
%o A243312                 valid_expression_count += 1
%o A243312             except ZeroDivisionError:
%o A243312                 pass
%o A243312     return valid_expression_count
%o A243312  
%o A243312  
%o A243312 if __name__ == "__main__":
%o A243312     min_num_digits = 1
%o A243312     max_num_digits = 6
%o A243312     operation_set = ["+", "-", "*", "/"]
%o A243312     for num in range(min_num_digits, max_num_digits + 1):
%o A243312         print(num_valid_expressions(num))
%K A243312 nonn,base,uned,obsc,more
%O A243312 1,2
%A A243312 _David Lyness_, Jun 03 2014