Fourteen Years of Service
Posts: 176
Threads: 21
Brute-Force Algorithm? 10-06-2012, 04:02 AM
#1
Hello again, community--
I need help to figure out how to do this algorithm as I do not normally use recursion. What I would like to do it have a function with a loop that recursively calls that loop to create another for loop, until it is stopped by itself. xD
I have not been able to come up with such an algorithm, so I decided to ask you if it's possible, and if so, provide me with an example. Thanks!
To hack is a skill, but a skill may or may not be to hack.
•
Fourteen Years of Service
Posts: 721
Threads: 49
RE: Brute-Force Algorithm? 10-09-2012, 05:21 PM
#2
I don't understand the question. Loops are form of iteration and allow you to define algorithms using the iterative way. On the other way, function calling itself (directly or indirectly) is recursion, which is a way to write algorithms using the recursive way. Loop doesn't call anything, loop simply executes a certain piece of code multiple times.
Here's a small example demonstrating difference between iterative and recursive implementation of the same algorithm - factorial (product of all integer numbers smaller or equal to number
n).
Iterative version (using a loop)
Code:
double FactorialIterative(int n)
{
double product = 1.0;
for(int i = 2; i <= n; i++)
product *= i;
return product;
}
Recursive version (using a function calling itself)
Code:
double FactorialRecursive(int n)
{
if(n <= 1)
return 1;
else
return n*FactorialRecursive(n-1);
}
Both calculate exact same values, but in a different way. Generally iterative version is better - faster, uses less memory (each function call allocates more memory on the stack) and there's no or lower risk of stack overflow (too many nested calls, that the program runs out of memory to keep track of them), but also generally more difficult to implement, especially for more complex tasks. If you wanted to use recursive algorithm just to try out all the possibilities in a brute force algorithm, it would be 1) Slower and 2) you would run out of memory (your program would crash) pretty soon. I'm just guessing though, as I already said that your question isn't really very clear.
Test of the algorithm implementations provided above
Code:
#include <iostream>
using namespace std;
double FactorialIterative(int n)
{
double product = 1.0;
for(int i = 2; i <= n; i++)
product *= i;
return product;
}
double FactorialRecursive(int n)
{
if(n <= 1)
return 1;
else
return n*FactorialRecursive(n-1);
}
int main()
{
for(int i = 0; i < 20; i++)
cout << i << "! = \t" << FactorialIterative(i) << "\t" << FactorialRecursive(i) << endl;
return 0;
}
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.
•
Fourteen Years of Service
Posts: 171
Threads: 13
RE: Brute-Force Algorithm? 10-16-2012, 03:52 PM
#3
btw, what's the relationship between looped-recursive-functions and brute force? i don't see that they have a strong relationship...
•
Fourteen Years of Service
Posts: 176
Threads: 21
RE: Brute-Force Algorithm? 10-17-2012, 02:40 PM
#4
About Frooxius's answer: I tried doing something like [main calling a loop function, loop function starting a loop and calling itself and some other stuffs] and it ran out of memory, but it ended up at the end of the loop in almost no time... Is it just the compiler giving me false hope that it is actually going through the *whole* loop?
To hack is a skill, but a skill may or may not be to hack.
•
Fourteen Years of Service
Posts: 721
Threads: 49
RE: Brute-Force Algorithm? 10-17-2012, 04:22 PM
#5
What's a loop function? There's no such thing as loop function in C/C++. You can have a loop in the code and you can contain the code in a function, basically all the code is contained within functions (main is a function too).
What you describe sounds more like a recursive implementation instead of a loop, since you ran out of memory. If you read my post properly, you'll see that it's what's most likely to happen with recursive implementations, because each nested call allocates more space on the stack and if it goes deep enough, it will run out of space.
I have no idea why you want to use recursive algorithm for a brute force algorithm. You description is too confused and contains too little information to draw any conclusions or actually help you somehow. Please provide more clear description of what you're trying to do and if you have some code that doesn't work, it would be ideal to show that too, otherwise you won't get much help.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.
•