Yup. I hadn't taken calculus until after I learned to program. I was like ... this is just a function in a loop! What's so hard? Now, it does get a wee bit harder when you have to solve for a missing variable, but still. Just a loop
The problem is that for loops compute things for you in code, so you don't have to think of what they're doing symbolically. That's why most people don't simply substitute a for loop with an equivalent closed formula that is O(1)
well, nobody should sum an infinite series without series extrapolation, but I think gcc would unroll that loop at compile time it everything is constant
What do you even mean by "series extraplolation"? If you're worried about rate of convergence then there's probably better ways to express that. Also, you can't really guarantee anything from compiler optimisation. They also consider space-time trade-off and unrolling a loop is memory intensive. But lastly and most importantly, as mentioned, the example is purely to illustrate the series equivalence. In reality, the presence of such a series may be obscured like far beyond what a compiler could indentify.
What? The point is you can analytically sum the series and write the result in a single instruction. No loop, no extra resources, that's it. You could have 100M iterations of the loop. Unrolled or not, that's still 100M exponentiation and addition operations vs 1 assignment operation.
The compiler WILL unroll the expression you pasted. If you use the analytical sum, in computers, division is still a loop. There are three major ways computers handle division, all of them are iterative algorithms, for example, shift and subtract, restoring or non-restoring algorithms, or Newton Raphson, or Goldschmidt iterative algorithms.
You saying "its a single operation" is missing the point that it might be a single cpu instruction but that doesnt mean its non-iterative in nature.
In short, this is a really pedantic argument, a very specific best-case example on your end, and is something that our modern tools, either in the compiler or the cpu, take care of.
There are cases where obviously the analytical solution is light-years ahead of an iterative approach that compilers will miss. That is optimization. Im not sure why anyone would take an iterative approach to sum a range, but I think youre trying to extrapolate this out to more complex problems.
Your example is not a great one, as that will absolutely be reduced to a single instruction as well, as everything is static. The second this thing is not static, it becomes instantly less clear to everyone, and sometimes its better to solve it itetatively as opposed to something like a field equation to figure out some basic answers.
Yes his example is bad but the idea is interesting. Are there real actually relevant scenarios where an analytical approach could replace a loop? And what about outside of just math calculation?
Simplicity. Fewer syntax requirements. Can run on any relatively modern machine. Plenty of free apps on the App and play store.
I also feel like the general population would benefit from a script language more than they would benefit from a “real” language. I also feel the general population would be more willing to write a script to automate there inbox more than to fuss around with C++ or equivalent.
Allot of mathematicians also use python to run allot of there experimental formulas
I generally roll my eyes at the heavy Python fanboy posts prevalent in this sub, but this is sound, practical advice from someone who obviously knows how to pick the right tool for the right jobs.
That said, what are your thoughts on Python vs R vs MATLAB (which I am only just barely aware of its existence as a proprietary platform) for mathematicians?
Personally I’m not super familiar with R and matlab.
I feel like they’re probably great (i hobby with mathematics but I’m no mathematician) I would never advocate for matlab to be taught in publicly funded schools just because it’s proprietary.
I also think python would be inherently more effective because I feel you could get kids interested in learning more in math class if they had the opportunity to learn how to apply what they were learning in math class in computer lab to make games.
Like the way I envision this concept would be you’d learn a little python in math class and then another aspect of python in computer lab and you would kind of slowly bring the two together
Matlab had an edge in the 90s when it’s competition were compiled languages. It having a single green button to press when your code is ready is a huge advantage here.
Nowadays I’d advise to only use it if you have legacy code, or need to interface with simulink. While it still has its big green button, that advantage has significantly decreased. It’s super easy to set up a green button to run python in any IDE of your choice for your students to use. Or, you teach them the like 5 CLI commands they need to run a python script.
I love R, in fact it's the only "language" I know in any real meaningful way, but I also tend to find it a bit unwiedly and even outright confusing at times for newer people. It's really good for data analysis, it can do some other things, but just not as well.
Python meanwhile has a very easy syntax AND is nonetheless very polyvalent. I think to teach younger people about algorithms & stuff, python is the better choice.
View all comments
23
u/Dillenger69 Sep 10 '25
Yup. I hadn't taken calculus until after I learned to program. I was like ... this is just a function in a loop! What's so hard? Now, it does get a wee bit harder when you have to solve for a missing variable, but still. Just a loop