How to "feel" recursion?

0 like 0 dislike
8 views
Hello. I learn by myself programming, but I've noticed a huge problem with the tasks recursion is slightly heavier than the base.

For example the second section with the objectives of the recursion on this website:
codingbat.com/java/Recursion-2

The entire section is based on one method(codingbat.com/prob/p145416 — show solution), the perception of which is my problem. I knew it — drew the principle of its operation, but still difficult, I have no sense of perception, logic; and say if you slightly change the condition(the next task), I can't figure out what needs to change in order to apply this method...

I decided to save this for later, and take care of lighter tasks, one of which I invented myself while watching this video(not the formula):
https://youtu.be/UfEiJJGv4CE?t=168

Objective: given a natural number N. Find the number of integers in the interval [0-N] containing a figure "3" in the loops by using recursion.

And I solved it... but my decision not like it. I am passing two parameters - Number and Сurrent which is exactly the same and are just buffers for the values used in the code part which looks in the current number "3" and part of the code which walks backward from N to 0.

public class ThreeCounter { public static int ThreeCounter(int number, int current) { //Base case -- the algorithm went through all the numbers if(current == 0) { return 0; } //Base case? the second recursion -- algorithm went through all the numbers, the current number of if (number == 0) { ThreeCounter return(current-1, current - 1); } if(number % 10 == 3) { return 1 + ThreeCounter(current-1, current - 1); } ThreeCounter return(number/10, current); } public static void main(String[] args) { int n = 100; System.out.println(ThreeCounter(n, n)); } }


How to make the code "more correct" because I don't think it would be reasonable to write: "Solkatronic(taraseviciene, tozhesamoe number)"? What would you suggest in order to understand the above algorithm? There and so all is explained at the elementary level... to Beat my head until I understand?
by | 8 views

3 Answers

0 like 0 dislike
How to make the code "more correct" because I don't think it would be reasonable to write: "Solkatronic(taraseviciene, tozhesamoe number)"?


private static int ThreeCounterImpl(int number, int current) { ... } public static int ThreeCounter(int number) { return ThreeCounterImpl(number, number); }
by
0 like 0 dislike
I have exactly the takayazhe problem was the time, also did not understand until the end, in the end, the concept of fully come when did simple recursive method snooping around the files.
Example algorithm:
funkcijos( url ){ if (url.file()) then *do need for example look at the extension or name* otherwise цикл_перебора_содержимого_каталога call funkcijos( url )

I kicked with such problems in recursion.

Even once, for static methods slap on the wrist and sometimes on the head. Better learn to avoid them and use only for completed constant.
by
0 like 0 dislike
Try to write programs with a more intuitive result, it is advisable - graphic. The bypass of the tree structure, the generation tree, generation of Peano curve, Koch snowflake, triangles Sabinskogo. Play with the settings. It is very simple algorithms, in which in addition to recursion no nothing.
by

Related questions

0 like 0 dislike
1 answer
asked Jun 11, 2019 by RihardXXX
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Jun 13, 2019 by Ka4_Piton
110,608 questions
257,186 answers
0 comments
33,850 users