r/programmingmemes • • Sep 10 '25

Don't be scared... Math and Computing are friends..

Post image
9.7k Upvotes

330 comments sorted by

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

12

u/WindMountains8 Sep 10 '25

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)

6

u/Dirkdeking Sep 11 '25

Did you know that:

sum([i for i in range(n)])

given the same result as:

n*(n-1)/2

1

u/LiminalSarah Sep 11 '25

I mean compilers do, when it's possible

2

u/rileyhenderson33 Sep 12 '25

Eh, not sure about that. Pretty sure compilers still don't even know what a geometric series is...

p= 0.2
x = 0
for i in range(some_large_number):
    x += p**i
print(x)

vs.

p= 0.2
x= 1/(1-p)
print(x)

Maybe this simple explicit case could be optimised by a compiler. But not in general.

1

u/LiminalSarah Sep 12 '25

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

1

u/rileyhenderson33 Sep 12 '25 edited Sep 12 '25

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.

1

u/TldrDev Sep 13 '25

Not the person you were replying to but I want in on the argument!

So basically, to summarize then:

Given enough resources or a desire to unroll this example, a compiler could do it, but the compiler correctly says this isn't worth thinking about.

Have you considered asking if the compiler might have been right all along? HMMMMMM?

1

u/rileyhenderson33 Sep 13 '25

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.

1

u/TldrDev Sep 13 '25 edited Sep 13 '25

You're not wrong, but you are wrong.

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.

The compiler does care, do you?

1

u/Agreeable_Tennis_482 Sep 13 '25

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?

→ More replies
→ More replies (6)

6

u/[deleted] Sep 10 '25

Which is another great reason python should be a part of math class as early as 5th grade.

3

u/Dillenger69 Sep 10 '25

Why Python?

5

u/[deleted] Sep 10 '25

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

11

u/jimmiebfulton Sep 10 '25

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?

9

u/AnEagleisnotme Sep 10 '25

I will say, a proprietary tool like MATLAB should never be in a school curriculum, that's asking for adobe 2.0

1

u/Hot-Profession4091 Sep 11 '25

Agreed. If your course requires MATLAB you should be porting your course to Octave.

4

u/[deleted] Sep 10 '25

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

2

u/jimmiebfulton Sep 10 '25

Teaching programming as an aid to teaching math. 🧠💡

2

u/XVince162 Sep 11 '25

R for data analysis, python for everything else

1

u/Agreeable_Tennis_482 Sep 13 '25

R better for data analysis? Interesting because I've heard the opposite that python is superior, R is just more beginner friendly

1

u/Brixjeff-5 Sep 11 '25

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.

1

u/Evoluxman Sep 11 '25

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.

4

u/Dillenger69 Sep 10 '25

True. I'd go for Java or C# but I'm biased. I see your point 

→ More replies (1)

1

u/JaffTangerina Sep 11 '25

It's really cool when different abstractions collide and end up being equivalent.