# Energy levels of electrons [closed]

## Energy levels of electrons

Electrons are ordered into "shells". However, sometimes electrons get excited, for example by heat, and leave their shell to go to another! These electrons will go back at some point, but they cannot go back to their original shell without emitting energy in the form of radio waves.

For example, the energy levels for hydrogen look like this:

The base energy level of an electron in hydrogen is at n = 1. If it gets 'excited', it might go to n = 4. However, on the way back, the electron does not always simply go from n = 4 to n = 1, it has different options. See the example.

Write a program or function that takes an integer and outputs the amount of options the electron has to get back to its un-excited state. For this task, you can take granted that an electron can only emit energy from its excited state. It will not get more energy once it has been excited.

This is , the answer with the least amount of bytes wins.

If you are interested in these type of questions, check out this similar question. Also check out this for more info on these matters.

## Input

The input of your function/program is a whole integer. It is greater than 0 and less than 2^31.

## Output

An integer, the amount of options the electron has to return to its original state, which is n = 1 (for hydrogen). When the input is 1, return 0, as it is already in its original energy level.

## Examples

Input: 4
Output: 4


Because the options are:

4 -> 3 -> 2 -> 1
4 -> 3 -> 1
4 -> 2 -> 1
4 -> 1


Visualised:

## closed as off-topic by isaacg, Blue, Blackhole, Stewie Griffin, Mego♦Oct 22 '15 at 12:36

• This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

• When I saw "the electron does not always simply go from n = 4 to n = 1" i.imgur.com/FNxsSSC.png – DanTheMan Oct 21 '15 at 16:00
• @DanTheMan I didn't get the joke... – Cows quack Oct 21 '15 at 18:46
• The simple mathematical expression for the solution makes the whole problem rather silly. Before posting a challenge, you should write code to generate test cases, and here it would have let you catch the pattern. – xnor Oct 21 '15 at 19:34
• To add to what @xnor said, it's also a good idea to post challenges to the Sandbox. That way people can offer useful feedback before the challenge goes live. – Alex A. Oct 22 '15 at 5:26
• I'm voting to close this question as off-topic because it's too narrow - there's a simple mathematical formula to solve it, no ingenuity involved. – isaacg Oct 22 '15 at 8:11

# Pyth, 5 bytes

/^2Q4


I integer-divide by 4 instead of subtracting two from the power and flooring.

# CJam, 68 7 bytes

1li2-m<


Try it online

Unless I completely misunderstood the question, the result is simply 2^(n-2). For each level between n and 1, you can either skip or not skip it on the way back.

Using the left shift operator instead of the power operator is one byte more, but gives the specified result 0 for n=1, while the power operator gives 0.5 for that case.

Explanation:

1     Push 1 for later shift operator.
li    Get input and convert to int.
2-    Subtract 2.
m<    Left-shift 1 by (n-2).


If I shamelessly stole Thomas Kwa's approach, this would be 6 bytes in CJam:

2li#4/

• Ouch. Did not realize the answer was that simple. Nice work though. – Thomas W Oct 21 '15 at 15:47

# Dyalog APL, 6 bytes

⌊2*-∘2


This is a monadic function train. It is equivalent to the following, trainless function.

{⌊2*⍵-2}


Try it online on TryAPL.

### How it works

   -∘2  Take the right argument and subtract 2.
2*     Elevate 2 to the difference.
⌊       Round down to the nearest integer.

• Note to self: If you beat Dennis to a CJam answer, he's just going to find something else that's shorter. ;) – Reto Koradi Oct 21 '15 at 16:02
• 6 bytes? What character encoding are you using? – ev3commander Oct 21 '15 at 19:10
• @ev3commander There are legacy APL code pages (that predate Unicode by a few decades) where one character is one byte. – Dennis Oct 21 '15 at 19:29

# JavaScript ES7, 12 11 bytes

i=>2**(i-2)


Defines an anonymous function.

• Anonymous functions are fine. I'm pretty sure that ^ is bitwise XOR though, not power. – Dennis Oct 21 '15 at 17:07
• @Dennis Frick, you're right! Gotta add another byte :( – DanTheMan Oct 21 '15 at 17:14
• Gives the wrong result for i=1 – Peter Taylor Oct 21 '15 at 17:30
• the ** Opererator is ES7 and presently supported in almost nothing – Shaun H Oct 21 '15 at 17:41
• i=>(1<<i)>>2 1 char more, works everywhere, correct for i=1 – edc65 Oct 22 '15 at 0:14

## Python, 2922 16 bytes

lambda n:2**n//4


Technique shamelessly stolen from Thomas's answer. Try it online

# Stuck, 6 bytes

2i2-^)


Hey look I almost outgolfed Pyth outgolfed CJam

• Does Stuck implicitly floor? Interesting. – lirtosiast Oct 21 '15 at 16:44
• I didn't execute this, but from the documentation, 2^ seems to square instead of calculating a power of 2 – Dennis Oct 21 '15 at 16:46
• ^ pops off two values x and y and calculates x^y. So this does do power of 2 not square. – a spaghetto Oct 21 '15 at 16:48
• I wish Stuck had an online interpreter... – a spaghetto Oct 21 '15 at 16:48
• @ThomasKwa Actually it appears to implicitly ceiling since an input of 1 outputs 1. I'll have to fix that... – a spaghetto Oct 21 '15 at 16:49

# dc, 6

2?^4/p


Algorithm expertly lifted from other correct answers.

### Testcases:

$for t in 1 4; do dc -e'2?^4/p' <<<$t; done
0
4
$ # Julia, 9 bytes n->2^n÷4  Like several other answers, this uses Thomas's approach. ÷ performs integer division in Julia (but unfortunately is 2 bytes in UTF-8). • Does Julia support the ISO 8859-1 encoding? – Dennis Oct 21 '15 at 18:12 • @Dennis I have no idea. – Alex A. Oct 21 '15 at 18:24 # PHP, 28 bytes I hope I got it right. floor handles the case for 1 where it should output 0 <?=floor(pow(2,$argv[1]-2));


Works from command line like:

electrons.php 4


+1 and thanks to Reto Koradi for providing the formula. ;)

• Note that you can left-shift 1 to simulate a power of 2 (as is the nature of binary). The result is necessarily an integer. For the n=1 case, this would imply a negative number - so you can simply compare with zero. Suggestion: <?=max(0,1<<\$argv[1]-2); (24 chars) – JArkinstall Oct 21 '15 at 17:08
• Never mind. This doesn't work with the full range defined by the question. Neither does pow, as it assumes large numbers are infinity, but it works for a larger scope than the left shift. If PHP allowed one to specify a var as a long, that would be more useful. – JArkinstall Oct 21 '15 at 17:15
• @JArkinstall Yeah, even with 64 bit int it will return INF. It's not made for this domain. – insertusernamehere Oct 21 '15 at 19:58

## Minkolang 0.9, 7 bytes

Copying Thomas like everyone else...

2n;4:N.


This is a full program, and the first five characters are equivalent to 2**n//4 in Python 3. Try it online.