Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #8260
| From | root <NoEMail@home.org> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: Looking for simple brute force solution |
| Date | 2018-01-04 17:26 +0000 |
| Organization | Linux Advocacy |
| Message-ID | <p2lo4t$lmo$1@news.albasani.net> (permalink) |
| References | <p2jobe$48s$1@news.albasani.net> <Djg3C.31394$uY5.18424@fx21.iad> <p2kstv$o1a$1@news.albasani.net> <Kzr3C.51402$uM7.28748@fx06.iad> |
Richard Damon <Richard@Damon-Family.org> wrote:
> On 1/4/18 4:42 AM, root wrote:
>> Richard Damon <Richard@Damon-Family.org> wrote:
>>>
>>> Brute force method:
>>>
>>> One simple method would be to have an array, the same size as the number
>>> of numbers, with values representing the options, maybe 0 for ignore, 1
>>> to add, -1 to subtract.
>>>
>>> 1) Start all elements a 0.
>>>
>>> 2) process.
>>>
>>> 3) Take the first element, move it to the next value in the sequence,
>>> 0 -> 1 -> -1 -> 0
>>>
>>> if it became 0, then move to the next element and repeat until you
>>> sequence and element and it doesn't become 0, or you run out of elements
>>> (then you are done).
>>>
>>> go back to 2 and process
>>>
>>>
>>> This is basically counting base 3.
>>>
>
> (untested code)
>
> #define N 20
> int index[N];
> int i;
>
> for(i=0; i<N; i++)
> index[N] = 0;
>
> while(1)) {
> /* Do Stuff */
>
> /* Now, increment the state */
> i = 0;
> while(1) {
> if(index[i] == 1) index[N] = -1;
> else index[N] = index[N] +1;
> if(index[N] != 0) break; /* Have new number */
> i = i+1; /* need to propagate the carry */
> if(i >= N) break; /* Number overflowed */
> }
> if(i >= N) break; /* Overflow goes all the wa out */
> }
>
As written the program doesn't terminate. I think in the inner loop
you mean index[i] when you write index[N]? Making those
changes the program terminates after 243 iterations which
is 3^5.
Here is the final program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termio.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <ctype.h>
#define N 5
int main(argc,argv)
int argc;
char **argv;
{
int index[N];
int i;
int j;
for(i=0; i<N; i++)
index[i] = 0;
while(1) {
/* Do Stuff */
for(j=0;j<N;++j) printf("%d ",index[j]);
printf("\n");
/* Now, increment the state */
i = 0;
while(1) {
if(index[i] == 1) index[i] = -1;
else index[i] = index[i] +1;
if(index[i] != 0) break; /* Have new number */
i = i+1; /* need to propagate the carry */
if(i == N) break; /* Number overflowed */
}
if(i == N) break; /* Overflow goes all the wa out */
}
}
Thanks for the suggestion. I'll keep this program.
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-03 23:18 +0000
Re: Looking for simple brute force solution fir <profesor.fir@gmail.com> - 2018-01-03 16:49 -0800
Re: Looking for simple brute force solution fir <profesor.fir@gmail.com> - 2018-01-03 17:11 -0800
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 10:14 +0000
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-04 10:52 -0500
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 16:46 +0000
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-04 12:37 -0500
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-03 21:28 -0500
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 09:42 +0000
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-04 10:16 -0500
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 17:26 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 21:19 +0000
Re: Looking for simple brute force solution Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-04 02:29 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 09:55 +0000
Re: Looking for simple brute force solution Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-04 14:29 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 16:55 +0000
Re: Looking for simple brute force solution Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-04 20:36 +0000
Re: Looking for simple brute force solution Robert Wessel <robertwessel2@yahoo.com> - 2018-01-04 12:03 -0600
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 21:28 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 21:50 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-04 23:06 +0000
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-04 21:35 -0500
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-05 05:59 +0000
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-05 16:48 +0000
Re: Looking for simple brute force solution Richard Damon <Richard@Damon-Family.org> - 2018-01-06 08:50 -0500
Re: Looking for simple brute force solution root <NoEMail@home.org> - 2018-01-06 16:47 +0000
csiph-web