Start by indexing possible nuclei by their numbers of protons and their numbers of neutrons. Just forget the names for now. Call each one a "distinct element" [itex]X^p_n[/itex]. For example [itex]X^6_8[/itex] means the element with six protons and 8 neutrons (we usually call this carbon-14 ...).
Now you have two issues. How well does the nucleus hold together, and if it does this well enough how does the element behave otherwise.
To get into nuclear stability you will have to consider the nature of the strong nuclear force binding it together and the fact that the proton's being all positive want to fly apart. This is a deep subject invoking many topics in quantum mechanics and the standard model. But in the end we can roughly say that nuclei with equal numbers of protons and neutrons are somewhat more stable.
Now you look at the more or less stable cases, the ones that stick around long enough to study. These will have very concentrated positive charges and so will attract electrons. They will keep attracting electrons until you have as many electrons as protons and the atom becomes neutral. However the electrons don't fall all the way into the nucleus. Since the electrons are very very VERY light relative to protons and neutrons they tend to float around in orbits moving very fast. Again this has to do with quantum theory and the fact that the wavelength of the electrons is much bigger than the size of the nucleus when the electrons energy is small enough to be close to the nucleus. There's a trade-off making it very improbable for the electron to "fall all the way in" (and this affects stability of the nucleus since the electron can occasionally hit the proton and make a neutron plus a neutrino).
So when you study the ways electrons can configure themselves around the nucleus you get (again due to quantum theory) some "resonant" orbital modes. You also have the statistical behavior of electrons as fermions and their mutual repulsion so that they fill up the space around the nucleus in layers that get rather complicated.
Then we look at how two elements with their clouds of electrons behave together. We find they can form bonds and the bonding behavior is purely a function of the electron configurations. So elements with the same number of protons behave the same way chemically. Elements [itex]X^6_6, X^6_7[/itex], and [itex]X^6_8[/itex] all behave almost identically chemically, so much so that you can eat sugar made of each and not notice the difference (too much). (Their atomic masses differ which affects some reaction and diffusion rates.) (also [itex]X^6_8[/itex] is a bit unstable, having a 50-50 change of exploding every 5730 years and there are a very very large number of them are in a gram of sugar, enough that you'll eventually die of the radiation from the few which are exploding each second if you ate a gram of sugar made of pure C-14.)
So we, especially not having discovered the nuclear structure yet, decided to give classes of these elements names based on their chemical behavior (tendency to stick together) which we now know is due, indirectly, to the number of protons they have. Atoms with 6 protons we call Carbon. Atoms with 8 protons we call Oxygen. etc. When we look closely enough to distinguish beyond chemical behavior, the number of neutrons we append the total number p+n since we can best indicate the case by the total mass per atom (and protons and neutrons are almost identical in mass.)
Now if you want to understand why the number of electrons in orbit affects the chemistry in such an odd fashion, creating sometimes metals, sometimes halides, sometimes semiconductors, and sometimes inert gasses, you'll have to master a good deal of mathematics and then the physics of quantum mechanics which uses that mathematics as its language. It isn't too hard but it will take some years. You can begin to understand the "why" of that "computer code" you made reference to.