I LEARNED ABSOLUTELY NOTHING - Project Euler #2
Key Takeaways
Project Euler challenges, specifically #2, are tackled using math coding skills in Python, with an emphasis on problem-solving and algorithmic thinking.
Full Transcript
what is going on guys welcome back in this video today we're going to continue to do mathematical coding challenges on Project Oiler so let us get right into [Music] it all right now for those of you who haven't watched the first part project Oiler is basically a website similar to code Wars or hacker rank where you have these uh coding challenges and they're most of the time very math focused so it's all about numbers and sequences and factorization and so on and we already did the first 10 problems now I'm going to continue hopefully if it doesn't take too long with problems 11 to 20 the rules are simple I'm not allowed to use chat GPT I'm not allowed to Google EXA Exact Solutions I am allowed to Google small things like how certain functions work or uh you know small code snippets on how to flatten a list or something like this so little things that can help me I'm also allowed to look up Wikipedia articles and so on on certain Maybe algorithms but I'm not allowed to Google the exact solution unless I admit that I'm not able to solve it myself so let's get right into it we're going to start with problem 11 and here it says it's a 20x 20 grid below four numbers along a diagonal line have been marked in red so these here the product of these numbers is 26 63 78 14 equals this okay what is the greatest product of four adjacent numbers in the same direction okay so basically it's asking go through all the numbers like these four and then these four and then these four but then also these four here and also all the diagonal ones all the possible um I'm not sure if it also wants diagonal into the other direction like 37 13 628 but it just wants to have the maximum the maximum product now as always there is a Brute Force solution right so it's not too difficult to brute force a solution I mean at least logically I'm not sure what the runtime complexity of this is but basically you would go through all the numbers that are in a row all the numbers that are in a column all the numbers that are diagonal and I think that this should be manageable computationally so what I'm going to do now here is I'm going to go to my desktop I'm going to make a directory Oiler stuff I'm going to go to Oiler stuff and I'm going to create a script here 11. py and here I'm going to say now uh we somehow need to get this grid so I'm going to just copy paste this uh as a string maybe so let's say grid string is equal to a multi-line string of that actually this should this should be quite easy to turn into an array somehow let me just think about this so one thing that I can do is I can print grid string split on a line break that would result in all the lines as a list so python 31 py uh print was never closed okay that's not good so let's do it and now we have yeah the individual lines and then we can split those again so we can say grid equal to actually let's say grit row strings it's going to be equal to that and then we can say grid is equal to uh x.s split on a wh space for X in Grid row strings and then grid should be what we're looking for but of course in string format so maybe we need to typ cast it but actually that is it so we can actually say that um it should split then maybe let's say grid is equal to what was it again maybe let me open up a second terminal here I don't want to constantly have to go uh out of this so let me just python 3 11 py so we have a list of lists so we can say here we want to have uh int X for X in y for y in grit does this make sense I'm not sure so let me see if that makes sense yeah makes sense okay so now we have this as a list I would prefer this as a numpy array so let's say import numpy SNP uh and by the way because I didn't mention it in the beginning uh what's the value of watching this series if you you know what's the value of watching someone just solve challenges why not just solve it yourself uh the goal of this series and also the code War series is to show you my thought process so I don't edit out any mistakes I don't edit out any uh fails or any you know when I give up I just show you my raw thought process how I solve problems or fail to solve problems and I think that this can be useful especially for new programmers um all right so let's turn this into a numpy array let's say NP array just because it makes it easier to work with yeah this is what I want okay so what I can do now is I can just sum up uh all the the pairs or not pairs but all the four adjacent elements rowwise how would I do that I would basically just say for each list go through four elements right or is there a simple way now is there something that I can say about let me just think about this for a second if I use something like a 4x4 grit would that guarantee that if I look at a certain Criterion here is that may be something that I can use to find promising grits of 4x4 size not sure not sure now there's probably a fancy numpy function that I can use I'm not sure if I can find it by just looking into the documentation but there's probably some fancy cumulative product with a certain stride and a certain window function and numpy that I can use to do this efficiently I'm not sure but um what I would do now is I would just go for it so I would just say actually don't even need an NP aray for this let's say just for the row sums what I want to do is let's say for an individual list what would I do if I have one 2 3 4 5 6 7 8 or something what I would do is I would say that I want to do uh if that's my list L I want to go with L certain index time L this index + 1 * L this index + 2 times L this index plus 3 three for I in range going from0 to length of the list minus I think three or four not sure let's just see what happens here why is this oh so I'm not closing the range print row sums so that would give me 24 because it's 3 * 4 yeah this works okay then 2 * 3 * 4 is still 24 * 5 is 120 okay so this would give me that per row and if I do that for every row I can just say Max Max of that so I can say give me the maximum what is what is actually the question the sum or yeah it's or the product or the number so it's actually just a product right I just need the number so I can say the maximum of a row for each row so I can say that I want to do this for l in grit does this make sense I think so max sums row does this make any sense I think so yes row sums is not defined uh where do I use row sums oh here okay so that's the maximum actually not sums why am I constantly talking about sums this is actually products so and then again I can of course just take the max of that so I can make this a singular thing and then I would get the maximum number the maximum product if you take the four adjacent numbers per row but then [Music] um columnwise I would do a similar thing right I would say Max product column but this time here I would be using uh what would I be doing I would be going actually we can make this super simple does this work out of the box if I turn grit into a numpy array first I want to see if that still works because that would make a lot of things easier I could just do grit equals NP array grit and hopefully this still works yeah okay this still works since this still works all I have to do is I have to say I think I hope I'm not naive here but I think all I have to do is I have to say Grit T is equal to or actually I don't even have to do anything like that I can just say grid. t so I'm just transposing t uh this doesn't work right or is it the same I don't I don't think so I mean it could be but I think that the difference is grit and grit T should be different so yeah they are different so maybe the result is the same could be not sure so let's just leave it like this but the tricky thing is now the diagonals what do we do here we go um how do we do that we go through all the rows and all the columns let's see if I say 4 I in range I actually zero up until length of grit minus 3 for J in range zero length grid minus 3 [Music] let me just do it like this it would be 0 0 1 one but it doesn't have to be the same it can also be 01 one two so basically just have to be it has to be one more which would mean if um no it doesn't make a lot of sense I would need all the sequences where I have some number I I + 1 I + 2 I + 3 and then J what has J to be J has to also be increasing obviously so it still has to be J + 1 J + 2 J + 3 because we're going down but J has to have a property J needs to be does J need to be anything no actually I can pick any point as long as it's not too far to the edge and then I just go like this yeah shouldn't this cover all of it so wouldn't this just be um let's say Max sum is zero or still not some Max product is zero and then I say [Music] um product is equal to grit i j times grit I + 1 J + 1 times grit I now I'm using curly brackets here I 2 J + 2 time grit I + 3 J + 3 if product greater than Max product then Max product Max product equals product and then we print Max product not sure okay this is a problem invalid literal okay this now happens already here oh yeah because this is a problem okay uh no this is no this is actually not what I'm looking for or am I looking for this oh let me just see okay ah okay I think I wasn't printing the column some before I think this was the problem but now uh we're printing this as well and I think if I implemented this correctly the other way around if it still matters uh would be let's call this Max product two Max product two Max product two and here now we want to do the opposite we want to start at three want to go up until the length of the grit and we want to do the opposite we want to do to the left so just replace the plus with a minus here and of course print the result in the end I think that should yield a solution I don't know if that's correct but yeah I mean then this would be the answer let's see if that's true probably not probably I made some mistakes no okay so let's see what that would even be so when I get the max product column or actually I cannot print this here but maybe before doing that maybe I can say print and then just this part here so that is at the top here uh which one is it actually it's 512 so it's this one here right so it's not the last not that before so it's 1 2 3 4 five so it's 1 2 3 4 five it should be here somewhere I assume probably this here is this correct [Music] 63 * 93 * 53 * 69 no that's not it [Music] I don't know though is this a correct calculation no actually we're doing this with columns I forgot so it's actually sorry it's 1 2 3 4 5 should be here somewhere so probably this here this looks like it could result in a large number so 66 6 * 91 * 88 * 97 yeah that's it but that does not seem to be the best solution now let's read again what the task was about what is the greatest product of four adjacent numbers in the same direction up down left right uh yeah I mean up down and left right is the same or diagonally am I missing something am I maybe going I mean I'm adding I'm I'm multiplying here what happens if I go with just minus two does this crash the script cuz that could be a yeah okay this crashes the script what if I go from two probably also a problem right no okay interesting why would that not be a problem I don't get it why does this not crash the pro uh the program wouldn't this result in this here being negative oh then it actually would go oh okay I see so it basically goes back because negative numbers go and index the last position again which is not what we want so this is why it doesn't crash but it probably yields uh ineffective results now let me just see if if this is actually does this make sense why is this where could I have a better number so again what I'm doing is I'm going through the list I'm saying give me the index zero if I change this we saw that it crashes the script so I go through all the rows and I add all four elements that are side by side I do the same thing for the transposed array which is then flipping rows and columns so if I do the same thing on a transposed array I'm doing it on the columns and then I'm going through the diagonal cases where I'm saying go through the grit because what I'm saying is actually I'm saying take zero and zero so again it starts let me just zoom out a little bit so I can see the code I'm saying 0 0 and then I'm saying for all the combinations so also for this one this is also a combination because this is where I is uh four and J is some larger number here go i j time I + 1 J + 1 * I + 2 J + 2 * I + 3 J + 3 and I do that all the time I do that until I get to the end until I get to a point where I have length of the grid minus three so it would be somewhere like here did it crash when I did minus two here I think so right yeah okay oh actually now it crash because this again yeah okay so it seems to me that this is actually correct H I think we're not going to do 10 of these today I think we're probably going to do five if if even that's possible so maybe we're going to do two or three um I'm not sure right now where my problem could be I'm not even sure if this is considered but actually this is my Max right I mean maybe that's the problem maybe this isn't is isn't my Max but it should be my Max right this is what I get as a result here yeah I mean this is the largest number I mean this has to be correct this also has to be correct I'm just going through the columns and rows and I'm multiplying all these things so I think that the problem has to be with the diagonal numbers because we also saw that the product is a valid one this is actually one I found so it was 1 2 3 4 five it was here somewhere it's these these four numbers here so it is actually a valid solution it's not like it it uses one that doesn't exist so it has to be a larger number than that because this number exists and if it finds that number in the columns but it doesn't find a better one it probably has to be in a diagonal so for I in range is there something I don't consider I consider from zero so from the very border up here I consider all these things except for where I cannot go to the end so I will still take this one here because I can take the diagonal but I'm not going to take this one because that goes out of balce and I'm not going to take this one because that also goes out of bounce so I'm considering the whole Space here and I'm doing it by saying this and this and this and this I really have no idea why this uh let's let's just see if I can find this one to see if the calculation is done correctly I don't know what else to do so if product is equal to let's just see if we would find this one 1788 696 1788 696 if that's the case print the calculation for me show me what you're doing actually just to see if we even get to this point because if not something is wrong with the c calculation uh I have this problem again oh it doesn't seem to find it does it oh no it it prints the sum I'm stupid it prints the calculation itself um wasn't there what was the a syntax I forgot the syntax on how to Output also what is happening or was this python 12 I'm not sure wasn't it like equals or something uh I mean in curly brackets obviously no I'm not sure but I actually just wanted to print the number so can I do it like this yeah okay so it finds that I'm really not sure why it doesn't find now let me just try again and type in these things manually so I'm going to put it on my second screen here it's 51267 216 and it says no answer is incorrect 48477 312 would be the other one the second largest one it's for the rose no then maybe let's go with a 40 30 4286 no okay I have no idea to be honest what I'm missing so up and down should be the same so these four should be the same and these four should be the same right actually what I meant is these four should be the same and up in the up Direction they should also be the same doesn't matter really because it's you know it's a product it's multiplication diagonally we have this and we have this maybe maybe I'm missing the other direction somehow because what I do here is I start somewhere and I go NE oh my God no is this the problem this is the problem oh my God no yeah no wait yeah I think that's the problem I think that's the problem let me see if that's the problem and then I'm going to explain what I think is the problem boy that would be that would be really uh yeah wait we need to go from zero to length grid minus three here there you go I think that's a solution oh my God oh boy okay uh super stupid mistake what I was doing is I was uh going diagonally like this which is correct but then in order to to check the other direction I was going up I was basically doing the same thing instead of going like this where I have to decrease on the X axis but I have to still increase on the y- AIS super stupid mistake but this this was it okay I'm happy that I didn't that I didn't uh give up here all right boy this takes way too much time here uh all right uh let's go to 12 if we list all the natural numbers below 10 that are multiples of 30 or five we get oh this is problem one we already did that uh what was the next one 12 there you go the sequence of triangle numbers is generated by adding the natural numbers so the seventh triangle number would be 1 plus okay yeah it's basically like factorial but with addition first 10 terms would be mhm wait the first 10 terms yeah so it's one one + two what does this mean oh these are just the triangle numbers okay okay so we can see that the 28 is the first triangle number to have over five div divisor divisor yeah what is the value of the first triangle number to have over 500 100 divisors oh my god um isn't there a formula I think there was like a I think we did that in University we did some proof but I'm not sure if this was it wasn't there this thing I'm going to use my paint now I think there was this formula which said the sum k equal 1 up until n is wasn't it like equal to n * n -1 divided by two or something wasn't this the case not sure wouldn't that mean let's let's check uh wouldn't that mean that in this case uh seven would be n so if I say 7 * 7 - 1 / 2 that is one below that but I think it's correct right so if we say 8 8 - 1 oh no sorry I think it was plus that's why it didn't work so I think it's 7 * 7 + 1 / two there you go okay now can this formula help me it's definitely better than iterating over this stuff but how do I find the number of divisors um well if if something has 28 as a divisor what if I say what if I do it like this 12. py and I say 28 or let's say best divisor is 28 right now um and I say I'm not sure if that's correct but let's let's just try it uh let's say 28 is the thing and divisors is equal to six then for I in range or actually while true this could crash my system uh while true number equals 1 let's say or actually not number n equals one number is equal to n * n + 1 / 2 if the number is divisible by the best divisor but then I need to start with uh with eight then H I don't think that this is going to work or could it work what's special about 28 I mean of course one Brute Force solution let's see if that works maybe it's not that computationally expensive or I mean it will be though for I in range going from I mean this one counted as well yeah one is count it as well so one up until the number itself divided by two + one divisors is equal to zero if number divided by I is equal to zero uh or modulo I is equal to zero divisors plus equals 1 if divisors greater than 500 print number and also maybe uh this is going to crash my system bro this is going to crash my system um now I don't have n defined so n is still going to be let's go with n is one and let's say if n modulo 100 print print in just so we can take a look at it so let's say Python 3 12 py yeah this is not going to work this is not going to work I think doesn't even get to n is 100s n this is no no it is not going to work okay so I have an efficient way to calculate them by just using the formula but I have no efficient way yet to find a number of divisors I don't want to Google even though I'm going to probably do it if I don't find a solution I want to know how to efficiently find how many divisors a number has is there a pattern I can see here I mean this is one plus 2 + 3 + 4 + 5 plus 6 okay I found the pattern no this is actually just a oh man this is just a pattern for for the number this is I already know how to calculate the number uh yeah this is just the n * n +1 / two pattern but how do I find the divisors I have one here two here let's maybe print the number of divisors so let's say for each of them print n print a colon or actually let's do it in F string here as an F string here and n and then divisors oh okay well obviously it's not going to work if I don't increase in so let's run the brute force from before again now let's also print a number of divisors I'm curious to see how many we already have cuz maybe it does work I mean we could find it like this we could probably do something efficient like if a number has a lot of divisors and the new number is divisible by that number then we can probably say okay I immediately have six if the number is divisible by 28 I have six divisors so maybe we can go top down I can go from this to one and if I encounter a number that I already know has a lot of devisers I can can use that to speed up the process but uh how would I do that uh because we saw that the numbers fluctuate it's not like it's always getting larger and larger so I don't know I mean we could keep track of the best best advisor we could say I mean this would this would make sense actually so I just have to go I can I can exit the loop earlier I'm not sure if this makes sense I might be doing something really stupid right now but my idea is that I'm calculating the number then I'm saying the best divisor and say if divisor is greater than than best divisor then the best divisor is equal to divisors no if divisors is better than n divisors best divisor is number and end divisors is divisors and if that's the case I have my new best advisor and here what I want to do is I want want to go top down I want to go from this to this with a step of NE -1 and I want to say if I is equal to oh let's say if that's divisible and if I is equal to best divisor then I can say divisors plus equals n divisors and then I have to break the loop or does this make sense I have no idea if this makes sense but wouldn't this be enough to say all right break and otherwise just add plus one not sure if I'm I'm talking huge nonsense right now absolutely no idea it's still very slow though what if I do this somehow recursively like with caching I make this a function where I say how many divisors does the number have oh is difficult [Music] um I mean it can't be super simple Can it can I have like some some small expression where I put in a number n and I get how many divisors it has I don't think so it would be too simp Le I think I need some sort of algorithm but I somehow probably can simplify it and and reduce the search space I want to actually Google that I want to not Google the solution but I want to Google like what can I Google to not immediately get to the solution something like uh I mean this is already quite close to the solution it's like number of divisors recursion formula let's go maybe if you count this as a defeat you can count it as a defeat I want to know if there's at least a hint I can get what is that in mathematics a specifically and specifically number Theory a divisor function is an arithmetic function related to divisor integer M sum of positive divisor function for real complex number Z is defined as the sum of the Z power of positive what D divides n i sum up is this what I'm looking for I don't think so suppose you wish to have uh or you wish to find the number of divisors of 48 starting with one we can work through a set of natural numbers yes one time yeah okay sure hence we can see exactly 10 divisors it should be also clear that using this method we only ever need to work from one up to the square root of the number I mean oh we actually didn't we learn about this already in the last episode because we did it with primes so maybe this can help reduce the search space maybe that's enough maybe we don't need to keep track of this maybe we can just go to the square root so I need to say if that's a solution I'm going to be pissed because I should have learned this in the last video math.sqrt whatever we get or actually we don't need to division here sqrt the number make an integer of this and then plus one oh this is way faster but probably still not fast enough it's much much faster yeah but not sure let's think about this or let's read further um although this method is quick and easy with small numbers it's tedious and impractical for larger numbers yes this is what we saw right now fortunately there's a quick and accurate method using the divisor or to to function whatever you want want to or however it this is pronounced uh let DN be the number of divisors We Begin by writing the number as a product of prime factors okay there you go again with prime factors then the number of divisors DN is equal to a + 1 b + 1 C + 1 to prove this we consider numbers of the form Nal P to the^ of a the divisors are okay I don't think I understand this We Begin by writing the number as a product of prime factors so these are primes I think okay I get it so if you have a prime number to the power of a the divisors are one and all the pr or the Prime Times yeah does this make sense I think so yeah because we you can Define yeah uh you can divide by by P and p^ squ and so on because as long as it's less than a got it okay understand this part now consider we have two primes then the divisors would be okay so basically we just have to do prime factorization and then what A+ 1 B+ one should be clear how this can be extended for I don't understand what A and B are exactly so if I have something like 28 let's see if we learn something if I want to do prime factorization we said we I forgot how we did prime factorization uh how did we do the prime factorization last time this is now you see I didn't learn anything let's say Prime the py if I have a number 28 and I have a function is prime if n or for I in range one [Music] [Applause] sqrtn if n modulo i equal Z return fals return true then basically how did we do the damn prime factorization last time wasn't it like dividing by the largest prime or was it just dividing by primes in general I mean wouldn't it be just if we divide it by two then we have 14 so if I let's do I divide it by two I have 14 I can divide 14 Again by two that was not how we did it or was it not sure I mean what kind of devisers do we even have we have seven seven is a prime if I divide it by seven I get four seven and four I get two then I get two again so actually it would be 7 to the power of 1 * 2 to the^ of two so the number would actually be 2 + 1 * 1 + 1 which would be 3 * 2 which is six okay this is exactly what I saw here so this is correct so I just have to know which prime factors it contains and how many times but isn't this something I can do quite easily can I not just say let me go back to the other one here can I not just say let me copy this is Prime and then I can say for all the numbers up on tell this if is prime I actually am I not making this more complex how did I do it the last time let me just uh look at something here on my second screen I need to find a directory so if I go and say fuzzy finder Oiler there you go stuff project Oiler so it's stuffff project Oiler okay so here I have the solutions from last time uh what did we do last time for the prime factorization oh actually we did it exactly like this so we said if the number is prime then we want [Music] to actually only if the number is divisible so if the number is divisible by I if I is prime then what I want to do is I want to say [Music] [Applause] it's getting messy now so Prime amounts an empty list and if I say now four or how could I do that well I wouldn't need the divisors then because I'm not counting them anymore so what I would be doing is I say check of the number is divisible by this number here so I actually have to start with two if this number that it's divisible by is prime take the number or while while the number is divisible by this Prime set the number equal to or we divide the number by the prime and say Prime amount actually let's make this a dictionary Prime amounts Prime amounts here keyi is zero Prime amount i+ = 1 not sure if that makes any sense uh and then in the end we want to say actually let's just print the prime amounts and see if this actually works uh where am I right now empty dictionaries okay perfect this is exactly what I wanted not so I have a dictionary I get the number I ask is the number divisible by this number if yes I check if the number is prime the divisor is prime if that's the case I set this to zero now it seems that I'm not getting here which is interesting and I don't know why really I know why because I have to start with two okay um let's see what we get here actually I want to also print the number or actually n so N7 should [Music] be N7 should be 28 and I get 2 * 2 but it doesn't find the seven that's also part of this which is a problem why doesn't f doesn't it find it it should find it though right because I go in I get 28 I say okay four range two then okay 28 is divisible by two 2 is prime I start with 2 equals 0 and then I say while number is divisible by two which it still this divide it now I have 14 I increase the amount by one go back still divisible by two 14 ided 2 is 7 Prime amounts + 1 okay no longer divisible by two got it there you go 3 4 five 6 7 oh it's using that okay I got it uh number shouldn't be that it should [Music] be maybe we should just put in here n * n + 1 divided okay this is getting messy let's let's do it let's say original number is this the most recent run no there you go still doesn't find it but the original number doesn't change right so it still should go to seven or should it what's the what's the square root of 28 no it doesn't go to seven I thought I just have to check up until did I misunderstand this I mean 28 is divisible by 7 but the square root of 28 is 5.2 and if I A+ 1 it's 6.29 um so I never really get to to seven why is that not sure um one thing that I could do to quickly fix this or I'm not sure if this is going to fix it but I could just go until this point here it's way slower but it's correct so the answer here would be prime amounts. values [Music] uh um it was X + one for X in Prime amount values and then we need to to use reduce I think where do I put the collection it's the second argument so I need to put the function first which is going to be Lambda a a * B why is this a problem print isn't this a valid list okay in the beginning it could happen so let let's say if length of X or length of L of this is larger than two print this so for seven this is giving me three and two yeah for seven I get six okay so now we have the calculation done but it's quite slow so that is actually the final result so this thing here is the divisor result um the problem is this is super slow and if I don't check this far for some reason it doesn't find the number now I want to do something here uh again if you consider that failing consider that failing I want to do something which is actually not I wouldn't say it's against the rules but I set up not going to use chat GPT I don't want to use chat GPT for the problem solving I want to ask chat GPT a theoretical question here I want to ask um when I check if a number is divisible by or when I want to find when I want to check if a number is divisible or let's is a prime I read that I only have to check up until it's square root oh because probably okay if it's Prime yeah because I already see it's not prime when I divide by two but it's not enough to check for all the divisors okay uh when I want to find divisors of a number I read that I have to check up until the square root but for 28 7 is a divisor and it is pass the square root of around what was it 5.2 let's see what it says the principle there you go [Music] mhm because if D is a divisor of [Music] N and n ided by D is also a divisor oh probably I think what we could do here is we could go back to this but say if num or if is prime number we could say Prime amounts number equals one maybe that solves the problem uh let's see if that works seems to work not sure let's see uh if divisors greater than 500 print number actually I'm not terminating here um break in this case and don't print all the other stuff huh what is this now oh not number but print original number oh okay that is now correct that would be great so no oh I was I was actually hoping that this would be true um okay this is now unfortunate let's print this one here again I want to see if it always gives me the correct the correct value so I do get four or three I mean three is actually this one here right yes we got 4 4 four six and then the next one is 18 is that true is it true that the number after that which is what is the number to be honest this this format here is more like a studying session than a coding session it's more like learning math and you can watch me do it um so if I have 8 * 8 + 1 divided by two that's 36 36 doesn't have 18 divisors does it I mean it could have it let's uh let's ask chat GPT again I don't consider this cheating because I'm not really asking for a solution how many divisors does 36 have it's nine not 18 so there's a problem with my calculation uh oh I think actually I think one of the problems could be that I'm setting this here but I'm not dividing it is that the problem number has to be divided by number so number actually has to be one afterwards or in this case I would have to break right I mean let's see let's go again to desktop Oiler stuff python 32. py oh now I get a different number let's see still wrong oh man okay let's go up and see if the numbers are correct this time I get 18 I still get 1848 why is that let's let's think about this we start with 36 we see 36 is divisible by 2 two is a prime so we say Okay divisible by six um so we set 2 equal to zero we divide it 36 / 2 is 18 so we divide it 18 and we get back here we have then 18 divisible by two yes divide it again it's 9 so I have 2 is two then it's no longer divisible by it I have nine nine is not a prime number so I go out of this and I start again with a three nine is divisible by three so three is also Prime 3 = 0 9 / 3 uh is possible so divide it we get three so I get oh there you go I get the problem if number in Prime amounts. Keys we want to add it not replace it otherwise we want to set it that is a big issue now I get again a different value no actually I had this one already come on but do I at least get the correct value for eight now no I still get 18 why do I get 18 okay let's go back again so we had nine divisible by three yes three is prime yes set 3 to zero while 9 is divisible by three divide it so now it's three and add one to it okay so we have 3 1 then we have three still divisible by three now now we have two then number is one one is not prime hopefully if n = 1 return false I think that could also be a problem okay we get a new number there you go finally there you go okay so this was super interesting this was my favorite one uh until now this is super interesting so what I learned here and I'm probably going to forget it for the next time is it's only enough to check up until the square root if you're considering that the result in the end is going to be a prime and it's also of course divisible by itself so that's important but it's still enough to check you just have to then understand that the remaining number is also Prime and added so you just get the prime factors you count how many times the individual prime factors occur it doesn't matter which one they are but you get the prime factors you count how many times they divide the number and then you add plus one to all of them and you multiply them together to get the result super interesting super interesting um I didn't solve it myself completely I used chat GPT uh but let's go and see what the PDF says actually so let's go to desktop yeah basically it did exactly this okay awesome okay this was a super interesting one uh we're already past the 120 Mark so I'm going to actually end this video we only did two challenges but they were quite especially this one was quite challenging so uh we're going to continue with the rest in another video I hope you enjoyed it that's it for today so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
Today we continue with the math coding challenges on project Euler.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Timestamps:
(0:00) Intro
(0:16) Largest Product in a Grid
(32:24) Highly Divisible Triangular Number
(1:21:48) Outro
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Indian AI coding startup Emergent becomes a unicorn with $130M Series C
TechCrunch AI
Disposable software
Seth Godin's Blog
Join live event: Building With AI Without Creating Technical Debt
Reddit r/learnprogramming
I Built an AI Coding Cost Tracker to Finally See What Copilot and Cursor Are Actually Costing Me
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI