I made a whole Language just to solve Coding Problems

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hmm looks like we're live so yeah hello everyone and welcome to another azusan session how about that hello by robert hello hello uh so let's make a little bit of an announcement on um discord server for everyone that the stream has started um all right so uh red circle live on twitch and what we're doing today uh we are drumroll we are solving project all your problems in port how about that isn't that cool i think that's pretty cool so we've been developing uh our own programming language called port uh for quite some time you can find the link to the language in the description if you're watching on youtube if you're watching live on to which you can find this thing by the command project right so anything the language is mature enough to try to do something with it and one of the things i decided to do on one of the previous streams is that i'm going to go ahead and try to solve some of the project euler problems in that specific language and if while solving the problems we'll encounter some features that are lacking in the language we're going to implement them and then we're going to solve the problems so solving the problem will help us to identify uh what kind of things we're missing in the language to for for the for that language to be useful so uh that's basically the idea uh hello everyone hello recursive chat cnd a man video on the mad beat stream algorithm shooter mm-2pl hello hello welcome welcome welcome really glad to see you so uh yeah and photoshoot doesn't know project euler is basically an archive of you know something like competitive programming problems or um so it's basically um more of like a computer science math problems and uh yeah i already solved a couple of them actually more than a couple uh but i solved them in a different language i don't remember which one i used i think i used a combination of c and haskell and stuff like that uh so but i'm gonna try to basically solve those problems again but in port uh so hello hello everyone who just joined nothing hello hello uh so does it sound good sounds gucci sounds tamagotchi i've got a little bit of tea um it's going to be very interesting so uh is our language even useful for anything for at least some imaginary problems alrighty so let's take a look uh let's take a look so where is our language so here is our language and uh just to remind people our language is basically stack based or some sometimes people call this language is concatenative uh right so essentially um if you wanna sum up two numbers right if you wanna sum up the numbers you basically uh do it like this you do 10 20 and plus right so it uses like a reverse polish notation and essentially what it does is that when you do a number it pushes that number into some sort of like a data stack then it pushes another number into the data stack and then uh when it encounters plus separation it pops these two elements from the stack sums them up and pushes them back right and after that you can print this entire thing and this is a valid port program right you can go ahead and use the compiler to compile this program into an executable as you can see it will generate an assembly code and then it will compile that assembly code with assembly with nasam and then it will link that generated object file into an actual executable right so and here is executable that sums up two numbers uh 10 and 20. so as you can see it's a it's a native executable f64 bit and if you try to run it it will print uh 30 as you can see yeah these 30. uh can you do stuff like handling get requests something that can communicate between services written in different languages uh i'm not sure so you mean something like http um so well you can implement a http in that language but uh well it will take some time but eventually we're gonna do that eventually we're gonna do that so it's gonna be rather interesting uh we're definitely gonna implement uh something like actually useful like maybe jason passer or http client and http server at least for http 1.1 um you know stuff like that uh we only have six calls but i mean uh everything in your operating system uses c schools right it's just like the matter of implementing all of these things uh in in port right it's just a matter of like doing all of that work um okay so let's actually go ahead and try to solve one of the one of the problems uh and see how it goes so i'm gonna like i kind of want i kind of low-key want to use the solutions to these problems as the tests for the language uh you know that would be kind of cool uh maybe i'm gonna actually start developing in uh right in the source code of the language so it can be a folder euler uh how do you spell euler um it's um euler there we go and we're gonna start with the first problem multiples of three or five uh so if we list all the natural numbers below ten that are multiple of three or five we get three five six and nine the sum of these multiples is 23. find the sum of all multiples uh of three or five below one one thousand okay that sounds actually pretty pretty feasible to implement right now so let's go ahead and do that so problem zero uh zero one fourth uh what i'm thinking i'm gonna try to uh first uh implement the thing that does things below ten right so what's interesting is that since we only care about multiples of three of five we can start iterating things not from one but actually from three right so if i do three and while uh dupe um is uh below right it's supposed to be below less than ten we're going to do a dupe print so right now i'm just trying to print all of the numbers from three to ten right that's what i'm doing so and i'm gonna go ahead and just try to compile the entire thing uh problem zero one and i'm gonna right away run it uh i think i did a little bit of work here uh there we go and as you can see it went into an infinite loop that doesn't allow me to even stop this entire thing and the reason why it went into an infinite loop is because i never increment the counter within the loop right so it's kind of a dangerous language it's a pretty unsafe language you go you can go into an infinite loop uh right there we go so here we have numbers from three to nine uh which was relatively easy to actually generate so the thing we need to now do uh is check if the number is multiple of three or five uh how can we do that so we can duplicate this number on the stack and uh maybe take a mod of three right we're taking a mode of three and interestingly enough we can just check if it's equal to zero uh right [Music] so if we take a look at this stack so we have three then we duplicated three uh on the stack um and then we put three in there and we found the mode i would actually uh like to actually replace this with an index right so now we have this thing in here and then we compared it with uh zero right we compared it with zero uh but now i need to have a second condition that does the same thing but with five so i need to duplicate index yet again right but i cannot just do a dupe in here right i cannot just do a dupe because it will duplicate this value on the stack i want to duplicate like this one for this one i have an operation called over right what it does it uh duplicates the element over the top right so this is what i'm gonna have on here uh there we go over the top or another solution would be to actually duplicate well i mean it's a good solution i think i think it's a good solution so in here we're gonna push five mode equal zero and uh as you can see here we're gonna have two conditions in here mod uh three right so here is a mod three uh and uh actually it has to be five right so we have these two sort of like booleans uh on top of the stack and i think we need to actually combine them with or but we don't have a logical or right now which is kind of sus uh we only have a bit or but since the logic operations produce 0 or 1 anyway i think logical or actually bitwise or is totally fine in here right i think uh bitwise or is totally fine to combine these two uh two conditions together uh right so and there we go we have some sort of a condition right and if that condition is true uh right we're going to print that number otherwise uh we're not going to do anything right there we go so we implemented some sort of filter uh that um filters out only those things that are multiple divisible by three oh by five right so and this is basically a condition for if right so this is how it looks like in concrete native languages when you write it like this the iteration variable being implicit is weird um so it's not implicit there is no iteration variable that's what's funny uh all right i can show you how the uh loops even work so first thing we're doing here we push three onto the stack right so we have a three on the stack the next thing while doesn't do anything while doesn't do anything at all it exists for the compiler to know where to jump when it reaches the end so when the compiler reaches the end it just knows that it needs to jump in here and that's it while does literally nothing so then we have a condition and the condition duplicates the number on top of the stack then pushes 10 uh like so and then it checks whether uh this value is less than 10 right like this so as you can see on top of the stack after these three operations we have a boolean true or false and now the next operation do it does something what it does it takes a condition on top of the stack and if it's true it just falls through and execute the rest of the operations right it just falls through and executes the rest of the operations but it was false it will jump straight after the end and finish the loop so there is no implicit or explicit um variables here at all it's just two uh two instructions do which always pop an element from the stack and checks if it's true or not and end which always jumps to the while and while does nothing it serves as a marker for the end to jump to and check this condition again this is how it works uh so and the iteration variable like variable right it's something that is always on top of the stack all right and that's why i have to increment that thing on top of the stack at the end of the iteration in here um so uh yes right so uh let's take a look if uh now uh we only have the things that are uh multiple of three and five uh let's go ahead and run it and as you can see we don't have a mod and the reason is actually rather interesting is that because mode is not a built-in intrinsic uh it's not a built-in intrinsic it's available in the standard library right so i recently made some changes to the language uh std porth is not found oh i know why it is not found it is because the std is available in here right and it's not in the include paths right so we need to do in here i think we have to do something like std and there we go so now it kind of works but it didn't really do anything for some reason um okay uh let me see so it consumed both of the separations and now i want you to print anything but it really didn't do much uh this is really strange so let me uh comment out this entire thing so this is duke print all right so this is the print do print works um but if i do dupe three mode zero equals yeah i have to swap it i first have to put a zero and then compare it okay so i found the mistake uh we need to implement some sort of like a static uh type checking that actually like catches this kind of errors right uh this is i think this this should be feasible but i don't want to introduce uh static typing until after i implemented like i reimplemented fourth in itself right because if i introduce static typing it will complicate the compiler so i'll have to do more work to rewrite it in itself so i don't want to do that uh i don't know so now we only have things that are divisible by three or five threes divisible by three five is divisible by five six is divisible by three and nine is divisible by three so and all of them are below ten so all of them are brilliant and what's interesting is that as already said mod doesn't work by itself you have to include standard library and this is because we don't have neither division nor mod operation uh in the uh built-in set of words of the language the thing that we have um the thing that we have we have an operation div mode right this is the built-in operation and what it does uh it essentially uh takes two numbers right and then it divides a by b and pushes that and then it takes more of this entire thing and pushes that so essentially it pushes two things simultaneously and if you want to only division in only mode you can use this macro that basically drop unnecessary information like drop and soap and so on and so forth right and the reason why i did that is because it's a little bit closer to how x8664 works if you know the div instruction in x8664 it actually performs uh division and remainder simultaneously and it pushes the result of the division into racks and the result in the remainder into rdx right so it's simultaneously available in both of these things and i decided uh if you want to be efficient and just do these things simultaneously you have this thing and then you have an opportunity to drop just whatever you don't care about uh all right so i decided to implement it like that and that's why it's instant library now we have a lot of things in the standard library by the way uh i have like a cisco uh wrappers i also have cisco constants uh right if you wanna call cisco directly you can pretty much interact with linux now right at least you have like read write and exit and you can probably open files you can already open files by the way i haven't tried that yet uh but theoretically you can try to open a file and read from pyle and maybe generate another file which is precisely what compilers do right compiler is a program that opens file reads the file opens another file and generates the content of another file that's precisely what it's doing that's precisely all the uh interaction with the operating system it usually needs uh right and we already have that in the language right so you can already do that um which is kind of cool uh so used to use we used to go away freaking this so now what we need to do in here instead of printing those things we need to actually accumulate them uh the question is where are we gonna be accumulating this entire stuff this is very good question uh we can be accumulating it on the stack as well um which is fine i guess uh and we can try to do that okay so we're gonna push the accumulator in here uh right and at the end of this condition we only have the index right we only have the index at the top and uh the accumulator over the top so what we need to do in here i suppose we need to uh swap this entire thing uh right so because uh what we're gonna have in here is uh accumulator index we swap them around and then what i probably wanna do i want to do over which will duplicate index over here and then i want to sum them up so this will give me this thing and i suppose i want to swap them back uh making the accumulator uh like this and after that i will just increment the index by one okay cool so after that i probably want to drop the index and here we go at the top of the stack i'm going to have the accumulator that i just need to print that's pretty much the entire solution uh that's pretty much the entire solution uh all right so let me try to run the entire thing and see if it's gonna produce it produce 23. is that the correct solution uh it is 23. look at that actually uh it is straight up 23. so the next thing we need to do we need to actually solve it for 1 000. so what we need to do in here we just need to replace it with 1000 and uh we have two three three one six eight two three three one six eight there we go we sold it so uh yeah so it's kinda and it's not that big of a solution like if you look at the like how much it takes in terms of code uh that's the whole code really not not that much though granted it's a very simple uh problem and very simple solution but um it generates an assembly so it's like a native programming that's pretty cool so maybe i'm gonna start actually um you know doing things maybe testing uh what you can do in here by the way in in the test suite you can uh specify the folder right and specify the folder euler right and then i can say record or record i don't know how to probably pronounce it and what it does it actually records the output the expected output of the of the program right and it puts it into txt file right and the next time uh you're gonna run the tests on that folder it will actually assert uh that the um you know generated program i'll put it the expected result right if you made any mistake in in the program right so replace 1000 with 10 uh the test will fail and as you can see it tells us that expected uh two three three one six eight but we've got 23 right so you can already use it for testing if you want to right so which is pretty convenient um so uh yeah let's do one thousand [Music] alrighty and um so this is just examples this is euler and let's go ahead and implement the second one even fibonacci numbers okay each new term in the fibonacci sequence is generated by adding the previous two terms by starting with 1 and 2 the first 10 terms will be like this but considering the terms in the fibonacci sequence whose values do not exceed 4 million find the sum of even valued terms holy so that means we first we have to do the fibonacci sequence then we have to filter out everything that is owed and sum it up that's a lot of different things and uh we'll see how language is going to stock up with that um all right so this is going to be uh problem zero two it's gonna be fourth and we're gonna include uh std ports uh so the first two numbers uh one and two okay so one and two um one and two this is what we have on top of the stack and i suppose uh we're gonna use the first value as an iterator right so i'm gonna do over right so this is over this is over and um let's say that it has to be less than 100 so let's just like learn how to generate uh fibonacci numbers let's learn how to generate them all right after that uh what do we have uh i suppose one way to generate the next fibonacci number is to essentially swap them right that makes uh the second one with the first one then i'm gonna copy the one that is over this thing and then i'm gonna sum it up right so i'm essentially summing it up and uh here i have something like this never go that should be you know sufficient enough uh on top of that we probably want to print them so i'm gonna do over uh copy the over and then print it so that will generate fibonacci numbers that are below 100 hopefully we'll see so let me let me see uh and i did forget to do something which is kind of weird uh so what did i forget to do [Music] oh yeah yet again so i need to get used to the positive notation yeah i need to get used to it uh all right so here are fibonacci numbers like from uh 1 to a 89 there we go um so and what's funny is that there is like exactly 10 of them right so this is exactly 10 of them and maybe i can already try to sum them up but summing them up already becomes kind of difficult right because that means i need to have an accommodator right so i already have like a and b like the nth fibonacci number and n plus one one first and plus first how do you say that um so and now i have to put accommodator like some way here right so and for instance if i want to copy that value i don't have a like over uh like two or not so it's kind of not convenient it would be kind of better if the accumulator was located somewhere in the memory you know what i'm talking about it would be kind of cool if it was located somewhere in the memory and we have an ability to work with the memory we have a memory buffer and you can do mem and that pushes the pointer to the beginning of the memory buffer right uh to the beginning of the memory buffer and then we have operations like comma and dot that actually read and write into the memory but here's the problem these two operations can only write one byte right they can only write one byte but here we need an ability to actually read and write bigger numbers right because we have an accumulator and the sum of the even fibonacci numbers below 4 million is probably going to exceed a byte it's probably going to exceed 255. so we need an ability to actually you know uh say how many like what kind of number we wanna uh we wanna read in write into the memory so i had the following idea actually i had the following idea so that i would implement some sort of a typing for the uh load and store operations so yeah the comma and dot are load and store operations so for instance by default they're going to be one byte and then i could say okay so here's the memory and then i say u64 uh and read and what this thing does it sort of like uh hints what kind of type you want to read so in this case i want to read u64 so and uh the same is going to be with like dot which uh which reads and what not that will be kind of cool right and then we can have different types so by default it's u8 then maybe u16 u32 u64 and um yeah but here's the interesting thing uh i could probably implement that myself um i could probably implement that myself because if i can read one byte what i can do i can read uh 8 bytes and then construct a 64 bit number out of it you know i mean it's going to be slow right it's going to be extremely slow because you're essentially like pulling out byte by byte and then just constructing it into the stack and uh but it's going to work without modifying the compiler that's the cool thing about it um right and furthermore we have the system of macros so we can actually tuck this entire behind the macro uh that would be interesting actually what do you guys think so uh yeah let's actually try to implement something like let's implement a macro called load 64. right that basically pulls out 8 bytes from the current memory do you have more than 8 bit arithmetics yes so essentially each number on the stack is 64 bit number and every time you pull out a byte it's actually converted into 64-bit number so it's like it's loaded into the lower byte of 64-bit register so uh you can actually do some shifting and whatnot and uh you know it's gonna be it's gonna be feasible uh all right so let's imagine how you would use that uh load 64 you would do something like memory and then you would do load 64. so that means on the top of the stack and here we have uh the pointer to the memory all right we have a pointer to the memory um so what i want to do with that point right uh i want to probably duplicate it right so i'm duplicating it so i have the memory in here uh so that also means that i need to maintain uh i can and accommodator right accumulator into which i'm going to be um saving all of that so maybe one of the things i want to do i want to first push the zero right like this and uh swap it around because i wanna keep the uh memory pointer uh at the top right because again we're gonna be using it uh all right so then uh we also need to consider what is the um engines right so we're on x86 64 machine so it's gonna be little engine right so uh bleedo engine so it's a little engine uh so then we're gonna duplicate the center i think right this gives us um this gives us the oh man that's actually very interesting so well let's just try to implement that um let's just try to implement that so we've got that and then i can read this entire thing so that means i how now have this thing and now i need to add it to the accumulator right somehow i need to add it to the can later which means that i already up right um which means i already up because i i cannot just add it to the camellia okay let's actually go back right so we have a memory uh and then i push zero right i push zero uh so the next thing i need to do i need to copy over memory right i'm copying the over in memory and then i'm reading this entire thing and now finally i can add it to the accumulator right so i do plus and that adds this entire thing uh to the accommodator perfect so i have to do that like the other way around right i had to do that the other way around so and if i repeat this entire thing all right it will uh keep adding this thing up uh but what's interesting is that before i will add that zero i think i have to shift the accumulator by one byte to the left right so i need to actually uh shift it by one byte to the end uh so so this is a memory um so maybe it would make sense to actually shift it somewhere here so one byte is eight beats so it's gonna be eight uh right this adds eight a shift left right so this is going to shift left so let's actually limit the accumulator and there we go so we have something like this and we have shift left then i do over all right then then i read this entire thing then i read this entire thing and then i sum it up uh right then i sum it up there we go so now we have at least something repeatable right uh now we have at least something repeatable so because now we can do that eight times uh almost we almost can do that eight times because after that uh what we have to do we have to swap this thing around right we have to swap this thing around and add one to it one plus right one plus this will add one to this entire thing and then we're going to swap it back and on this then we'll be able to repeat this entire thing right on this then we'll be able to repeat this entire thing uh there we go so this is the line that is probably repeatable so let me see one two three four one two three four right uh and there we go so we probably constructed a 64-bit integer but it's not correct by the way it is not correct at all you know why because it's a little indian right so essentially the least significant bytes are located first so it's going to be uh a0 a1 uh a2 a3 and so on and so forth and how we're constructing everything uh we essentially take a0 then we shift it and then we put a1 a2 and so on and so forth so as you can see the least significant byte becomes the most significant one so this way it is completely incorrect so we probably have to you know what we can actually do a very interesting thing we can set the memory to uh the end of the number and do absolutely the same thing but instead of adding one to the memory we can subtract one from the memory um so and essentially it's gonna be pretty much the same thing but uh instead of moving from left to right we're gonna be moving from right to left so produced it for what do you guys think um so we can try to do something like that um okay so uh we have one two three uh four five six um all right seven so we have eight bytes and the memory actually starts somewhere here so how we're gonna be doing all of that uh so this is the memory uh we can essentially add eight but that means this pointer is going to point at the thing before uh or after after the number so i think what we have to do here we have to add seven right we have to add seven and then uh add this byte and then uh move backwards okay so let's give it a try so at the beginning we have uh memory so the first thing we want to do we actually want to add seven so it's gonna be seven uh seven plus right seven plus this is uh plus seven uh after that we are adding an accumulator which is going to be zero right uh so i'm gonna put it like this and to confuse people around we're gonna put seven plus zero in people who don't know can catch native languages we are going to wonder uh what the is seven place plus zero okay i think this is beautiful in my opinion uh all right so then we put eight and then we do shift left or uh is it shift left oh yeah it's it's a shift left all right there we go so we do shift left then we're copying the memory over right we're copying the memory over uh right and we are reading the byte right so essentially reading that byte and there we go we have a red byte then we add uh this entire thing to the accumulator right so we add the entire thing to the accommodator then we're swapping this thing around uh right swapping it around and uh one and this is where we have to do minus right there we go we have to do minus one uh right so this is minus one which will result in six and then we're swapping this entire thing back right then we're swapping this entire thing back and there we go so now we can replace this kind of stuff and what's interesting is that the last swap one minus swap is really not needed right because this swap is gonna basically put it below the number and it's like really really not needed so i can probably do something like this right so i can just get rid of that that [Music] uh two to the two all right uh what was interesting is that uh we're gonna end with a memory pointer and the final accumulator right the memory pointer and the final accumulator and we need to get rid of the memory pointer so what i need to do in here is just swap drop right so this is the last thing i need to do like swap uh the memory and just drop it so and this is going to end up something like this right and we're gonna end up with uh just a single accommodator right it's just a simple accumulator and this is what you can do without modifying the compiler to just read 64-bit no isn't that a beautiful language i think i think it's pretty cool pretty cool language uh this makes sense hopefully that makes sense i don't know so we need to give it a try um so right now uh in the memory i suppose we have nothing all right uh we can try to just do something like mem load 64 and just print what we have loaded um i'm not sure if it's a generally good idea [Music] so it's kind of difficult to test this entire thing unless we implemented store 64 if you know what i'm talking about uh i think we need to implement store 64. because in that case i could just at least iterate over some range of numbers and just sum them up just to see if it works or not uh but yeah but we can just try to to just run this entire thing and see the okay so it exited abnormally cool it existed abnormally i'm really happy um so um i suppose oh this is because dot is uh writing into the memory we have to read uh right we have to read from the memory okay so that makes a little bit more sense uh there we go so and it actually like printed zero in here there we go this is roughly what they expected uh but since we have nothing in the in the memory so i know that's that's fine i guess uh we can write something into the memory like 255 or maybe like 69 uh looking to least significant byte like 16 uh memory uh 69 right uh zaki08 uh subscribed with twitch prime thank you thank you so much for for the subscription and uh also lori master is gifting uh a sub to entradocs thank you thank you so much for gifting this up and supporting the support in the channel really appreciate it okay we can write a 69 into the memory right and uh so if we load 64 it's also going to be 69 so it's not really testing anything right as you can see it is 69. though we could probably um okay so luke uh subscribed with twitch prime thank you so much for three months of twitch pancake screen thank you thank you thank you um so yeah thank you so much so if we actually put this thing a one byte to the right uh all right so it's gonna be a slightly different a slightly different number so this number has to be equal to 69 multiplied by 256 if i understand uh this stuff correctly so we can already kind of test this thing so 69 multiplied by 1056 and there we go so it's actually matching right so it is actually loading 64 bit numbers correctly more or less we can actually uh try to test this thing so if i put 69 into the third byte this is the value we should see uh being printed so let's actually give it a try so if i put a 2 in here and then i'm going to try to do this thing and as you can see it's 45 21 98 okay so this operation works correctly uh more or less right so we can already load 64-bit numbers from the memory into the stack and do some sort of operations with it so but we need to be able to store 64-bit numbers right so uh macro store again i do realize that it's kind of slow and it would be better if the load 64 was a compiler intrinsic right but i just want to implement it for now and see how slow the the program will be and then i'm going to make it in intrinsic and see how much of speed up we're going to get by making this thing intrinsic you know what i'm talking about i think it's going to be kind of interesting all right so here is the macro and um okay so what do we have in here zero zero one two three four five six seven uh essentially here so we're storing a value right we're storing a value oh and since we have a value we're actually by shifting it around we can iterate it from left to right we don't have to iterate it from right to left okay this is actually perfect i really like that um okay so here uh we're gonna have uh the memory and then the value right we have a memory and then the value so memory and the value so what i want to do in here is probably copy both of them so i have an operation called two dupe right so this is a two dupe and that copies two of these values right two of these values then i need to extract a byte the first bite out of this entire thing so the question is how i'm gonna be doing all of that well i can put uh 255 right so this is gonna be the current byte and then i'm gonna combine it with the end operation so this is going to be 255 and there we go and after that i suppose what i can do is just uh write that into the memory and that will write this value in this specific place right so that eliminates that so after that i need to actually move value by one byte right so what i need to do here is eight eight and uh shift right all right so this is going to be shift right cool but i also need to advance the the memory pointer so i need to swap the memory point like so this is gonna be that then uh one one plus and there we go so now we have this entire thing and we can swap it back cool and we're back to square one we're essentially back to square one and we can just repeat that eight times one two three four one two three four and again so at the last one we don't really need to do swap one plus one swap uh but one of the things we'll probably need to do we need to just get rid of both of the values because store just stores both of them and that's it so we can just do something like drop drop or i think we can do two drop right i think it also works um okay so that's probably the entire thing which is a little bit more like simple it's a little bit easier to store values than read them and to test this entire thing let's actually do the following thing uh we're gonna have memory one two three four five six seven eight nine right and let's store that value in the memory then we're gonna load that value from the memory and we're gonna try to print it and everything if everything is correct we're going to see the one two three five five uh printed on the screen right so essentially what we did in here we stored that value into the memory and then we read it back uh and what's cool is that store 64 and low 64 will expand into sequence of these operations so we have we have macro system um all right so let me try to do this entire thing and it exits it abnormally nice very cool i wonder why uh is it because i forgot yet again well i mean yeah i supposed to actually put a dot in here but i'm not kind of sure uh so let's try to do that one more time so let's try to understand what the hell is going on uh so we have the pointer and the value right memory and the value uh to the two so i do two dupe right i duplicate both of them then 255 and end right now this is not how it has to end so this is what we've got and then i do a dot and dot is the store that you store in a single byte right that's what essentially we're doing we're storing this in goodbye so that will consume the whole thing in here i didn't see any problem with that uh so oh yeah i'm just okay thank you so much yes thank you thank you thank you i see i was just missing them uh all right so let me see and there we go so it is correct so i just made a mistake but not in the macros themselves uh so that's pretty cool so now we can actually store and load 64-bit operations in a very scaffed way uh but that's fine i guess uh all right so we can try to do the following thing i'm going to iterate over like one while dupe uh 100 less actually let's actually make it equal right and essentially uh by default in memory we have all all of these zeros right so by default we have zeros uh so what i'm gonna do in here is i'm gonna uh duplicate uh the index then read this value from the memory uh hector hc hsc thank you so much for 100 bits okay so i'm loading this thing from the memory right i'm loading from the memory and add it to the index and now i need to save it back uh so let's do that slightly differently i think i'm gonna put a memory in here and then i'm gonna do store uh 64 right so as you can see in here i'm loading the value um [Music] well yeah it's not gonna really work that well okay so here's an index uh right i duplicate the index this is gonna be the index then i push the memory this is gonna be the memory then i load 64. that gives me this thing uh then i add this entire stuff right so as you can see we just edited it together and unfortunately i still have to do this like memory right which pushes the memory and now i need to swap uh this entire thing to put memory in here uh right i have to put it in here oh i i could do a slightly different thing okay so this is actually perfect i can do memory uh zombie show thank you so much for three months uh of tehran subscription thank you so doing cool indeed uh thank you thank you so much uh and welcome to our epic porth club that's right so we're solving euler problems in our own language at least we're trying right uh so okay so i'm gonna do memory that will put the memory and then to copy an index right to copy an index i'm going to do over which will give me the index it gives me the index or maybe i can actually do two dupe right so that will give me that uh then i essentially uh load 64 and then add this entire thing together and finally store 64 and finally okay so this is actually pretty cool all right and then i can do uh one plus and there we go cool you see so now now we have this like a very comp uh like a uh compressed expression so now i'm gonna do mem load 64 and i'm gonna just print this entire thing so we're summing up numbers from 1 to 100 that's what we're doing and yeah this is a sum from 1 to 100 i know that for uh for a fact because of the uh the progressions right so yeah the sum from 1 to 100 is 50 50. so as you can see we have no ability to read and write 64-bit numbers from the memory but that didn't stop us from implementing this thing ourselves right right so we're pulling out the bytes one by one from the memory and we're constructing it on the stack and it works it's probably slow as but i mean it works uh we can use that as an accumulator now uh okay so and this is how it looks like the uh all right so we have a load 64 and store 64. uh we can actually put that into the standard library uh i don't know so yeah let's actually go ahead and put that instantly why not uh we have a full control over the compiler uh so now this is a part of the standard library and we'll be able to just use this entire thing okay so what was the problem does anyone remember what was the problem uh the problem was to actually sum up uh all of the even fibonacci numbers um below 4 million okay that's cool so here we actually um iterating over the fibonacci numbers uh below 100 right so that's what we're doing in here so if i try to run this entire thing so here are all of the fibonacci numbers in here so uh let's try to um now maintain an accumulator right so accumulator is going to be somewhere in the memory uh you know what uh we have an ability to actually alias uh memory to something like ack right uh there we go so now every time i do ack i actually push the pointer to the accumulator not to the memory in general right so we can sort of abstract it out uh so we need to take a look at the even numbers right so i do over and then i try to divide the number by two uh i'm dividing it by two and actually i need to do a mod right and if it's equal to zero only then i wanna do something about it right over print there we go so now we are printing only the even ones uh but we need to add them to the accumulator so i do accumulator load 64 add it to the uh to the number then um um so this is the number that i have i put accommodator yet again swap it and store it back there we go so that should be fine if i understand correctly right so here we have a and b i do a so then accommodator then i load it then i add it to a right i add it to a then i do accumulator yet again then i swap it and then i store it back and there we go so that should be fine so that allows me to essentially accumulate everything into the memory and then if i do accumulator load 64 print that will print the final sum of these things and we have 44 is it 44 uh right by starting well it doesn't really tell us the sum um right it doesn't really tell us the sum but okay so i suppose now the thing we need to do we just need to replace 100 with four million right so it's gonna be one two three one two three and hopefully this entire thing is gonna produce the result right because we trading over the fibonacci numbers and we just accumulating them into the memory so let's see if it's going to produce the expected well okay four six one thirty seven thirty two uh vortex one thirty seven thirty term a good duke thank you so much for 26 months hello thank you thank you so much for tier 2 holy thank you so much and welcome to epic euler club uh there we go so we implemented this thing without the ability to read and write 64-bit numbers into the memory just think what the are we doing with my life like why um but yeah yeah we have an ability to do that with without uh reading and writing 64-bit numbers because we do that byte by byte uh right i mean we can just implement that in the computer itself but uh i wonder how much time it takes for this simulation simulation is probably completely unbearable uh right so if i just run problem uh problem zero two uh it's still fast you know what's funny this is still fast it's incredibly fast that's how fast the computers are in 2021 it's not even in 2021 because this computer is seven years old right so that's how computers were seven years ago you could do completely nonsense solution that just instead of using like uh like bulk reading from the memory it can read 64 bit numbers by by byte and it's still going to be fast that's how much we screwed up software that you can do this kind of and it's going to be fast um [Music] uh so um let's actually see so if i try to interpret this entire thing i think it's gonna be super slow so let's actually do the simulation so how much time it's still really fast uh well it's 100 milliseconds but yeah [Music] you could write this interpreter and in python and it would be still fast well i mean we have i mean it's already written in python i mean you're probably joking but i mean it's already written in in python and anyway uh so we have a compiler that generates assembly and we have an interpreter that is written in uh in python as well right so and the assembly that the generates is absolutely horrible but yeah i wonder how fast it would be if you interpreted fourth in ports and simulated that i think it would be faster i'm pretty sure it would be faster but i have nothing to back that up until i re-implemented porcelain pores but my prediction my intuitive prediction is going to be that the interpreter of pores in port is going to be faster than interpreter of ports in python but we'll get to that conversation again once we do self-hosting so all right so this is precisely by the way why i wanted to do these euler problems because it lets us know uh what kind of things we're missing in our language so we can clearly see the things that we're missing in our language i already kind of knew that i just wanted to have a confirmation that yes we um we do miss an ability to read big numbers from memory um okay so uh let's actually add that to the test cases so i'm gonna do uh test pi uh f uh euler record right so this is gonna be a record and then if i just run the test cases uh everything seems to be okay right there we go so we can also try to run that in only ci in the future uh so um all right so what's going to be the next problem what's going to be the next problem largest prime factor oh my god oh um i mean this problem is super easy to solve i remember solving this problem like this so yeah so for those who doesn't know like in the chord utils you have a factor utility that takes a number and factorizes it right so every time you need to factorize a number uh you just have this utility i think it's part of the curry tools yeah it's literally part of the core details of gnu uh so but we can implement the factorizer in the um in in ports why not so this one is going to be rather interesting um because we need two indices like two things right uh we need the number and we need the current factor uh both of the things we can try to maintain on the stack uh so the question is do we need the memory in this case i think we do need the memory for the final result right uh we do need the memory for the final result uh so let's go um so this is gonna be problem zero three ports ah problem zero three fourth uh include std porth so what we're gonna be pushing in here uh i'm gonna push this number and i'm gonna push the first factor so the first factor is going to be 2. the question is in which order i want to keep them i think it would be easier to actually keep the current factor as the first one and this one is the second one because then i can do while dupe is greater than one and we're gonna do do okay or maybe not you can actually keep them in other order in that case jupiter's gonna become over right so i think it's a little bit better because then in the body of the loop i can do two dupe right which will essentially uh duplicate this entire thing and then i can do um a mod of this entire stuff right i can just do mode uh yeah it's actually a little bit easier to do it like that uh and then i can compare it to zero right i can compare it to zero to the two and if it is equal um to zero that means we're going to have one thing otherwise we're going to have another thing all right oh by the way it would kind of cool if we had like a to do word uh that basically crashes when it reaches the the execution and it's not that hard to implement you can make it a macro that looks like this and what you do is that you can print something like to do uh right to std error like so and then exit with a non-zero exit code and that will work by the way we do have uh syscalls we have a wrappers for c schools in our language so uh we do have exit you want to see how exit is implemented i can actually show you it's not a built-in word by the way it is not a built-in word it's uh located in the standard library uh and it's implemented like so uh macro exit so it's a cisco uh of c exit like we literally call it as a six code because we have a cisco instruction uh we can pretty much use the whole api of linux whatever you can do in linux you can do import right so this is actually pretty cool uh this is actually pretty cool so you can have a to do and it will be also kind of cool if we put the location in here but um i'm not sure if it's implementable right now so if i try to uh do something like this zero three uh no such uh one more time uh oh yeah we have to do compilation yeah there we go so it just says to do right it just says to do but it doesn't tell you where exactly uh where exactly it uh failed but anyway okay so what do we do i want to do in here uh in here i want to actually um divide this thing by two um if mod is equal to zero all right i'll need to ah well i probably need to swap this entire thing okay and then do over like so and then properly divide this entire thing like so uh right and swap it back and uh yeah that that's pretty much it which brings it to bring brings us to another question maybe i should keep them in a different order but yeah whatever uh so if it's not divisible by this value what we can do is just increment that value by one right so we can just increment that value by one um so and that's pretty much it uh do we need to do anything else um every time we wanna um print this entire thing uh i can do dupe print right uh so and let's take a look at all the factors in here uh okay so it exited abnormally where did i make a mistake okay so this is gonna be something like this over uh greater than one okay that's fine then we do duplicate all this thing then we do mod zero equal all right and if it is true right we duplicate two and print it then uh we swap this entire thing right we solve this entire thing um copy two divide this thing by two and swap it back right and swap it back swap it back if it's not divisible uh we always add one to the index so the question is why this thing wouldn't work or why would it actually you know fail so i probably missed something so and also i think i'm actually getting tired um so oh wait oh well it's a to do well i mean probably probably not [Music] all right so maybe the time has come to make a small break uh because i want to refill my cup of water and also you know yeah to reset my brain a little bit because i've been doing a quite intensive for the past hour so i think we do need a small break so um all right so uh the chat actually told me that i made a oopsy doopsy in here so yet again um i still cannot get used to uh you know prefix notation i think these kind of errors with confusing the order of operation could be easily avoided if we had uh static typing uh similar to was a verification step and at the beginning i didn't really want to implement the static typing before i made the compiler itself hosted but i'm starting to feel that having some simple static typing might be beneficial in the long run while re-implementing the compiler in itself so i think maybe maybe we're gonna implement simple static uh like checking uh before doing self-hosting we'll see we'll see because that may help in the future um all right so let's actually see if it will print the uh the step and there we go so here are all of the prime factors right and as you can see um yeah we can even check that that's the correct prime factors right uh so if you do factor these are the same prime factor uh so and essentially what we need to do in here is just um i suppose uh print the last value right so i might as well just not print anything in here and print at the top and uh yeah there we go so that's the solution we didn't even need any memory access uh so you just print whatever you have on top because on the top we maintain the factor the current factor so yep pretty straightforward so here's another solution it's actually very simple i really like how small and simple it is look at that [Music] so uh let's see uh if we can add this entire stuff to the test cases um so let's actually record so the simulation is kind of slow but that's that's fine i suppose that's fine okay that's cool so the problem number four and i never solved the problem number four oh well i don't remember how i solved these problems as well so it's not that different from solving these ones so let's actually go ahead and see a palindromic number reads the same both ways uh the largest polynomial made from the product of two digit numbers is uh nine zero zero nine um so it's 91.99 uh find the largest potential made from the product of three digit numbers so essentially we just need to organize the nested loop the two-dimensional nested loop and we need to have a way to check whether a particular number is a pauline drum or not so that's what we need to do okay it sounds pretty straightforward i have no idea why i didn't solve this problem before so let's go ahead and give it a try so maybe maybe it should be easy uh okay guy um so euler problem zero four ports include this is fun actually i really like that so uh let's actually try to solve that for two digit numbers um so all of the two digit numbers are from 10 to 99 right so this is what we can do in here so while uh this thing is duplicate uh 100 let's we're gonna do that all right and then we're gonna do one plus uh and then another one uh is going to be while uh duplicate 100 less and one plus and you should not forget to actually drop the counter after the loop and maybe we're gonna actually put the drop here as well uh okay so here uh at the top of the stack we have two indices right uh what we can do in here maybe we can just multiply them together right so here we go we multiply them uh and now we need to check if whether it's this number is a polydrome or not so the question is how we're gonna be checking that uh for now we're gonna just print this entire thing uh and see what's gonna happen uh so let me just go ahead and do this thing and here are all of the numbers right so we're doing like a two-dimensional loop and we're multiplying the indices in a pretty straightforward way okay so the question is how can we check that a particular number that we have on top of the stack is a polandrom well uh we can actually iterate through its digits and uh add them to a different number so essentially you have two operations on a number divide by 10 and mod by 10. if you do mod by 10 you're essentially popping out a digit from the right right so essentially you can uh pop out digits from one number and push them into another number uh and uh then you can compare the results of those numbers right so i can kind of simulate the process right i have a number one two three four five uh right and then i have the second number uh which is zero so i add mod ten of a to b that means i will add five and then i can divide this thing by 10. then i can multiply this thing by 10 which is going to be 50 and then add a mod 10 and 1 to 3 mod 10 is going to be 4 and this one is going to be 54 and then i can divide uh this entire thing by ten one two three again i can uh multiply this thing by ten uh multiply by ten it will become 504 then i can add a mod 10 uh which will be three right which will be three and then i divide it by ten and i can continue this process until this entire thing becomes essentially zero and then um i can compare the final result with the initial result so this is how we can do that right so just like divide mod divide mod and that basically reverses the digits of the number uh does it make sense um all right so this is what we can do in here uh rdb42 thank you so much for seven months of tier three hello hello hello uh thank you so much for seven months of tiers 3 subscription holy thank you thank you thank you and welcome to our epic port club all right so uh let me let me see so but we also need to preserve the original value too so it's going to be kind of kind of difficult to do so all right what i can do is i can duplicate a right i'm duplicating a and i'm also pushing 0. so here i have these two numbers so i need to move uh the digits of that number into that one right somehow we'll see how we can do that uh to do two but maybe i can do something like this where i push first zero and then i do over right like this uh and that will enable me all right [Music] okay so let's actually do dupe so this is going to be dupe zero so this is going to be like this then i do over right this is over a after that i need to push 10 10 and do a mod i can actually use percent why not so i have a percent in here uh and after that i need to add this entire thing to this stuff but maybe i need to back it off a little bit right so let's actually back it off a little bit because the first thing i probably want to do with the second number is to multiply it by 10 right so i want to do 10 uh 10 multiply so this is going to be b multiply by 10 there we go so we multiplied it by 10. then uh i do over which gives me a and then uh i do 10 and then i do mod that gives me a mod 10 there we go and then i can do plus right i'm doing plus and that sums up these two things so we have this stuff after that i can do swap that gives me that and i want to divide a by 10 uh right so it's going to be divided by 10 and then i want to swap this entire thing back right i want to swap this entire thing back like so and i kind of want to keep repeating this entire stuff until a becomes zero uh so here zero i hope i pronounce your nickname correctly i don't know how to pronounce it thank you so much for six months of tier one subscription uh thank you thank you thank you and welcome to our epic oiler club uh which problem uh it's a fourth one so this is the fourth problem largest palindrome product uh okay so and yet again we need to keep repeating this entire thing um i need to keep repeating until a becomes essentially zero right so one thing i can do i can do over [Music] so this is gonna be like this while uh over uh greater than zero uh zero greater we do this stuff uh we do this stuff and then this is gonna be the end and at the end we have uh essentially zero and final b um so and i wanna swap this entire stuff so it's gonna be b a b zero and then i'm gonna drop the last zero so and if the number is a polynomial a and b have to be equal right so uh that means if this is uh well i probably have to do something like equal if uh if they're equal uh that means i need to print that value we can just go ahead and uh i suppose print it [Music] though might as well do something like two dupe right uh to dupe and then compare this entire stuff and then dupe print and uh and then we can do to drop uh hopefully i didn't make too much miss the too many mistakes uh okay so this is the procedure that checks whether a particular number is a polynomial or not it's kind of involved procedure but um it is what it is and it isn't what it isn't uh we can go over it uh yet again why not um so it's actually pretty small so i think shouldn't do that that hard so we have after this to do we have a uh i duplicate a and i push zero so we're gonna say that zero is a b right then i copy a over and then zero and i check that while a is greater than zero we keep repeating this entire thing then i do 10 and multiply b by 10 then i copy a over and i do 10 uh mod so this is gonna be mod uh 10 and then then i sum them up right so i sum them up then i swap this stuff around all right then divide everything by 10 and swap everything uh back then i repeat this entire thing so as you can see i just keep dividing and adding dividing and adding and uh at the end of the day we're gonna have here a zero and then b as a final result we're gonna swap this thing uh around and then drop it and then in here we're gonna do two dupe uh which we're gonna compare with each other right which we're gonna compare with each other and if they're equal that means they are um palindromes then i duplicate b and a print b and at the end here i just do two drop uh which is totally fine okay so this is a valid procedure so this entire thing is going to essentially uh print all of the pollen drums right so it's going to print all of the polymer drums let's see how it's going to perform hopefully uh so there we go okay so look at that it actually yeah these are polling rooms yeah uh but what's the largest one what's the largest one um interestingly enough what if we are iterating this entire stuff starting not from 10 to 100 but actually from 100 to uh 10 and if we find the well it may not work actually yeah it might not work so it we need some sort of a way to have an accommodator um [Music] all right so uh yeah let's have an accommodator um macro uh and this one is gonna be answer and we're gonna put it into the memory there we go uh so if uh this thing is what we think it is right so after the dupe after the duke we have the value that we need to store into the answer this one is rather interesting [Music] so this is a and b we're not going to duplicate anything yet duplicate b then i put the pointer to the answer right i put the pointer to the answer and then i load 64 the reference in the answer and if answer is less than b uh actually something like this if b is greater than answer right something like this uh we have to store b in there okay uh we have to store it in there uh right uh alright so essentially i'm gonna put answer uh that is gonna be like this and then i need to swap this entire thing right i'm swapping it um now uh maybe i need to i need to duplicate it so let's do dupe it's gonna be b [Music] answer oh well i mean you can do dupe answer or answer over is gonna end up with the same anyway right if you do uh answer [Music] answer you're gonna do like this and then you do over you're gonna have like this and then you can just store 64 and that will just store it in the answer uh ants for all restore 64 is actually looks pretty cool i really like how it looks like all right and then after that you do two drop okay so uh that's pretty cool and then we can do answer load 64 and just bring this into it right so we're trying to find the biggest uh palindrome we're trying to find the biggest palindrome uh okay girl so let me see uh okay so we found it nine zero zero nine there we go it is nine zero zero nine but we have to do that for three digit numbers now right so it works fine for two digit numbers and it actually finds what we wanna find but now we need to do that for three digit numbers are you guys ready are you guys ready so now we need to iterate from 100 to uh 1000 right so because this is gonna be all of the three digit numbers there we go [Music] okay it is nine zero six six zero nine it is pauline drum but it's is it the biggest one let's find out i mean we can google up the answers online um so probably cac w indeed uh kw indeed okay so um is offline for maintenance um oh it's complete sake okay so uh i know a pretty cool thing uh project euler uh euler offline so uh some time ago actually found a pretty cool uh repon uh essentially it's all of the project order problems uh in a txt file but you know what's cool they also contain answers they also contain answers but they contain answers hashed with md5 well it's actually a pretty bad idea to hash it with md5 but it's just for your own uh discipline right so essentially you have to hash uh your answer with md5 and compare it with this one but i'm pretty sure um um [Music] yeah so let's actually see if i take this entire thing uh right and eco minus n because i'm pretty sure they're hashed without the uh new line uh nine zero six six zero nine uh and it's gonna be md5 uh right so this is the hash and uh there we go so this is the problem for this is the the correct answer right um this is the correct answer so maybe we're gonna use this instead of the project or the website because this thing is reliable furthermore i can just download this entire thing on my machine uh all right and uh i won't care anymore whether the project order is alpha online or not right so as long as my computer is online uh it is going to work right so just gonna have it locally and here we go so that's pretty convenient so i'm gonna give the link to this repo in the chat for whoever is interested and i also gonna put it in the description for people who's watching on youtube uh project uh euler offline so it's actually a pretty cool idea right just have like a file um right and i even started so i think i think it's pretty cool i think it's pretty cool but here is an interesting question can i actually brute force this uh you know hash um mg5 terrain [Music] right so uh crack station all right so i just wanna double check that none of these motherflippers lick my ip right so i'm gonna just take that md5 uh write another robot uh check hashes uh all right okay i hope nothing actually looks might be just a second um all right so yeah uh you essentially can use the things like crack station or something like that to actually crack the answers and as you can see uh it's so using md5 was actually kind of bad idea um so uh yeah um it is what it is but i mean it's like if you wanna ruin yourself the uh the pleasure of discovering the solutions you just go ahead and crack it instead but it's just like uh it is what it is and it isn't for the decent all right so uh let's go let's see if the solution that we wrote even fast enough for simulation i'm just i'm just curious of the simulation okay so this is how much time it takes for the simulation to solve this problem [Music] so you know i'm starting to think that simulation a mode in port is kind of useless so maybe i should remove it so first of all it's not even that fast to be useful in the first place and second of all uh if i remove it i'll have to implement less when we're gonna go self-hosted you know what i'm talking about right so yeah so i'm actually like genuinely thinking to get rid of the uh of the simulation mode um so this is straight up useless like i mean how much time we should take but maybe if we implemented the load 64 in store 64 intrinsic uh maybe it will be yeah i think it was useful in early stages yeah it is true it is true it was useful in early stages but now the the more we implement different features low level features the more it becomes kind of useless and it doesn't even support all of the c schools it only supports like a right cisco um and exit cisco i think yeah i think at some point i'm to just illustrate the premium simulation mode and i'm going to leave the compilation on yeah so it is what it is and it isn't what it isn't okay so maybe we should solve another problem you guys think uh problem five um oh boy 25 20 is the smallest number that can be divided by each of the numbers from 100 for 1 to 10 without any remainder with the smallest positive number uh what is evenly divisible by all of the numbers from 1 to 20. so i think i do understand how we solve this one so we have to maintain the uh the table yeah we have to maintain the table of divisors all right [Music] so if we get a number right so let's actually go ahead and try to solve something [Music] lcm well here's an interesting thing um it might not be as easy as lcm uh but maybe it could be implemented with lcm i remember implementing it slightly differently i remember implementing a slightly different one but we'll see [Music] force include std ports so let's actually keep track of the current n so the solution for n equal 10 is 25 20 and we have to find the solution for n equal to when so but for now i'm going to keep the n equal to 10 right so uh what i want to end up with is the solution where i can just change uh the end to something else and it's going to be a new complete solution uh so now we need to iterate uh so i'm going to duplicate 10 and actually n uh less or equal right duplicate print one plus and there we go and let's do a compilation five and do we have stuff in here looks like we do um so interesting enough i don't think we need actually one right because everything is always divisible by one so it's kind of useless to have so uh the next thing i remember doing is i was trying to factorize uh listen to stuff right so each individual number i would try to factorize um and that is actually a pretty cool idea and as a factorize i uh store the values into the table [Music] all right so uh we already did factorization in the previous problem i suppose was it for fourth one no it was it's actually third one uh yeah so here i'm doing something like this duplicate and i'm starting factorization right while uh over one so i'm still in factorization algorithm from the previous problem so that's what we really do uh and this is gonna be the end uh and in here i essentially can do two dupe mode 0 equal swap over swap otherwise uh one plus and there we go so this is this is that stuff so um [Music] [Music] i think i'm already tired but i want to finish this problem i still want to finish this problem so duplicate over one and then i do the division [Music] so this is the x and uh the factor [Music] and i just keep using it um and essentially what i want you to do is to duplicate the f right so i'm duplicating the f uh then i'm going to print something like this write std out right and then print right uh and also here i'm gonna do print right so what i wanna do i wanna just see number and then factorization indented a little bit right so this is what i'm trying to achieve from here uh let's see if it's going to work and it didn't work because i forgot something i wonder what exactly i forgot i forgot to drop whatever we have in here i suppose so i forgot to do two drop let's see uh there we go so that's kind of dumb uh all right [Music] dupe print uh dupe two uh over one uh mod zero [Music] so what i wanted to have on here is just the number and its factorization but it never actually kind of worked uh which is kind of strange right so let me go over this code yet again uh so we have an index right so i'm duplicating uh this thing and then i push two all right then i push two and the reason why i wanna duplicate is because i wanna preserve that index in the future right so that means i can just simply ignore this entire step uh index uh 1 and it's greater than 1 right it is greater than 1 so it should be fine then i duplicate these two things then i do mod and then i compare this mod to zero right so that's fine then i duplicate uh two writer duplicate two then i print the space right there in the space and i print this entire thing then i swap this thing around uh copy two divide index by two and i swap this entire thing back okay that makes sense uh if this entire staff was not uh equaled well it's not divisible right i just do one plus and i just continue uh and after that um well yeah i think uh i don't see any mistake in here that's the problem uh i didn't see any mistake in here uh but it still doesn't really work it still doesn't really work so if i just do something like this it is fine um i don't know man okay so if i take a look at this stuff this looks fine as well so here we have uh something like this maybe i have to do just a single drop uh so where did they make a mistake why does it do it like that i literally don't see a mistake where is the mistake um yeah so it just stops in here oh yeah i know what's the mistake yeah i know what's the mistake i completely forgot i definitely need to implement the static type checking i definitely need to implement the static type shaking right returns the amount of characters that were written that's all it does all right so and since we're printing two characters in here it prints two so after write we have to do drop we have to drop the result of right and uh now it is printing uh everything so two is two three three four is two two five is five five to three and so on and so forth you see [Music] so finally finally i have uh have this thing all right so uh and we need to have like a table in which we're going to store um the the factors themselves right so and the question is how we're gonna allocate the memory for the how we're gonna lay out the memory uh so i suppose the answer is gonna be located at the beginning of the memory right so this is gonna be the answer uh memory end right and the intermediate one is going to be after the answer and what's the size of the answer well uh each element of the answer is going to be eight bytes because we can only uh read eight bytes uh and multiplied by n so this is gonna be n multiply eight multiplied by n uh and uh plus the memory right so with macros it is actually relatively easy to lay out the memory to actually define the schema of the memory so i say here is where the answer starts uh this is where the temporary uh table starts and so on and so forth so it's actually kind of kind of useful so and probably before we um before we start doing anything i want to fill temporary folder at the table not folder with zeros right so i wanna actually fill it with zeros the question is how am i gonna be doing all that uh so zero while um duplicate and let's do and we have an index we duplicate the index we do the temporary thing and we add them together uh and that gives us the um the point uh right the point we need to store the zero into the pointer so this is gonna be zero over uh and i'm gonna do store uh right store 64 then one plus and then the end so here we clean up uh clean up the uh tmp tmp table right so this is uh clean up the tmp tape uh one more time zero um zero n okay uh zero over um uh zero copy zero tmp plus that [Music] the tmp i actually have to go yeah i i think i it up uh all right so here is an index right so here is an index and i suppose i want to do tmp that will give me tmp uh then i do over uh index uh plus then over again no not over again then zero zero and then store uh 64 and there we go okay so this is how we're going to clean up the tmp table there we go so i think i think this is how it's going to be done uh cool and now um the thing is uh every time we encounter the divisor right every time we encounter the user we want to use it as an index within the table and we're gonna we want to increment the um the element within the table which is a rather interesting can we have a macro can we have a macro increment 64 right so it will accept the pointer and it will increment the value in inside of that point so i think it's actually a pretty cool idea so essentially uh i want to duplicate whatever we have in this pointer right and we're gonna uh load 64 right then i'm gonna do one plus that adds uh plus one to this entire value and then i'm gonna do store uh 64. and this is the entire macro right uh so that way i can do like tmp index plus increment 64 uh without worrying of loading storing and stuff like that so this macro system is actually very powerful look at that look at that yeah so first we introduced the concept of load 64 and store 64. and over that concept we introduced inc 64 and all of that without even modifying the compiler here's the interesting thing i'm streaming for almost two hours and i'm solving fifth problem and i haven't modified the compiler so far so we haven't written a single python line of python code today so that's what's cool about this entire stuff we just think i think that's pretty cool actually i didn't expect like i was expecting to imp uh like to dive into the compiler itself a lot while doing all of that but i just don't have to even though things like load 64 and store 64 are kind of lame i do agree with that and we can improve them later but i didn't have to go into the source code of the compiler so yeah so the macro system is actually super simple but it's super powerful um so yeah anyway uh so what i wanna have in here so at the top here we have uh a factor right and then what it can do with the factor is essentially use it as an index so it's going to be tmp plus right increment 64. and that's it believe it or not um so that's pretty cool and after that maybe i want to try to print uh the table i think it could be a very good idea to try to print the table uh let's go ahead and do that oh by the way uh that's fine actually yeah that's fine um so i'm gonna copy paste this entire stuff um back to ryzen right so factorize uh print the table print the table so duplicate and uh tmp over uh so here we have this thing tmp over plus and in here i might actually try to do something like uh load 64 right just slow 64 and just bring this value after the factorization of our nation um but before i can do that maybe i'm gonna actually do something like this uh space uh and then right right spacing right and also drop and in here dupe drop deep print so that's pretty pog right that's pretty poke so on each iteration right on each iteration we clean up the table we factorize the value and we store everything within the table and then we print the table so all of that is not going to be part of the solution but it's going to be just like to debug the solution uh right so if i try to run and it failed miserably uh which is kind of interesting so it didn't even print anything um reasonable at all uh okay so what if i uh get rid of this entire thing uh what if i could really listen to everything so we have it it doesn't print anything okay that's fine um all right so oh yeah i see uh std out i should have actually done this study out there we go okay that's kind of cool but this is lame hmm [Music] oh i forgot to drop and yeah [Music] so factorize everything duplicate print then i start from zero uh std out write and drop then tmp and over then plus then low this value and then print it and then increment it yet again so why does it print it like that this is so weird um so it's almost like yeah it prints complete nonsense if you know what i'm talking about why does it print complain is it because increment 64 is incorrect uh one more time uh all right so if i have the tmp right i duplicate tmp i load tmp i do plus one and i store this entire thing back right i store this entire thing back so this is kind of strange um okay yeah so it only performs like a single iteration in here this is the problem it performs a single iteration like what the uh and i don't understand why why did it perform on a single iteration it's like uh i'm really confused is that because of the two drop in here but two drop in here is important so all right so probably factorize actually something over um oh i forgot the drop in here as well okay so i i see i'm really glad that i'm using this language because i'm starting to understand what's needed to to make this language more usable uh okay so look at that uh there's something completely incorrect with how we read and write uh you know values in here there's something completely wrong and you know what exactly uh when we add the index in here we actually have to have set by eight right so we have to do a to multiply right so we're basically overriding the cells of the table so uh yeah it's okay so this is kind of uh it's kind of interesting [Music] and okay so this is a duplicate eight uh oh and it has to be also the same yeah i have to multiply by 8 everywhere right i have to multiply by 8 everywhere so this is over but also eight multiply right so eight multiply uh and also uh eight uh multiply there we go so now finally holy uh okay so these kind of things uh we don't have any static typing in here right so that kind of creates the problem uh but each of this thing corresponds to the amount of factors of the corresponding index right uh that's what we have in here oh boy okay [Music] so and right i'm kind of thinking um i want to be able to print arbitrary table not only tmp table maybe i could to like take this entire thing and put it into macro as well if you know what i'm talking about like print table uh so something like tmp print table and then at any point i could say answer print table um right so we can do something like that i think it's actually pretty cool idea uh just put this entire stuff behind the macro print table and we know that the table has a size n so we never have to worry about that uh so this is going to be 10p okay uh dupe n uh so this is tmp um this one may require actually changing stuff slightly uh right because i do zero uh right right right so then when i duplicate i actually have to do uh dupe zero and then we check everything okay that's fine then we do that um and here we have team p all right so here i can do two dupe all right so this is going to be 2d then i need to multiply it by eight right so this is gonna be multiplied by eight then i sum it up right so that will give me that uh right then i load oh it's not gonna work it's not going to work it's not going to work [Music] all right um maybe it maybe we don't need this macro but it will be kind of cool to have this microphone but i'll have to rewrite this into i think to accommodate for that macro uh the thing that makes this code easy to write is because i can don't have to store this pointer on the stack once i start storing this pointer on the stack it becomes kind of difficult to work with so that's why i don't think this macro is feasible so i'm going to just remove it like this okay so uh we are collecting uh we're collecting the table and now uh what i need to do is i'm gonna i need to add that table to the answer table [Music] so i need to iterate ants for ntmp table uh row by row like element by element right and uh then uh store in the into the answer the maximum between them right that's what i need to do so let's go ahead and do that uh so instead of printing the table all right i'm gonna be just iterating through the entire table and ants were so this is the index i'm duplicating the index duplicating the index and multiplying it by eight multiplying it by eight and adding to the answer right and loading the value so this is the value stored uh within the answer then i do over right uh so this is the index uh this is the answer right or rather answer index like then i do over multiply by eight multiply by eight tmp plus p plus load 64. okay so this is what we have on the stack and if um tmp is greater than the answer if it's greater than the answer we have to store the value of tmp in there right we have to store it in there so what we need to do i need to push i need to duplicate so here is the address here's the address [Music] here's the address and then i'm copying this entire thing over yet again uh i'm loading 64. but after that i'm storing this back right i'm storing this into anything back uh and that's it i think i think this is how we're going to be doing that right yes so we are merging the tmp table back into the answer table um yeah and after that we can try to print the answer table just to see how it goes uh right we definitely need like a way to to work with this kind of stuff in a more reliable manner uh okay so after the entire solution i just want to print the uh the table right i have a really weird reflex in here uh oh by the way i forgot to do a oneplus one plus drop one plus uh so answer [Music] over so this is index answer index multiplied by eight i multiply by eight and add uh-huh so and then i could can do load 64 and get the value out of here right i'm getting really out of here so then i do space uh write drop actually std out i have to do that on std out and then i can do print right i want to kind of sort of indent it to indicate that i'm working with the with the table uh okay so and there we go so this is the final uh table and what we have to do with the final table this is a very interesting question what we have to do with the final table we have to uh iterate over it and essentially multiply all of these um all of these factors uh we have to multiply all of these factors so let's actually quickly do that so we're already doing all of this all right but we have to check if this thing is uh greater than zero right so if it's greater than zero only then we have to add it to the final result um [Music] so okay so we have a tmp and answer tables but we need to put like this stuff into account later right so we need to have some sort of accommodator uh which will hold the answer so we already have the word answer so maybe i should rename answer to accumulator answer to accommodator uh let's actually do it like that so this is gonna be a kind of later and the answer i suppose right is going to be uh tmp8n plus right so it's going to be something after the tmp so first we have um the accommodator table right so this is the cumulator then tmp table uh all right and then the answer number right so this is basically the memory layout yeah so this is the accumulator team p and the answer that makes sense so with the macros we can just sort of like divide the memory however we want and then use these values um uh all right so since it's gonna be like factors and whatnot i think we'll have to initialize this entire stuff so this is the answer and that store uh store um store 64. right so this is store 64. [Music] so we go into the accommodator right and go into the accumulator we check that it's greater than zero and essentially what we need to do right so we have an index all right here's the index we need to oh boy since those things are factors right since those things are factors we have to uh repeatedly multiply this stuff by them all right so uh this is what we have to do okay so this is the index and uh so if essentially essentially this is the factor right this is the factor and this is the times right so i take accumulator over eight blah blah blah and load 64 and this is the times and uh maybe what i want to do in here is while um duplicate times is uh greater than zero we're gonna keep doing this entire stuff and every time we're gonna subtract one from the times right so and essentially what we have to do we have to multiply the answer by the factor so i have to do over that gives me the factor then i take the answer right okay here is the answer and i load the answer uh let's see like this uh then i suppose i want to multiply this kind of stuff so this is what i have now uh and uh then i suppose i want to put the answer yet again in here and i need to swap it so to put answer here and then i need to store uh this entire stuff back uh into the memory and end up with times and then i'm gonna do one minus one and it's gonna be uh repeated until uh the entire thing is uh zero and after that i can just drop these times and i'm gonna end up with a factor which is uh kept incremented um actually uh yeah it's not gonna be a factor per se it has to be two dupe i think is it gonna be two do i'm not sure but the index is the factor yeah okay so that makes sense all right so that has to be the thing and after that i can just take the answer i can load 64 and i can print this entire thing hopefully that's the solution theoretically theoretically that's the solution [Music] so yeah i checked that this thing works correctly so the only thing that may work incorrectly is this one hopefully it will work correctly uh so let's go ahead and just do that and 25 20 right so the final result is 25 20. wait a second i think this is correct yes holy it works yes it works for one from one to two so the only thing that is left right now to do is to switch n from 10 to 20. and we're done pretty much so yep let's go ahead and do that that's kind of interesting uh all right so this is the whatever it is so let's do eco uh md5 sum and okay so maybe i want to do that slightly different plane so i can copy paste this entire hash and uh wait wait wait did they it up yes i have to do minus n right so because it has to be without the new line here maybe there is a simplest solution for this problem people say something about lcm uh but to be fair i don't really see how you can do this kind of stuff with lcm i think you kind of need this kind of table uh but i don't know so that's pretty cool holy uh i only wish the compiler would do a little bit more checks on on the operations right um so things like inc 64 could be also part of the stand library so i think i'm gonna actually put it into the standard library because it's kind of useful uh so yes yes yes kawaii freaking desu how about that we solved five uh oil problems today's uh and yep uh i think that's cool i think that's pretty cool [Music] so i guess i'm gonna call the day for today all right so i think five uh order promise was enough and i know what's needed to be implemented for this language uh so i gathered enough information so um yeah i think it was interesting all right thanks everyone for watching uh i really appreciate it thank you for all of the subscriptions for all of the beats and stuff like that and i see you all next time if you're watching on youtube check out the description for all of the links if you're watching on uh on twitch check out the vots channel and yeah i gotta go thanks everyone love you all
Info
Channel: Tsoding Daily
Views: 11,957
Rating: 4.8958335 out of 5
Keywords: programming, coding, problem solving, recreational, algorithms, data structures, computer science, forth, stack-oriented, stack-based, stack, project euler, hackerrank, leetcode, coding interview
Id: GV1yjYnr8_o
Channel Id: undefined
Length: 124min 30sec (7470 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.