# Brute Force

• 08-15-2007
swgh
Brute Force
Since this opposes all languages It was better off placing in GD

Anyway, am I right in saying that Brute Force Programming is more or less a polished term for "hard coding?" I mean I have not come across an incident when I have had to "force" the computer to do what I wanted.

Or am I getting this all upside-down and back to front?
• 08-15-2007
CornedBee
No, that's not right, I think.

Hard coding refers to programming to very special cases, making the program inflexible and unmaintainable. However, that doesn't mean that there can't be some structure to it.

Brute force programming is even worse, because in addition to being written to special cases, it's also written without any concept or structure.

Not that there will be much difference in practice.
• 08-15-2007
Salem
http://en.wikipedia.org/wiki/Brute-force_search
It's more to do with writing simple programs which try every possible combination of solutions until a solution (or the best solution) is found.

For example, to count the frequency of a value in an unordered array, you would have to search every array element. This would be brute force.
If on the other hand the array was sorted, then finding (and counting) the value becomes a much quicker operation.

There are a whole bunch of problems - eg. http://en.wikipedia.org/wiki/P&#37;3DNP_problem - which are in the hard to solve category, and for which there is no good answer which doesn't take universe lifetimes to compute.

Crypto is another example where there are no answers better than brute force.
• 08-15-2007
Darryl
No, I think you got it wrong, I found this definition for
Hard Coding: http://en.wikipedia.org/wiki/Hard_coding
• 08-15-2007
MacGyver
How about we put a simple example:

Code:

```#include <stdio.h> int main(void) {         char *szFile = "SaveOutput.dat"; /* Hardcoding */         ....         return 0; }```
As opposed to:

Code:

```#include <stdio.h> int main(int argc, char *argv[]) {         char *szFile;         if(argc > 1)         {                 szFile = argv[1];         }         ...         return 0; }```
Bruteforcing is something else. It has to do with trying every possible combination.

Code:

```#include <stdio.h> int main(void) {         int x;         printf("Enter a number between 0 and 100 inclusive: ");         fflush(stdout);         scanf("%d", &x);         for(i=0;i<101;i++)         {                 if(i == x)                 {                         printf("Your number is %d.\n", x);                 }                 else printf("I'm guessing your number is %d... Nope, I'm wrong.\n", i);         }         return 0; }```
That's bruteforcing.

Otherwise, you could guess a little better. For example, start with ((HighLimit - LowLimit) / 2), etc. etc..
• 08-16-2007
VirtualAce
Drawing terrain without any triangle optimization, frustum culling, or any other type of algorithm is an example of brute force.

Using a quad tree or any other type of data reducing or render reducing algorithm is not brute force.
• 08-16-2007
@nthony
When "Brute Force" is used to apply to an algorithm, it means as suggested above, that every possible solution is considered (usually linearly) until a solution is found.
However, when used in the context of programming (i.e. referring to coding style), it is interchangeable with "hard-coding".
I.E. If you have to print the numbers 1 to 100 on screen and used 100 printf statements, I would say that you "hard-coded" or "brute forced" it. As you can see, brute-force used in this context is still highly similar to its "algorithm" context because, much like a brute-force algorithm, you are exclusively going through each step to reach the solution.

Personally, I tend to use "hard-code" for the coding style and "brute force" for the algorithm style to avoid confusion, but, yes, I've heard both used for coding style many times.