Energy levels of electrons

1

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:

Hydrogen energy levels

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.

Task

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:

enter image description here

Thomas W

Posted 2015-10-21T15:26:26.563

Reputation: 746

Question was closed 2015-10-22T12:36:15.647

18

When I saw "the electron does not always simply go from n = 4 to n = 1" http://i.imgur.com/FNxsSSC.png

– DanTheMan – 2015-10-21T16:00:46.600

@DanTheMan I didn't get the joke... – user41805 – 2015-10-21T18:46:51.777

8The 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 – 2015-10-21T19:34:31.623

1

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. – 2015-10-22T05:26:40.990

Thanks for the feedback, ill make use of your proposals next time. – Thomas W – 2015-10-22T07:49:56.177

5I'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 – 2015-10-22T08:11:25.477

Answers

8

Pyth, 5 bytes

/^2Q4

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

lirtosiast

Posted 2015-10-21T15:26:26.563

Reputation: 20 331

7

CJam, 6 8 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/

Reto Koradi

Posted 2015-10-21T15:26:26.563

Reputation: 4 870

Ouch. Did not realize the answer was that simple. Nice work though. – Thomas W – 2015-10-21T15:47:45.897

4

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.

Dennis

Posted 2015-10-21T15:26:26.563

Reputation: 196 637

8Note to self: If you beat Dennis to a CJam answer, he's just going to find something else that's shorter. ;) – Reto Koradi – 2015-10-21T16:02:02.030

6 bytes? What character encoding are you using? – ev3commander – 2015-10-21T19:10:57.827

@ev3commander There are legacy APL code pages (that predate Unicode by a few decades) where one character is one byte. – Dennis – 2015-10-21T19:29:46.170

2

JavaScript ES7, 12 11 bytes

i=>2**(i-2)

Defines an anonymous function.

DanTheMan

Posted 2015-10-21T15:26:26.563

Reputation: 3 140

Anonymous functions are fine. I'm pretty sure that ^ is bitwise XOR though, not power. – Dennis – 2015-10-21T17:07:07.827

@Dennis Frick, you're right! Gotta add another byte :( – DanTheMan – 2015-10-21T17:14:15.060

5Gives the wrong result for i=1 – Peter Taylor – 2015-10-21T17:30:05.343

3the ** Opererator is ES7 and presently supported in almost nothing – Shaun H – 2015-10-21T17:41:05.433

1i=>(1<<i)>>2 1 char more, works everywhere, correct for i=1 – edc65 – 2015-10-22T00:14:08.947

1

Python, 29 22 16 bytes

lambda n:2**n//4

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

Mego

Posted 2015-10-21T15:26:26.563

Reputation: 32 998

1

Stuck, 6 bytes

2i2-^)

Hey look I almost outgolfed Pyth outgolfed CJam

a spaghetto

Posted 2015-10-21T15:26:26.563

Reputation: 10 647

Does Stuck implicitly floor? Interesting. – lirtosiast – 2015-10-21T16:44:47.157

I didn't execute this, but from the documentation, 2^ seems to square instead of calculating a power of 2 – Dennis – 2015-10-21T16:46:25.627

^ pops off two values x and y and calculates x^y. So this does do power of 2 not square. – a spaghetto – 2015-10-21T16:48:00.617

1I wish Stuck had an online interpreter... – a spaghetto – 2015-10-21T16:48:16.910

@ThomasKwa Actually it appears to implicitly ceiling since an input of 1 outputs 1. I'll have to fix that... – a spaghetto – 2015-10-21T16:49:36.237

That prints 1 because you're squaring. x y ^ calculates x^y, not y^x. – Dennis – 2015-10-21T16:56:56.780

Oh, you are correct. I got confused because my test case was 4... probably a bad choice. Fixed – a spaghetto – 2015-10-21T17:01:21.017

1

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
$ 

Digital Trauma

Posted 2015-10-21T15:26:26.563

Reputation: 64 644

1

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

Alex A.

Posted 2015-10-21T15:26:26.563

Reputation: 23 761

Does Julia support the ISO 8859-1 encoding? – Dennis – 2015-10-21T18:12:29.407

@Dennis I have no idea. – Alex A. – 2015-10-21T18:24:33.980

0

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

insertusernamehere

Posted 2015-10-21T15:26:26.563

Reputation: 4 551

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) – Jake – 2015-10-21T17:08:56.213

1Never 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. – Jake – 2015-10-21T17:15:04.737

@JArkinstall Yeah, even with 64 bit int it will return INF. It's not made for this domain. – insertusernamehere – 2015-10-21T19:58:32.190

0

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.

El'endia Starman

Posted 2015-10-21T15:26:26.563

Reputation: 14 504