 What's up guys, I'm Michael and welcome to my YouTube channel today. We're gonna do another leech coach the next day of leech code 30-day leech code challenge So we're gonna do happy numbers So a number is happy if it's defined by the following process start with any positive integer replace the number by resumming up the squares of its digits Repeat the process until the number equals one where it would stay or would loop endlessly in a cycle Which does not include one deal those numbers in which this process ends in one our happy numbers Okay, well if you think about this this problem you could actually just You could just well first of all we need a function to add a sum of all the digits and square them to each other So I'm gonna create a function for that. So first I'm going to add up all the digits before For all this stuff, so I'm gonna do funk Add digit add digits, right? Yeah, right there. I'm gonna pass in an end And what am I gonna do? I'm gonna first get every single digit and I'm going to sum it up So I'm gonna need to return some so this is gonna be the sum of them returning and then what am I gonna do? I'm gonna do a while and is greater than zero. I'm going to divide Every single time I'm gonna divide by 10. So It over 10. Okay, so I'm gonna divide by 10 and then I'm going to Get each digit right so I'm gonna create this Variable called digit is gonna equal to n mod 10 Which is gonna get every single digit and then this this number n equals n over 10 That's going to divide get the remaining every single remaining digit. Okay, so what am I gonna do? I have to sum up every single digit. So once I get the digit, I'm gonna do sum plus equal to digit times digit Okay That's what I'm gonna do Ideally this should work But you should sum up all the digits. I haven't done this problem yet before so I'm just going to Hopefully this works. Let's let's let's just see out Digit Let's let's try it. Let's let's see what this does for I don't know eight for 19. Let's see what it should give out 82 Okay, we got a test your code remembered always test your code You run Fails you submit too soon. Oh, what? Okay, that's some BS I think this should 19 should give out compile error control. It's error. Well, whoops, okay, I'll just return the false Just to make it shut up Okay, yeah 82, okay, so 19 gives out 82 so that means that this function does add up all the digits So what does my function do? I'm going to basically add up all the digits for that Square every single digit and add up the sum of every single sum of every square of every digit. So that's what this function does Basically I have a sum of zero while n is greater than zero. I'm going to get every single digit So by modding by 10 when I mod by 10 I get every single One's place of the digit and I'm going to sum up the digits the each individual digit and then n over n Over 10 that's going to get the remaining. So if when I have like in this case 19, right? I'm going to get the digit 9 Right and then after that I'm going to sum up the sum of the squares of 9. So that's gonna be 9 square So some is gonna plus equal to 9 square, right and at the end of this 19 is going to become 1 because I divide by 10 I got rid of this 9 here the remaining remaining digits So it's going to be 1 and I'm going to sum up 1 right sum up 1 with the my sum of 9 square which is going to be 82 so I'm going to return 82 here. Okay, so that actually does work So now let's go back to the problem. So now We're gonna keep replacing the number by the sum of its square and repeat the process until the number equals 1 where it Will stay or we will loop endlessly in a cycle which does not include one Numbers which this process ends in one are happy numbers Technically you could just you could just loop endlessly until like Until you could just keep iterating just keep calling this function technically. I think you could do that I'm pretty sure you can just do that So what I'm going to do is I'm going to do like a while. What's my bad? I'm doing a while infinite while loop And what's sorry what am I gonna do is I'm going to repeatedly call this function add digit and pass in N and then I'm gonna call the Okay, I'm gonna create a separate Separate variable to keep track of my current iteration right the current end. So this is gonna be like Each Some digit. Okay, that that's what I'm gonna do and I'm basically gonna keep calling this each sum digit It's going to equal to add digit and I'm going to pass in Actually Yeah, yeah, okay, so I'm gonna say each sum digit is going to equal and in the beginning and I'm gonna pass in each Some digit in this function every single time. What's this going to do is it's going to repeatedly call this Function over and over again until Pretty much I'm going to repeatedly call this function over and over again And what do I have to do well the process of I'm gonna keep replacing the sum of the squares of its digits and repeat the Process until the number equals to one where we'll stay or we'll loop endlessly in a cycle Which does not include one those which this process and our happy numbers. Okay, so I'm gonna keep doing this until If some did each some digit is equal to one that I'm going to Break I'm gonna break. Okay. I'm gonna break and then return true Otherwise, I should I should actually keep track of the ones that are already I already counted, right? Because if I if I already counted it twice, then that means that like if it replaces go So let's say let's say I let's say in this case You're right 19, right? One square plus nine square is equal to 82, right? So if I let's say I just keep calling this function over and over again, right? Let's say I get back at 82 After a certain number of iterations then I know that It's and it's looping endlessly in a cycle. Okay, so what I could do is I could have a set That could keep track of the ones that are already I already seen before so Let me I'm trying to think how best to do this because I could if you use a set or I could use a Could use a map. I'm gonna use a map actually. Maybe I'll use a map Okay, so I'm gonna keep a map Scene I'm gonna call map scene Now do scene at each some digit Plus Okay, so basically that this means that I already seen it already So if it's not equal to one, I'm gonna say I already seen it already and then keep doing this over and over again Then I'm going to do Let's see if Seen at each some digit is Not equal to one Wait, yeah, if I already seen it twice so if I already seen this twice then that means that I Probably Return false so over here I'm gonna above it. I'm gonna do if seen at each some digit Is greater than two Greater than one actually if it's greater than one I'm gonna return false. Okay. I think this is what it should work. I'm not sure actually. I'm I don't know I'm not a hundred percent sure Hopefully it works. I haven't done this problem before a compiler. What's wrong? Expected expression. What does that mean? I? Thought you could do this well Okay, whatever. Okay. I guess I'll just do Well true I Was taught that you could do that Okay, well I got accepted so I guess I'll be right so okay, so I'll explain the algorithm again so pretty much They pretty much gave you the algorithm Already so what we did was we pretty much We had to sum up the squares of every single digit and that's what this function did in the beginning All right add digit so we did that then what we had to do It was basically we had to check if it ends loops endlessly in a cycle right and That's what we do here. So what we're gonna do is we're gonna repeatedly call this function over and over again and We're gonna repeatedly call this function over and over again and if if the number finally equals to one we're gonna return true and That means that I already have that it is equal to one right otherwise. I'm going to add the number of occurrences in my map hash map or yeah, hash map I'm gonna say that I've seen this number already seen My hash map has seen I'm going to add the number of occurrences of this number So each sum digit has like a number. So that means like it let's say in the top case, right? I Sum up the digits of one one and Yeah, 19. Let's say I sum up digits 19 right and it equals the 82, right? So I'm 82 doesn't equal to one right like it doesn't equal to one So what my code does is I'm going to add the number of occurrences that I've seen of 82 already So this would be I've seen 82 plus one. So originally it's zero then it's plus one. So then my number of occurrences of 82 is now one and if then I'm gonna keep calling this while loop over and over again and If I already seen the number 82 like I repeated and listening a cycle already, I'm gonna return false and say that okay I can't do it This is not a happy number. So yeah, that's that's pretty much all you have to do Yeah, I don't know I don't That's pretty much the algorithm. I hope you guys enjoy this video. I actually didn't solve this question before but Yeah, I just I just solved it in this video, but yeah, this is pretty cool. Yeah, but Ray coms subscribe I hope you guys enjoy this video. It's pretty nice, but yeah, it wasn't really the tutorial. I guess I It was kind of tutorial, but like I didn't really have like a prepared solution beforehand. Yeah, Ray coms subscribe I hope you guys enjoy this video keep solving problems, and then I'll check you guys later