1
\$\begingroup\$

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

\$\endgroup\$

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.

  • 18
    \$\begingroup\$ When I saw "the electron does not always simply go from n = 4 to n = 1" i.imgur.com/FNxsSSC.png \$\endgroup\$ – DanTheMan Oct 21 '15 at 16:00
  • \$\begingroup\$ @DanTheMan I didn't get the joke... \$\endgroup\$ – Cows quack Oct 21 '15 at 18:46
  • 8
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Oct 21 '15 at 19:34
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Alex A. Oct 22 '15 at 5:26
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ – isaacg Oct 22 '15 at 8:11

10 Answers 10

8
\$\begingroup\$

Pyth, 5 bytes

/^2Q4

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

\$\endgroup\$
7
\$\begingroup\$

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/
\$\endgroup\$
  • \$\begingroup\$ Ouch. Did not realize the answer was that simple. Nice work though. \$\endgroup\$ – Thomas W Oct 21 '15 at 15:47
4
\$\begingroup\$

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

JavaScript ES7, 12 11 bytes

i=>2**(i-2)

Defines an anonymous function.

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

Python, 29 22 16 bytes

lambda n:2**n//4

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

\$\endgroup\$
1
\$\begingroup\$

Stuck, 6 bytes

2i2-^)

Hey look I almost outgolfed Pyth outgolfed CJam

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

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
$ 
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Does Julia support the ISO 8859-1 encoding? \$\endgroup\$ – Dennis Oct 21 '15 at 18:12
  • \$\begingroup\$ @Dennis I have no idea. \$\endgroup\$ – Alex A. Oct 21 '15 at 18:24
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – JArkinstall Oct 21 '15 at 17:08
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – JArkinstall Oct 21 '15 at 17:15
  • \$\begingroup\$ @JArkinstall Yeah, even with 64 bit int it will return INF. It's not made for this domain. \$\endgroup\$ – insertusernamehere Oct 21 '15 at 19:58
0
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.