Coding ASMR | solving recursion problems

preview_player
Показать описание
This is an ASMR styled coding video, where I code various things from problems to simple applications while giving you tingles.

In this video I do more recursion exercises. In the last video I have made my first recursive methods and in this video I have kind of gotten a grasp on this topic. I do two out of the three problems: one involved taking the sum from 1 to n, and another one to check if a number is prime or not, only using recursion instead of a loop.

While I managed to get the idea of calling functions recursively I am still new to it, and I feel there are better ways to solving it, like the better solution I came up with outside this video.

If you have gotten your tingles, a like would be appreciated!

//better way to recursively call it
public static int sumToN(int n) {

//condition should be if n is strictly greater than 0, cant use angle brackets
if (n > 0) {
//return the number added with the result of calling the function itself, which recursively calls itself until n is 0
return n + sumToN(n - 1);
}

return n;

}
Рекомендации по теме