Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.programming Subject: Re: Looking for simple brute force solution Date: Thu, 04 Jan 2018 20:36:44 +0000 Organization: A noiseless patient Spider Lines: 31 Message-ID: <87y3ld744j.fsf@bsb.me.uk> References: <87shbm8igl.fsf@bsb.me.uk> <87h8s18zpa.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="d91270aa90bb7038a06ebe75f0537788"; logging-data="22041"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8r52WII1Qw0EryZRSUT7IeHC/3qF4lvk=" Cancel-Lock: sha1:G4MW3VD6hMeZwX4rCqACqpnI0WA= sha1:AJzhwqfF4JzXz3WC1wpCqxqI45U= X-BSB-Auth: 1.a5fc885e96a492dd6b2d.20180104203644GMT.87y3ld744j.fsf@bsb.me.uk Xref: csiph.com comp.programming:8263 root writes: > Ben Bacarisse wrote: >> root writes: >> >>> Ben Bacarisse wrote: >>>> root writes: >>>> >>>> >>>> You can just count up to 3^N. Call the count i. For each number in the >>>> set you use i%3 to determine if you add, subtract or ignore. For the >>>> next number you set i = i/3 and repeat. In effect you are pulling out >>>> the base-3 digits of i. (I'm using % to mean remainder and / for >>>> integer division.) >>> >>> Yes, that is the solution I was looking for. Thanks. >>> >>> I can do that with two nested loops instead of N. >> >> OK, but I don't see why you need two nested loops. That's bad news >> because you are squaring an already exponential function. > > The outer loop is to count from 0 to 3^N. The inner loop is to > work the outer loop counter down to 0 via a sequence of /3 > operations. Ah, right, so just what I was proposing. The wording suggested to me that the two loops had the same bounds. -- Ben.