Ruby solve classic math problem: make 56 from four 4s

Discussion in 'Ruby' started by Kelly Jones, Mar 31, 2010.

  1. Kelly Jones

    Kelly Jones Guest

    A classic math problem asks "using only four 4s and any
    mathematical operation, come up with the number
    56". "4!+4!+4+4" is one of probably several answers [1].

    I want to write a ruby program that solves the
    generalization: "given X number of the digit Y and a
    set of permitted mathematical operations/functions, can
    you come up with the number Z?"

    Has anyone already done this?

    [1] http://mathforum.org/ruth/four4s.puzzle.html

    --
    We're just a Bunch Of Regular Guys, a collective group that's trying
    to understand and assimilate technology. We feel that resistance to
    new ideas and technology is unwise and ultimately futile.
     
    Kelly Jones, Mar 31, 2010
    #1
    1. Advertising

  2. On Thu, Apr 1, 2010 at 6:11 AM, Kelly Jones <> wrote:
    > A classic math problem asks "using only four 4s and any
    > mathematical operation, come up with the number
    > 56". "4!+4!+4+4" is one of probably several answers [1].
    >
    > I want to write a ruby program that solves the
    > generalization: "given X number of the digit Y and a
    > set of permitted mathematical operations/functions, can
    > you come up with the number Z?"
    >
    > Has anyone already done this?
    >
    > [1] http://mathforum.org/ruth/four4s.puzzle.html


    The first thing that came to mind was http://www.rubyquiz.com/quiz60.html
    Not exactly what you want, but still good reading :)

    > --
    > We're just a Bunch Of Regular Guys, a collective group that's trying
    > to understand and assimilate technology. We feel that resistance to
    > new ideas and technology is unwise and ultimately futile.



    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Apr 1, 2010
    #2
    1. Advertising

  3. 2010/3/31 Kelly Jones <>:
    > A classic math problem asks "using only four 4s and any
    > mathematical operation, come up with the number
    > 56". "4!+4!+4+4" is one of probably several answers [1].
    >
    > I want to write a ruby program that solves the
    > generalization: "given X number of the digit Y and a
    > set of permitted mathematical operations/functions, can
    > you come up with the number Z?"


    As long as you do not limit the number of unary operations that
    program is not guaranteed to terminate - especially if there is no
    solution for given X, Y and Z. Am I missing something?

    It should be interesting to see what solution strategy you are picking.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 1, 2010
    #3
  4. Brian Candler, Apr 1, 2010
    #4
  5. Kelly Jones

    Kelly Jones Guest

    On 4/1/10, Robert Klemme <> wrote:
    > 2010/3/31 Kelly Jones <>:
    >> A classic math problem asks "using only four 4s and any
    >> mathematical operation, come up with the number
    >> 56". "4!+4!+4+4" is one of probably several answers [1].
    >>
    >> I want to write a ruby program that solves the
    >> generalization: "given X number of the digit Y and a
    >> set of permitted mathematical operations/functions, can
    >> you come up with the number Z?"

    >
    > As long as you do not limit the number of unary operations that
    > program is not guaranteed to terminate - especially if there is no
    > solution for given X, Y and Z. Am I missing something?
    >
    > It should be interesting to see what solution strategy you are picking.


    Thanks, and thanks to everyone else who replied.

    You're right: I hadn't thought of this.

    If I start with 4 and allow sqrt, I get sqrt(4),
    sqrt(sqrt(4)), sqrt(sqrt(sqrt(4))) and so on. Of
    course, these aren't integers after the first sqrt, but
    they could theoretically combine with other 4
    combinations later to form an integer.

    I incorrectly thought one iteration of a unary operator would suffice.

    I originally got interested in this problem because I
    thought factorial was a cheat. Some of the solutions on
    http://mathforum.org/ruth/four4s.puzzle.html use the
    gamma function, integer 4th root, etc. Where do you
    draw the line? If you allow constant functions (eg,
    f(x) = 56), the solution is trivial.

    My new goal is to solve the simpler problem, very
    similar to http://www.rubyquiz.com/quiz7.html (thanks,
    Brian!).

    Given X copies of the digit Y and the 5 mathematical
    operators plus, minus, multiply, divide, and exponent,
    along with concatenation and decimals (see below), can
    you construct the number Z?

    My approach for 5 copies of the digit 7 (example):

    % With one 7, you have {7, 0.7} (the latter because we
    allow decimals-- but not 0.07)

    % With two 7s, we union two sets:

    % {77, 7.7, .77} (from decimals and concatenation)

    % Apply + - * / ^ to every ordered pair of elements
    in the resultset for one 7 (including "pairs" like
    {7,7}). Not showing the results, but you get the
    idea.

    % We then recurse. For n 7s, we union:

    % {777...[n times], 777...[n-1 times].7, 777...[n-2 times].77, etc}

    % Applying + - * / ^ to every ordered pair of the
    resultset for n-1.

    It might still be interesting to create a website that does this.

    --
    We're just a Bunch Of Regular Guys, a collective group that's trying
    to understand and assimilate technology. We feel that resistance to
    new ideas and technology is unwise and ultimately futile.
     
    Kelly Jones, Apr 1, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    798
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,174
    Jonathan Turkanis
    Apr 1, 2004
  3. Just
    Replies:
    25
    Views:
    3,000
    Cousin Stanley
    Mar 6, 2005
  4. why the lucky stiff
    Replies:
    12
    Views:
    207
    Josef 'Jupp' SCHUGT
    Mar 4, 2004
  5. VK
    Replies:
    15
    Views:
    1,318
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page