javascriptalgebra

calculate number of cards in a house of cards


Backstory:

So my son randomly came up to me and asked how to write an algebraic (?) expression to calculate how many cards it will take to make a house of cards of based on number of levels specified. I'm fairly certain this isn't actually a homework question; he's generally pretty good about showing me a worksheet if he needs help on it.

The "assumptions" are that

a) There are cards laid horizontal between each layer, for support; one card as a "bridge" between two "peaks" b) The lowest (ground level) layer does not have any horizontal cards underneath it, because the floor/table itself is the support.

So for example, a single layer just has 2 cards: /\

Another example: 3 layers have a total of 15 cards (please forgive my crappy picture!)

   /_\
 /_\ /_\
/ \/ \/ \

Well, I'm terrible at this sort of thing. I only know basic algebra, but I'd like to consider myself a fairly decent (but not "pro") programmer. But anyways, for a purely algebraic formula, I was unable to come up with a formula to calculate all cards, but I did manage to come up with a formula that calculates the number of cards for a given layer, based on the assumptions above: 2v + (v - 1)

Well, there's probably a better/simplified equation, and again, this is for just a given layer, not all layers. So for example layer 1 (the top layer), v=1 and so the number of cards is 2. Or for the 3rd layer, v=3 and so the number of cards works out to 8 (so this formula counts the horizontal supports as part of the given layer).

And.. that's as far as I got with pure algebra. However! With some computer code (javascript is what I used), I was able to create a function based on the above equation, to return the number of cards:

function getNumHouseCards(layers) {
    var layers = layers || 1;
    var cards = 0;
    for (var v = 1; v <= layers; v++) {
        cards += (2 * v) + (v - 1);
    }
    return cards;
}

I did some manual counting of cards compared to the returned value from the function, and this seems legit, so I should be happy, but.. I feel like this can be done better. So..

Question:

How can this be improved? I guess firstly, is there a way to write this solely with an algebraic expression, or is coding really required to answer something like this? And in either case, I definitely feel like I've over-complicated it (well, at the least the part I did work out..)


Solution

  • This expression covers it:

    expression

    (y represents the number of cards, x represents the number of layers)


    Implemented in JavaScript, and comparing the output with the output of your method:

    function yourNumberOfCards(layers) {
        var layers = layers || 1;
        var cards = 0;
        for (var v = 1; v <= layers; v++) {
            cards += (2 * v) + (v - 1);
        }
        return cards;
    }
    
    function myNumberOfCards(layers) {
      return (3 * Math.pow(layers, 2)) / 2 + (layers / 2);
    }
    
    for (let i = 1; i <= 10; i++) {
      const yours = yourNumberOfCards(i);
      const mine = myNumberOfCards(i);
      
      console.log(yours + (yours === mine ? " == " : " != ") + mine);
    }

    How I got there? I calculated the first couple of values, and then ran a polynomial interpolation on WolframAlpha.