Deutsch English Français Italiano |
<86r02spzm7.fsf@linuxsc.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch <tr.17687@z991.linuxsc.com> Newsgroups: comp.lang.c Subject: Re: Suggested method for returning a string from a C program? Date: Wed, 19 Mar 2025 12:52:16 -0700 Organization: A noiseless patient Spider Lines: 97 Message-ID: <86r02spzm7.fsf@linuxsc.com> References: <vrd77d$3nvtf$2@dont-email.me> <868qp1ra5f.fsf@linuxsc.com> <vrdhok$47cb$2@dont-email.me> <864izpr3nk.fsf@linuxsc.com> <vreuhk$1asii$3@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 19 Mar 2025 20:52:17 +0100 (CET) Injection-Info: dont-email.me; posting-host="f38801bdea7b04066d3e2a5989bc7bcf"; logging-data="1616995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SYrIcweAxx8KPgEv7cBE5t/hv4K9kzXg=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:zFc709bswwpKVMaor6oN+XnGKXQ= sha1:zNTX0fpRCuBUDp9tO2KL5MUPAb4= Bytes: 3546 DFS <nospam@dfs.com> writes: > On 3/19/2025 1:27 AM, Tim Rentsch wrote: > >> DFS <nospam@dfs.com> writes: >> >>> On 3/18/2025 11:07 PM, Tim Rentsch wrote: >>> >>> >>>> Have you thought about how large the value of 'n' can >>>> become inside the while() loop? >>> >>> I was too smug in my first reply. [...] >> >> Yes, I knew that already. Did you think I asked the question >> without having first investigated the problem? > > I wouldn't presume. > > Did you investigate first? Of course. > I just now did: > > gcc on Kali Linux (in Windows WSL) > > run it: $ ./weird start stop > > $time ./weird 1 1000000 > <1000000 lines will be output> > max n = 56991483520 reached at input = 704511 > > real 0m5.792s > > > > code > ----------------------------------------------------------------- > // If n is even, divide it by two. > // If n is odd, multiply it by three and add one. > // Repeat until n is one. > // example: the sequence for n=3 is 3 10 5 16 8 4 2 1 > > #include <stdio.h> > #include <stdlib.h> > > int main(int argc, char *argv[]) > { > int steps, input; > int startN = atoi(argv[1]); > int stopN = atoi(argv[2]); > int64_t n = 0, max = 0, thismax = 0; > > for (int i = startN; i <= stopN; i++) { > > n = i; > steps = 1; > thismax = n; > while(1) { > > if((n % 2) == 0) > {n /= 2;} > else > {n = (n * 3) + 1;} > > if (n > max) {max = n; input = i;} > if (n > thismax) {thismax = n;} > steps++; > > if (i == 1) { > printf("input 1, max n = 1, steps = 1\n"); > break; > } > > if(n == 1) { > printf("input %d, max n = %6lld, steps = %4d\n", > i, thismax, steps); > break; > } > } > > > } > printf("\nmax n = %lld reached at input = %d\n", max, input); > return 0; > } > > ----------------------------------------------------------------- > > You'll get compilation warnings about the printf specifier used with > int64_t. I recommend using unsigned long long rather than int64_t. After all, all values reached are guaranteed to be non-negative. And there is no confusion or uncertainty about what conversion sequence to use in the call to printf().