slinkhi
slinkhi

Reputation: 989

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..)

Upvotes: 3

Views: 2962

Answers (3)

Jack
Jack

Reputation: 11

All of these comments are good feedback, but most of them have a flaw at 2 or 1. This equation works, but you need to understand sigmas, so look that up and this works.

               x-1.         x-1
           Y=(2sigma x+1)+(sigma x+1)-x
               n=0.         n=0

This means that your adding x to x-1 and addition of this to x-2 etc.

until you reach zero and multiplying it by two. This will find the number of cards for the crossing cards. The other part finds the cards for the flat cards. It does this by adding x+x-1+x-1 etc.

until you reach zero like the top, but then subtracts x, because as the number of layers increases, you will have to take away one card from each flat layer, and since x is the number of layers, this works every time.

Upvotes: 1

Robby Cornelissen
Robby Cornelissen

Reputation: 97282

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.

Upvotes: 6

user9141233
user9141233

Reputation:

I am not sure about the algebraic formula but got a pattern

start = 2 (basic 1 level) then as and how you need to increase a level the extra cards will be 5 (level 2) so total is 5+2=7, then for level 3 you need 8(level 3) cards extra from level 2 that would be 7+8=15. So summing up we can write this, basically it forms a simple progression. we need to find the sum of progression. Here is a formula for number of cards for a given height or level,

    2 + ((10+(n-1)3)*(n/2))

    where n is the level or height.

example : level 1, n=1 => 7

  /_\  -> level 1
 /\ /\ -> level 0

example : level 2, n=2 => 15

  /_\     -> level 2
 /_\/_\   -> level 1
/ \/ \/ \ -> level 0

Upvotes: -1

Related Questions