25
3
Backstory
Disclaimer: May contain made up information about kangaroos.
Kangaroos traverse several stages of development. As they grow older and stronger, they can jump higher and longer, and they can jump more times before they get hungry.
In stage 1, the kangaroo is very little and cannot jump at all. Despite this, is constantly requires nourishment. We can represent a stage 1 kangaroo's activity pattern like this.
o
In stage 2, the kangaroo can make small jumps, but not more than 2 before it gets hungry. We can represent a stage 2 kangaroo's activity pattern like this.
o o
o o o
After stage 2 the kangaroo improves quickly. In each subsequent stage, the kangaroo can jump a bit higher (1 unit in the graphical representation) and twice as many times. For example, a stage 3 kangaroo's activity pattern looks like this.
o o o o
o o o o o o o o
o o o o o
All that jumping requires energy, so the kangaroo requires nourishment after completing each activity pattern. The exact amount required can be calculated as follows.
Assign each o in the activity pattern of a stage n kangaroo its height, i.e., a number from 1 to n, where 1 corresponds to the ground and n to the highest position.
Compute the sum of all heights in the activity pattern.
For example, a stage 3 kangaroo's activity pattern includes the following heights.
3 3 3 3
2 2 2 2 2 2 2 2
1 1 1 1 1
We have five 1's, eight 2's, and four 3's; the sum is 5·1 + 8·2 + 4·3 = 33.
Task
Write a full program or a function that takes a positive integer n as input and prints or returns the nutritional requirements per activity of a stage n kangaroo.
This is code-golf; may the shortest answer in bytes win!
Examples
1 -> 1
2 -> 7
3 -> 33
4 -> 121
5 -> 385
6 -> 1121
7 -> 3073
8 -> 8065
9 -> 20481
10 -> 50689
15I downvoted because I don't like challenges where a complicated setup comes down to a straightforward formula to golf. – xnor – 2016-10-16T08:55:44.623
3While all answers so far have used the formula, I'm convinced that there are other ways to attack the problem. – Dennis – 2016-10-16T16:13:03.723
2Is there a challenge to generate the ascii art output of this sequence? – miles – 2016-10-16T18:01:58.843
@miles Not sure. Kinda hard to search for. – Dennis – 2016-10-16T18:04:02.077
Wolfram Alpha could not find a shorter version,
http://www.wolframalpha.com/input/?i=2%5E(n-1)*(n%5E2-1)%2B1
(Weird markup because a regular URL gets messed up) – Konijn – 2016-10-18T13:59:14.667@Konijn Certain chars need to be escaped or converted to
– mbomb007 – 2016-10-18T14:06:30.990%[hex value]
. Also, you should use HTTPS in your links. https://www.wolframalpha.com/input/?i=2%5E(n-1)%2A(n%5E2-1)%2B1@miles There is now: Leaping Kangaroos
– Dennis – 2017-01-21T20:12:33.930