Any good recursive tutorials? Python? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Wondering if anyone could point me towards a good recursion tutorial. I am a bit rusty on it as I learned about it in my Data Structures class first semester. Would like to brush up on my recursion...any help?

Consider this.
More seriously…
Recursion is a way of solving problems that have a clearly defined base case (or cases, btu I'm keeping it simple here.)
For examples, the commonly cited factorial problem is a great one.
What does factorial do? Let's see some examples:
factorial(0) = 1
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
factorial(4) = 24
The factorial of a number is that number multiplied by the factorial of the number that comes before it, unless (now, this is the base case) the number is 0. The factorial of 0 is 1. (You can't take the factorial of a negative number; only positive integers.)
So we have our clearly defined base case. And we know what to do with numbers that aren't our base case (we multiply them times the factorial of the number one less than it.) We're ready to write our function.
def factorial(x):
if x == 0: # this is our base case
return 1 # and this is what we do when we see it
else: # this is what we do with all other numbers
return x * factorial(x-1)
So you
Clearly define your base case.
Find a way to reduce your problem from a non-base case to the base case.
Formally express that in a function that (when it's simple!) looks like
function:
if base case:
this
else:
something + function(something closer to the base case)
If you want something more advanced, Google's got a lot of info.

I would highly recommend watching MIT's intro to programming course.
Lecture 4 talks about recursion.

Related

How to avoid stack overflow in infintie recursion? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to simulate functional programming in c++ , I'm stucking on "wait" function , assume I want to wait for 100 seconds without using any kind of loops , just recursion . How can I avoid Stack Overflow ?
Make the calls tail-recursive and hope for the compiler to reuse the stack frames. Although I don't think C++ compilers are required to perform this optimization, so all you can do is to rely on implementation.
But why do this if you can simply this_thread::sleep_for()?
I think the real question should be:
How do functional programming languages like Scheme and Haskell use recursion to achieve looping without causing a stack overflow?
And the answer is: they use a trick called tail-recursion to turn the recursive call into a goto. So you'll have to either find a C++ compiler that implements tail-recursion, or simulate it in your code.
Here's an example to give you an idea of how tail-recursion works:
countdown x = if x == 0
then 0
else countdown (x - 1)
countdown 1000000
Notice that, in the recursive step, it just calls the function with different arguments, and then returns its value. So the compiler "cheats" by converting it into code that works like this:
int countdown(int x) {
start:
if (x == 0) return 0;
x = x - 1;
goto start;
}
By the way, if you have to write your code to take advantage of tail-recursion. It doesn't just automatically work. More information here: What is tail recursion?
using a branching recursion. think about a grossly non-optimal recursive fibonacci program. They have exponential running time vs required stack depth.

time complexity of loops for programming contests problems [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question
Which has greater time complexity, a for loop or a while loop?
Can anyone give me a chart to compare the time complexity of different loops?
If possible suggest good references to learn about time complexity.
Barring a few technicalities, a while loop and a for loop are isomorphic:
for (a; b; c) {
body;
}
can be turned into:
a;
while (b) {
body;
c;
}
As such, there's no difference in computational complexity between the two.
As to choosing between the two: the usual rule of thumb is to use a for loop if at least two of the three clauses are meaningful for that loop.
If you are doing the exact same thing with them, they should be pretty much exactly the same. For instance:
int i = 0;
while (i < 10)
i++;
and
for (int i = 0; i < 10; i++);
should have pretty much exactly the same time complexity, since they are doing the exact same thing.
The only thing that changes the time complexity would be what is inside of the loop. But there's really no reason to favor for over while other than it adds a convenient way to declare and iterate a variable.

Obtaining an algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have a variable a and b. Now I want to find a value for c such that the fraction a / c is a positive integer, and where c is as close to b as possible.
For example: a = 100 and b = 30.
In this case I want c to be 25; because a / c is an integer, and c is as close as b for which this holds.
Any ideas how I can program a function in C++ which does exactly this?
Find the factors of a. (search web for methods)
Scan resulting list for minimum difference vs b.
Is this a homework assignment? Either way, think about how you would solve this problem without writing any code. A good algorithm comes from a good design. Break the problem down into pieces and walk through some more examples. For example, how would you solve the problem of determining whether the division results in an integer value? Hint: There is a different operator you could use as opposed to division to achieve this easily. Now, how would you solve the problem of determining what number to start at for c in the algorithm? Do not write any code until you have the pseudocode figured out.

C++ Algorithm Prediction, need to predict 3 numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
let's go straight to the facts.
I am studying for my own interest an eventual prediction algorithm for some unexistent lottery.
Let's say they roll out 3 numbers every day.
And those numbers are in range from 0 to 50.
I am asking, what would be the best approach to try to predict next 3 numbers knowing
all previous historic ones?
1. What i have
I have a list of 3 numbers from a range of {0,50} (integers)
<x0,y0,z0>
<x1,y1,z1>
<x2,y2,z2>
<x3,y3,z3>
Those numbers represent winning values of lottery.
2. What i need
I need to predict next 3 lottery numbers(possible WINNERS) by taking previous numbers into consideration
The order of the predicted numbers doesn't mater. It might be 1,2,3 or 3,2,1.
3. Question
Which approach / algorithm should i choose and why?
Super thanks for any help!
If the numbers you roll out are random, there is no way to make a prediction, as the next numbers are not linked in any way to the previous one. The most you can do is a guessing algorithm.

Generate prime factors of a number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a function that given an Int greater than one gives a non decreasing list made of the prime factors (with repetition) of that number
Example: n = 12, the output should be [2,2,3]
I don't know where to start.
There are of course well know algorithms for what you want to do, so simple google search would really solve that.
However, I'd like to show you a simple thinking process that might be helpful in the future.
Since the factors have to appear in the ascending order, you might:
Start with the lowest prime (2).
Check if the number can be divided by it. If it can, do it and go back to 1.
If not, replace 2 with a next prime and go back to 2.
Now, it's obvious that the biggest prime you will ever check is the number you've started with. However, the basic multiplication axiom states that if a number can be divided by a:
n / a = b
Then it can also be divided by b! You can use that fact to further narrow the checking range, but I'll leave it to you to figure (or google) the upper bound.
Actual implementation is of course a part of your homework and thus supplying code wouldn't be a wise idea here. However, I don't think that stuff such as next_prime will be hard for you.