Four fours is a mathematical puzzle. The goal of four fours is to find the simpl
ID: 3620689 • Letter: F
Question
Four fours is a mathematical puzzle. The goal of four fours is to find the simplest mathematical expression for every whole number from 0 to some maximum (max is 20), using only common mathematical symbols and the digit four (no other digit is allowed). Most versions of four fours require that each expression have exactly four fours, but some variations require that each expression have the minimum number of fours.I need help with writing a simple program to do this. I can use either c++ or Java language.
Explanation / Answer
Dear.. Am giving the sample code for "four fourd puzzle" in Python language: import time from math import ceil,fabs def combinations(size, values): if size == 1: return [[i] for i in values] half = combinations(size - 1, values) result = [] for value in values: for row in half: result.append([value] + row) return result def expressions(): # There are two versions of each term and operation - the pretty version, # to be shown to the user, and the version to be evaluated by Python all_terms = [ ('4', 4.0),('sqrt(4)', 2.0),('4!', 24.0),('.4', 0.4),('.4bar', 4.0 / 9.0), ('sqrt(.4bar)', 2.0 / 3.0)] all_operators = [('+', '+'),('-', '-'),('*', '*'),('/', '/'),('^', '**')] expression_combos = ['(%s %s %s) %s (%s %s %s)', # ( A x B ) x ( C x D ) '( (%s %s %s) %s %s) %s %s', # ( ( A x B ) x C ) x D '%s %s (%s %s %s) %s %s', # A x ( B x C ) x D '%s %s (%s %s (%s %s %s))' # A x ( B x ( C x D ) )] for association in expression_combos: for terms in combinations(4, all_terms): for operations in combinations(3, all_operators): values = ( terms[0][0],operations[0][0],terms[1][0],operations[1][0],terms[2][0], operations[2][0],terms[3][0]) # To be executed by Python py_values = (terms[0][1],operations[0][1],terms[1][1],operations[1][1],terms[2][1],operations[2][1],terms[3][1]yield (association % values, association % py_values) def main(): results = [None for i in range (0, 101)] start_time = time.time() iterations = 0 for expression, py_expression in expressions(): iterations = iterations + 1 try: result = eval(py_expression) if result > 0 and result 0: print ii,'=',expression print "Done. (%d loop iterations)" % iterations print "Elapsed time: ", (time.time() - start_time), " secondsRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.