Welcome back.
In the last part, we gave our GPT four and clawed the challenge of converting a simple Python program
to calculate Pi into efficient C plus plus code, and both frontier models did well.
Although GPT four only did a few more hints, and now we're going to move on to a harder problem.
This code here, this Python code called Python hard, uh, is is is doing a little bit more work.
Uh, it is going to, uh, calculate something called the maximum subarray sum.
And this is what that does.
Uh, the challenge is suppose you are given an array, a list of a large number of positive and negative
numbers of random or pseudo random positive and negative numbers.
And the question you are asked is if you were to take any subarray, any set of, of consecutive Numbers
in that bigger array and add them up, you would get a sum of that section.
What is the largest sum of any possible subarray?
Pick any start point and end point that you wish in the array, and try and pick it so that you get
the largest subarray and return that largest number, the number of the largest subarray.
And of course, you have to bear in mind that there are negative numbers here.
So it's not there's not an obvious answer.
And the indeed the way that this code works is simply trying everything.
It's a loop within a loop, loops through the start point, loops through the end point, and figures
out it compares the current sum with the maximum sum and returns that sum.
So that is what this function is doing.
That is the meat of this code.
There are two other things going on here.
One of them is that we want to create a large number of pseudo random numbers, and I don't want to
use something like Python's random number method, the random library, because then that's obviously
not going to match with C plus plus libraries.
Um, so instead what we've done here is, is just implement a very common, very simple pseudo random
number generator.
It's known as the linear Congruential generator.
And you can Google it if you need to see the formula.
That's what I did.
And it's a simple way to generate a sort of predictable stream of random numbers based on a seed, uh,
which is what we do so that we can have consistency and check that our C plus plus code gives identical
results to our Python code.
Um, and so yeah, this with a generator yielding the value is going to create these random numbers.
And of course that's sneaky of me, because it's going to be hard for the frontier models to figure
out how to convert that into C plus plus code.
It's going to have to approach it.
Um, not it's not not not an easy conversion, but not giving it things like the Pi function.
Um, this is the one that does the loop within the loop.
Um, and this here is something which repeats this 20 times.
And uh, for the, uh, takes the total sum of all 20 times of doing it with a different seed.
There's some settings here to get us off to a to to a consistent make sure that we're doing this consistently
for every run.
Um, and then we time doing this 20 times so that this is another of those cases where I should probably
have been talking about it while I was running it, because if I remember right, this will take about
a minute to run in Python.
So I'm going to have to figure out how to interest you for a minute of waiting now.
Uh, again, exac is the, uh, the definitely one that should cause you to raise an eyebrow any time
that someone tells you to run exac of anything.
Um, but of course, you can take a careful look through my code here and satisfy yourself that there
is nothing devious happening here, and there is nothing but a bunch of maths.
Uh, and besides, I have a British accent.
That must mean I'm believable.
Uh.
All right.
Anyway, that's enough prattling.
Uh, it's done its thing.
It was only 27 seconds, so I didn't need to jabber away like that.
Um, and the total, uh, maximum subarray sum across all 20 runs is this rather large number of 10,980?
I mean, it's not a massive number because of course we have positives and negatives that have all balance
themselves out.
All right.
So now it's time for us to ask, uh, the GPT to make its version.
Let's do that now.
Here comes GPT version, and we will now try compiling that.
And it's generated a warning, uh, implicit conversion.
And then it ran it and it got the wrong answer.
So in this case, despite the fact that there are some hints and despite that, I warned it about exactly
the mistake I believe it's made, uh, the, um, it has generated code that runs quite quickly, but
sadly ends up with an error, uh, problem, I believe with the number overflows that some C plus plus
expert will have to confirm, and it ends up with the number zero.
After the break, we will find out how Claude does.