Advertisement
Redxone

[CC Atom Calculator] Explained and shortened.

May 20th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. electrons = 102 -- change this number for a new element!
  2. subs = {2,8,18,32}
  3. -- Add more electron positions if our electrons fill the default ammount
  4. if(electrons > 60)then
  5.     for i = 1, math.ceil((electrons - 60) / 32) do
  6.         subs[#subs+1] = 32
  7.     end
  8. end
  9. -- do this while we have electrons still not placed.
  10. while electrons > 0 do
  11.     -- loop through the shells and try to fill them
  12.     for i = 1, #subs do
  13.         if(electrons >= subs[i])then
  14.                 print(subs[i])
  15.                 electrons = electrons - subs[i]
  16.         else -- if the electrons doesnt fill the shell then,
  17.            -- if we have less than or equal too 8 electrons left (max valence electrons)
  18.            -- then place them all down.
  19.             if(electrons <= 8 and electrons > 0)then
  20.                 print(electrons)
  21.                 electrons= 0
  22.             end
  23.             -- look backwards through the table and search through previous shells
  24.             -- so electrons that didnt get a chance to get placed are now placed.
  25.              for y = #subs, 1, -1  do
  26.                 if(electrons >= subs[y] and electrons > 0)then
  27.                     print(subs[y])
  28.                     electrons = electrons - subs[y]
  29.                 end
  30.              end
  31.              -- if the backwards loop couldnt find a place for some electrons then place the rest down.
  32.              -- the backwards loop cannot fail, if it does then there is less than 2 electrons left.
  33.              if(electrons > 0)then
  34.                  print(electrons)
  35.                  electrons = 0
  36.              end
  37.         end
  38.     end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement