WEBVTT
Kind: captions
Language: en

00:00:00.240 --> 00:00:06.880
welcome to the standup Mass Halloween episode&nbsp;
this year a viewer has sent in this mysterious&nbsp;&nbsp;

00:00:06.880 --> 00:00:14.080
incantation which apparently is able to conjure&nbsp;
up prime numbers and they wanted to know why&nbsp;&nbsp;

00:00:14.080 --> 00:00:20.880
does this work and given that allegedly this&nbsp;
expression opens a portal to the prime realm&nbsp;&nbsp;

00:00:20.880 --> 00:00:25.080
I thought that would be a good spooky theme for&nbsp;
a Halloween episode although it's not spooky is&nbsp;&nbsp;

00:00:25.080 --> 00:00:33.240
it it's just going to be maths just somehow up&nbsp;
Arrow dot question mark dollar sign vertical&nbsp;&nbsp;

00:00:33.240 --> 00:00:40.120
line up Arrow Open brackets dot dot plus question&nbsp;
mark close brackets back slash1 plus dollar sign&nbsp;&nbsp;

00:00:40.120 --> 00:00:49.440
is able to conjure up prime numbers okay we're&nbsp;
going to have a closer look at how this works&nbsp;&nbsp;

00:00:49.440 --> 00:00:54.760
for completeness I will show you that this&nbsp;
definitely does work in practice so this is&nbsp;&nbsp;

00:00:54.760 --> 00:01:03.080
something called regex or a regular expression&nbsp;
it's a way of matching or searching a different&nbsp;&nbsp;

00:01:03.080 --> 00:01:08.000
string of like whatever characters according to&nbsp;
these requirements I'll explain exactly what all&nbsp;&nbsp;

00:01:08.000 --> 00:01:14.160
of this means in a moment but first I'm going&nbsp;
to show you it operating nicely in Python so on&nbsp;&nbsp;

00:01:14.160 --> 00:01:21.360
my code here I've imported regular Expressions&nbsp;
so I can use that functionality and down here I&nbsp;&nbsp;

00:01:21.360 --> 00:01:26.360
want a regular expression to match don't all&nbsp;
those details and there's our string of text&nbsp;&nbsp;

00:01:26.360 --> 00:01:31.560
I've put it all in there and over here I've just&nbsp;
got a one time some number and that's the number&nbsp;&nbsp;

00:01:31.560 --> 00:01:40.520
we're going to be testing so let's say uh n is 13&nbsp;
I can now run it to see if it matches our regular&nbsp;&nbsp;

00:01:40.520 --> 00:01:47.960
expression that we've got um over here however&nbsp;
little spoiler this actually says if something&nbsp;&nbsp;

00:01:47.960 --> 00:01:54.920
is a composite number it says if it's not prime&nbsp;
this is true if n is not Prime and false if it&nbsp;&nbsp;

00:01:54.920 --> 00:02:01.160
is prime but if we want a true Prime test we&nbsp;
just want true for primes that's an easy fix&nbsp;&nbsp;

00:02:01.160 --> 00:02:07.840
we just write not there you go not that piece&nbsp;
of cake and so we can see the output I'm going&nbsp;&nbsp;

00:02:07.840 --> 00:02:15.000
to put all of that inside a print and now if I&nbsp;
run that you can see that 13 uh true it is prime&nbsp;&nbsp;

00:02:15.000 --> 00:02:25.880
if I change it something not prime let's say 15&nbsp;
15 is false not a prime 101 run it true it's a&nbsp;&nbsp;

00:02:25.880 --> 00:02:35.280
prime um against my better judgment a th000 run&nbsp;
it false okay so it can say mean for reasonably&nbsp;&nbsp;

00:02:35.280 --> 00:02:42.520
big numbers if something is a prime or not and&nbsp;
it does that by matching the number or rather&nbsp;&nbsp;

00:02:42.520 --> 00:02:51.000
the number times the character one against this&nbsp;
regular expression so to understand how on Earth&nbsp;&nbsp;

00:02:51.000 --> 00:03:00.840
this can tell if a number is prime or not we&nbsp;
need to understand all of these symbols [Music]&nbsp;&nbsp;

00:03:04.480 --> 00:03:10.240
we are not going to go through every single bit&nbsp;
of this expression at once to work out how it was&nbsp;&nbsp;

00:03:10.240 --> 00:03:19.200
able to conjure up this seven and huh now also&nbsp;
a five can't imagine this is going to become a&nbsp;&nbsp;

00:03:19.200 --> 00:03:22.520
problem instead we're going to do it a little&nbsp;
bit at a time we're going to start just with&nbsp;&nbsp;

00:03:22.520 --> 00:03:27.920
this section right above me there the dot dot plus&nbsp;
question mark so there's three symbols there we&nbsp;&nbsp;

00:03:27.920 --> 00:03:33.400
need to understand what are the dots doing and yes&nbsp;
one more dot over there what is that plus symbol&nbsp;&nbsp;

00:03:33.400 --> 00:03:37.280
up to what does it represent and what is that&nbsp;
question mark and yes there's another question&nbsp;&nbsp;

00:03:37.280 --> 00:03:43.440
mark over there so we'll do those three symbols&nbsp;
first let's start with two example sentences the&nbsp;&nbsp;

00:03:43.440 --> 00:03:50.560
kind of perfectly normal thing anyone might say&nbsp;
uh stuff like I love Matt's videos and I've taken&nbsp;&nbsp;

00:03:50.560 --> 00:03:56.440
out the punctuation it's all lowercase so don't&nbsp;
complain about that or another sentence a normal&nbsp;&nbsp;

00:03:56.440 --> 00:04:03.000
human would say math videos are the best we are&nbsp;
going to search both those strings of text for&nbsp;&nbsp;

00:04:03.000 --> 00:04:09.680
the phrase maths and surprisingly no maths does&nbsp;
not appear in the first sentence because those&nbsp;&nbsp;

00:04:09.680 --> 00:04:15.480
letters are not in there but it is in the second&nbsp;
sentence we've got maths in it hence lovely green&nbsp;&nbsp;

00:04:15.480 --> 00:04:21.720
tick for that one but what if we weren't searching&nbsp;
just for specific letters what if we put in some&nbsp;&nbsp;

00:04:21.720 --> 00:04:29.800
wild cards and in regex a DOT a a full stoper&nbsp;
period is a wild card so if we searched for m&nbsp;&nbsp;

00:04:29.800 --> 00:04:37.080
dot dot dot s that's now looking for any part of&nbsp;
the string that starts with an m has three other&nbsp;&nbsp;

00:04:37.080 --> 00:04:43.720
characters and then has an S now both of them&nbsp;
both of them have a bit of string that fulfills&nbsp;&nbsp;

00:04:43.720 --> 00:04:48.640
those requirements and whenever I say string I&nbsp;
just mean a sentence that's just a nerdy way of&nbsp;&nbsp;

00:04:48.640 --> 00:04:53.520
saying sentence I do apologize but that's what&nbsp;
we say in Computing for some ridiculous reason&nbsp;&nbsp;

00:04:53.520 --> 00:05:01.040
the point is now M do dot do s matches both so&nbsp;
the dot wild card very straightforward however&nbsp;&nbsp;

00:05:01.040 --> 00:05:06.160
what if we didn't know how many characters were&nbsp;
going to be between the m and the S what if we&nbsp;&nbsp;

00:05:06.160 --> 00:05:14.640
put one dot and then a plus symbol the plus symbol&nbsp;
just means more of whatever symbol comes before it&nbsp;&nbsp;

00:05:14.640 --> 00:05:22.280
so in this case it's more of wild card how many&nbsp;
more as many as you want well strictly speaking&nbsp;&nbsp;

00:05:22.280 --> 00:05:29.840
one or more if there was an asteris instead of a&nbsp;
plus that means zero or more so it's a bit more&nbsp;&nbsp;

00:05:29.840 --> 00:05:35.000
flexible but we specifically what we're going&nbsp;
to do later we want one or more and they both&nbsp;&nbsp;

00:05:35.000 --> 00:05:41.800
still match we have mats and we have maths but now&nbsp;
there's a level of ambiguity here some of you may&nbsp;&nbsp;

00:05:41.800 --> 00:05:46.080
have noticed in both these cases and we'll look&nbsp;
specifically at the second one there's multiple&nbsp;&nbsp;

00:05:46.080 --> 00:05:50.320
points in the string that match our new search&nbsp;
because if we don't say how many wild cards&nbsp;&nbsp;

00:05:50.320 --> 00:05:58.360
between M and S that could match maths from an&nbsp;
m to an S it could match math videos that's an&nbsp;&nbsp;

00:05:58.360 --> 00:06:03.280
m to an S with wild card cards in the middle&nbsp;
or it could be the whole thing apart from the&nbsp;&nbsp;

00:06:03.280 --> 00:06:10.120
final T it could be math videos are the Bez and&nbsp;
that would begin and end with an m with a bunch&nbsp;&nbsp;

00:06:10.120 --> 00:06:16.200
of characters in the middle and as it stands if&nbsp;
you just do it this way it's greedy it's like ah&nbsp;&nbsp;

00:06:16.200 --> 00:06:21.360
give me give me as many characters as possible so&nbsp;
it will go for this one the one where you've got&nbsp;&nbsp;

00:06:21.360 --> 00:06:28.400
the maximum string that matches those requirements&nbsp;
if you put in our third symbol the question mark&nbsp;&nbsp;

00:06:28.400 --> 00:06:36.480
that means don't don't be greedy just look for the&nbsp;
smallest substring that matches your requirements&nbsp;&nbsp;

00:06:36.480 --> 00:06:42.240
and that would push it all the way back down&nbsp;
again to just being maths because that is the&nbsp;&nbsp;

00:06:42.240 --> 00:06:47.720
shest string that fulfills the requirements and&nbsp;
so those are our three symbols the dot is a wild&nbsp;&nbsp;

00:06:47.720 --> 00:06:53.120
card the plus means more of whatever's on the&nbsp;
left and the question mark means why are you so&nbsp;&nbsp;

00:06:53.120 --> 00:06:59.880
greedy don't be greedy but what about the rest&nbsp;
of these symbols well that vertical line mean&nbsp;&nbsp;

00:06:59.880 --> 00:07:05.720
or so actually these are two expressions either&nbsp;
this expression over here matches or this&nbsp;&nbsp;

00:07:05.720 --> 00:07:11.280
expression over here matches so we can just deal&nbsp;
with them separately and you may have noticed they&nbsp;&nbsp;

00:07:11.280 --> 00:07:17.200
both start with that up arrow and then end with a&nbsp;
dollar sign the examples we saw before the match&nbsp;&nbsp;

00:07:17.200 --> 00:07:22.080
could be in the middle of the string somewhere&nbsp;
but by having that up Arrow it means it has to&nbsp;&nbsp;

00:07:22.080 --> 00:07:27.000
start at the beginning so we could use just the&nbsp;
up Arrow if you only want to match the beginning&nbsp;&nbsp;

00:07:27.000 --> 00:07:32.400
of a string and the dollar sign means it has to&nbsp;
end with that so you could use that if you just&nbsp;&nbsp;

00:07:32.400 --> 00:07:38.120
want to search at the ends of strings by using&nbsp;
both of them it means whatever's in the middle&nbsp;&nbsp;

00:07:38.120 --> 00:07:46.040
has to match the entire string so what all of&nbsp;
this actually means is whatever we're comparing&nbsp;&nbsp;

00:07:46.040 --> 00:07:54.360
this search to that string has to either be&nbsp;
completely that top one or completely that bottom&nbsp;&nbsp;

00:07:58.401 --> 00:08:05.161
very slight confession you may have noticed in my&nbsp;
code from earlier we were not actually matching n&nbsp;&nbsp;

00:08:05.161 --> 00:08:13.241
the number itself we were matching n times the&nbsp;
character the digit one and in this case that&nbsp;&nbsp;

00:08:13.241 --> 00:08:19.521
the Asterix is not it's not part of the regx it's&nbsp;
just multiplication and that is saying that you&nbsp;&nbsp;

00:08:19.521 --> 00:08:24.641
turn whatever the number is that you're sending&nbsp;
in so for example from before if you're sending&nbsp;&nbsp;

00:08:24.641 --> 00:08:33.841
in the seven or indeed the five oh now there's a&nbsp;
four that doesn't make a lot of sense oh there's a&nbsp;&nbsp;

00:08:33.841 --> 00:08:39.361
seven that that that's okay that makes sense right&nbsp;
and a three okay whatever the whatever the number&nbsp;&nbsp;

00:08:39.361 --> 00:08:46.401
is there's a surprising number of them down here&nbsp;
now whatever it is you're sending in you multiply&nbsp;&nbsp;

00:08:46.401 --> 00:08:54.241
it by uh By One The Character one so three would&nbsp;
be three times the character one so that would&nbsp;&nbsp;

00:08:54.241 --> 00:09:02.761
actually just become uh one one one so we don't&nbsp;
actually process the number we're interested in&nbsp;&nbsp;

00:09:02.761 --> 00:09:09.401
we process a string which is a tally of that&nbsp;
number so we wouldn't send in number three we&nbsp;&nbsp;

00:09:09.401 --> 00:09:17.241
would send in the string which is the characters&nbsp;
11 one or if we were checking 15 we would send in&nbsp;&nbsp;

00:09:17.241 --> 00:09:26.801
15 ones and so on and that's what we're actually&nbsp;
comparing to um magical incantation from before&nbsp;&nbsp;

00:09:26.801 --> 00:09:34.281
now we are finally in a position to have a look&nbsp;
at these two cases where one or the other has to&nbsp;&nbsp;

00:09:34.281 --> 00:09:40.641
be true where compared to the entire length of the&nbsp;
string there are so many numbers down here and the&nbsp;&nbsp;

00:09:40.641 --> 00:09:46.001
string just so we're familiar is a string of all&nbsp;
ones in fact it could be any symbol as long as&nbsp;&nbsp;

00:09:46.001 --> 00:09:50.481
they're all the same where the number of symbols&nbsp;
in the string matches the number we want to check&nbsp;&nbsp;

00:09:50.481 --> 00:09:58.641
if it's prime or not so this would represent three&nbsp;
and uh that top case is only for if there's one or&nbsp;&nbsp;

00:09:58.641 --> 00:10:06.081
zero symbols because if you have a single dot for&nbsp;
just one wild card you might think oh well that&nbsp;&nbsp;

00:10:06.081 --> 00:10:10.801
will only match if there's one symbol and that's&nbsp;
true if you had a single dot it would return true&nbsp;&nbsp;

00:10:10.801 --> 00:10:17.721
if you had a single symbol in your string because&nbsp;
there's one symbol one Wild Card dot in this case&nbsp;&nbsp;

00:10:17.721 --> 00:10:25.001
the question the why are you so greedy question&nbsp;
mark also means means it applies to the null&nbsp;&nbsp;

00:10:25.001 --> 00:10:30.721
string if you have an empty string and you put&nbsp;
the question mark after the wild card that means&nbsp;&nbsp;

00:10:30.721 --> 00:10:39.481
there's either zero or one that match so actually&nbsp;
that's just a catchall for if you feed the system&nbsp;&nbsp;

00:10:39.481 --> 00:10:45.201
an empty string and it returns true because&nbsp;
an empty string is not a prime or you feed it&nbsp;&nbsp;

00:10:45.201 --> 00:10:51.841
a single symbol which represents one and then it&nbsp;
returns true because one is not Prime and that's&nbsp;&nbsp;

00:10:51.841 --> 00:10:58.441
all that's there is just to catch those edge cases&nbsp;
the actual calculation for a real number that's&nbsp;&nbsp;

00:10:58.441 --> 00:11:04.401
where this one comes in inside the brackets we&nbsp;
have dot dot plus question mark that first dot&nbsp;&nbsp;

00:11:04.401 --> 00:11:08.841
is a wild card very straightforward the second dot&nbsp;
is another wild card but this time with the plus&nbsp;&nbsp;

00:11:08.841 --> 00:11:15.881
symbol acting on it which as we saw before means&nbsp;
you can have multiple wild cards in a row as many&nbsp;&nbsp;

00:11:15.881 --> 00:11:19.801
as you need and in this case we haven't got an&nbsp;
upper bound so it's just going to generate as many&nbsp;&nbsp;

00:11:19.801 --> 00:11:24.841
as we may require now I'm going to show you those&nbsp;
cases up here so you've got initially you got the&nbsp;&nbsp;

00:11:24.841 --> 00:11:29.121
two which is the first one and the second one but&nbsp;
then the second one can have more so you might&nbsp;&nbsp;

00:11:29.121 --> 00:11:34.321
have three you might have four you might have&nbsp;
five so on I'm not implying by what I'm showing&nbsp;&nbsp;

00:11:34.321 --> 00:11:38.641
you here that this is the order the computer&nbsp;
actually does it in I'm just showing you all&nbsp;&nbsp;

00:11:38.641 --> 00:11:44.241
the cases and somewhere in the computer whatever&nbsp;
is doing the regx it'll check them in some order&nbsp;&nbsp;

00:11:44.241 --> 00:11:51.201
I'm just trying to put them in a logical way&nbsp;
that makes sense and it will stop once it hits&nbsp;&nbsp;

00:11:51.201 --> 00:11:56.201
the length of the string so whatever the number is&nbsp;
it will do as many as is required but so far all&nbsp;&nbsp;

00:11:56.201 --> 00:12:03.361
we have is the bit in the brackets gives us two&nbsp;
wild cards or three wild cards or four so on and&nbsp;&nbsp;

00:12:03.361 --> 00:12:09.721
so on as many as we may need I say as many as we&nbsp;
may need because there's more so that back slash&nbsp;&nbsp;

00:12:09.721 --> 00:12:15.481
one the back slash is a break character that just&nbsp;
says the thing after this is not a symbol because&nbsp;&nbsp;

00:12:15.481 --> 00:12:23.281
that one is actually an index the brackets you&nbsp;
have in a regex are not zero index indexed they're&nbsp;&nbsp;

00:12:23.281 --> 00:12:28.841
one indexed and you can then refer to them just by&nbsp;
using a break character and their number so here&nbsp;&nbsp;

00:12:28.841 --> 00:12:32.921
we only have one set of brackets and they brackets&nbsp;
number one so what we're saying here is hey&nbsp;&nbsp;

00:12:32.921 --> 00:12:40.441
remember those brackets from a second ago you know&nbsp;
what them but with another plus so that now means&nbsp;&nbsp;

00:12:40.441 --> 00:12:47.401
what we actually do is take all the situations we&nbsp;
had a moment ago but we can have multiples of them&nbsp;&nbsp;

00:12:47.401 --> 00:12:52.481
so we can have two twos or three twos or four twos&nbsp;
all the way up as many as we need to check we can&nbsp;&nbsp;

00:12:52.481 --> 00:12:58.401
have two threes or 3 threes or four threes all the&nbsp;
way up now it's not super efficient because we'll&nbsp;&nbsp;

00:12:58.401 --> 00:13:04.081
also look at fours and two fours and three fours&nbsp;
we've already done twos we don't need those I say&nbsp;&nbsp;

00:13:04.081 --> 00:13:08.881
already done like I said before we don't know the&nbsp;
order these things are going to happen in they're&nbsp;&nbsp;

00:13:08.881 --> 00:13:15.361
just going to happen but what it means is if&nbsp;
you start with a string of let's say 15 ones&nbsp;&nbsp;

00:13:15.361 --> 00:13:20.761
or whatever you will go through all of these&nbsp;
cases and it will return true if any of them&nbsp;&nbsp;

00:13:20.761 --> 00:13:27.721
match perfectly from the beginning of the string&nbsp;
to the end of the string and that will only happen&nbsp;&nbsp;

00:13:27.721 --> 00:13:34.161
if you've got a multiple of those smaller units&nbsp;
that were generated inside the brackets so it&nbsp;&nbsp;

00:13:34.161 --> 00:13:42.801
means it will return true if any of the various&nbsp;
sized sets of wild cards generated fit an exact&nbsp;&nbsp;

00:13:42.801 --> 00:13:50.441
number of times inside the string it's checking if&nbsp;
any of the sets of wild cards the length of them&nbsp;&nbsp;

00:13:50.441 --> 00:13:56.281
is a factor and then it returns true and if none&nbsp;
of them are factors it returns false which is the&nbsp;&nbsp;

00:13:56.281 --> 00:14:03.321
case for a prime number it's so clever the whole&nbsp;
thing is effectively a modern implementation of&nbsp;&nbsp;

00:14:03.321 --> 00:14:08.481
the Civ of ostanes because it's just going through&nbsp;
and looking at multiples of every possible factor&nbsp;&nbsp;

00:14:08.481 --> 00:14:14.521
and then returning in this case true if one of&nbsp;
them perfectly lines up with the number you were&nbsp;&nbsp;

00:14:14.521 --> 00:14:19.321
checking it's so clever and again like I said&nbsp;
before the order it's doing it in is not exactly&nbsp;&nbsp;

00:14:19.321 --> 00:14:25.521
what I showed and that question mark there which&nbsp;
we didn't cover before is that actually forces it&nbsp;&nbsp;

00:14:25.521 --> 00:14:31.161
to do it in a more efficient order if you want&nbsp;
all the details behind um the actual order it&nbsp;&nbsp;

00:14:31.161 --> 00:14:37.441
does this in I'm hugely in debt to a fantastic&nbsp;
blog post by Ilia gas simchuk who put together&nbsp;&nbsp;

00:14:37.441 --> 00:14:43.041
a phenomenal write up for everything behind this&nbsp;
I will link to it below you can check it out but&nbsp;&nbsp;

00:14:43.041 --> 00:14:50.201
there you are it does work that is why you can&nbsp;
take up arrow. question mark dollar sign vertical&nbsp;&nbsp;

00:14:50.201 --> 00:14:58.401
line up Arrow open brackets. dot plus question&nbsp;
mark close brackets back SL1 plus dollar sign&nbsp;&nbsp;

00:14:58.401 --> 00:15:22.121
and it will give you all the F there's oh there&nbsp;
there's an actual RI down here what the f up

00:15:22.121 --> 00:15:37.041
[Music]

00:15:37.041 --> 00:15:51.961
[Music]

00:15:51.961 --> 00:15:56.761
oh wait hang on hang on they&nbsp;
gave me two incantations such&nbsp;&nbsp;

00:15:56.761 --> 00:16:04.121
a dingus right let's have a look&nbsp;
um there's no place like 127.0.0.1

00:16:04.121 --> 00:16:15.441
oh I'm home home I'm not explaining that one&nbsp;
thank you so much for watching the video huge&nbsp;&nbsp;

00:16:15.441 --> 00:16:19.441
thanks to Chris Lawrence who was the viewer who&nbsp;
originally sent in oh I haven't got it anymore&nbsp;&nbsp;

00:16:19.441 --> 00:16:24.561
the cursed text it was later independently&nbsp;
sent in by Jimmy deep I really appreciate&nbsp;&nbsp;

00:16:24.561 --> 00:16:29.241
everyone who sends me in great ideas uh all&nbsp;
of these Bits of Paper the ones you've seen&nbsp;&nbsp;

00:16:29.241 --> 00:16:33.721
and all the digits oh the digits forget them&nbsp;
they're obviously they're trapped in the prime&nbsp;&nbsp;

00:16:33.721 --> 00:16:39.361
realm now I will sign all the props from this&nbsp;
video they will be on eBay all the money going&nbsp;&nbsp;

00:16:39.361 --> 00:16:46.841
to charity specifically uh water a and that's it&nbsp;
that's our Halloween episode and we survived UNSC&nbsp;&nbsp;

00:16:46.841 --> 00:16:59.561
thanks for watching good video everyone well&nbsp;
done great job I mean joh just excellent work

