5
\$\begingroup\$

Covalent bonding uses so-called "hybrid" orbitals to form tetrahedral, octahedral, etc. formations out of the s, p, and sometimes d orbitals. Their naming conventions are pretty much the concatenation of the component orbitals; for example, the hybridization of the s orbital and one p orbital would be the two sp orbitals, and the hybridization of the s orbital, all three p orbitals, and 2 of the 5 d orbitals would be the 6 sp3d2 orbitals. Real-life hybrid orbitals rarely go beyond this, but of course we'll do it anyway.

The Challenge

Write a program that takes in the number of orbitals needed and outputs the name of the hybrid orbital as a string such as sp3d4. Superscripts can be omitted.

The standard orbitals, ordered by the order in which they're hybridized, are as follows:

letter   # orbitals
  s           1
  p           3
  d           5
  f           7

No orbital should be used unless all lower orbitals have already been consumed. For example spd2 is not a valid orbital hybridization because there is still a p orbital that is not used.

If you go beyond the f orbital, then use the angular momentum quantum number (the zero-indexed position in this series: 0 is s, 1 is p, etc.) in parentheses. For example, 27 would be sp3d5f7(4)9(5)2. The number of orbitals for each set should continue with the pattern of "twice the angular momentum quantum number plus one".

Also, don't list the number of orbitals used in the output if that would be 1. It's sp3d, not s1p3d1.

Test Cases

1 => s
2 => sp
3 => sp2
5 => sp3d
12 => sp3d5f3
36 => sp3d5f7(4)9(5)11

Victory

The winner will be the shortest answer in bytes.

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 75 bytes

f=(n,l=0,r=n>l*2?l*2+1:n)=>n?("spdf"[l]||`(${l})`)+(r-1?r:'')+f(n-r,l+1):''
\$\endgroup\$
2
\$\begingroup\$

JavaScript 121 bytes

Works thanks to Stephen Leppik :)

o="";a=["s","p","d","f"];i=0;while(x>0){i<4?o+=a[i]:o+="("+i+")";b=Math.min(2*i+1,x);b>1?o+=b:z=0;x-=b;i++}console.log(o)

Here is the not golfed version :

o="";
a=["s","p","d","f"];
i=0;
while(x>0){
    i<4?o+=a[i]:o+="("+i+")";
    b=Math.min(2*i+1,x);
    b>1?o+=b:z=0;             //z : useless assignment
    x-=b;i++;
}
console.log(o);
\$\endgroup\$
  • \$\begingroup\$ Also, 93 bytes. \$\endgroup\$ – Nissa Dec 21 '17 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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