Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Multics is security spelled sideways.


devel / comp.lang.c / Re: Losing my mind: results change with/without printf() statements

SubjectAuthor
* Losing my mind: results change with/without printf() statementsDFS
+* Re: Losing my mind: results change with/without printf() statementsDavid Brown
|`* Re: Losing my mind: results change with/without printf() statementsDFS
| +* Re: Losing my mind: results change with/without printf() statementsBart
| |`* Re: Losing my mind: results change with/without printf() statementsDFS
| | +* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| | |`* Re: Losing my mind: results change with/without printf() statementsDFS
| | | +- Re: Losing my mind: results change with/without printf() statementsDavid Brown
| | | `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| | |  `* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |   `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| | |    `* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |     `* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |      `* Re: Losing my mind: results change with/without printf() statementsBart
| | |       `* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |        `* Re: Losing my mind: results change with/without printf() statementsBart
| | |         `* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |          `- Re: Losing my mind: results change with/without printf() statementsMichael S
| | +* Re: Losing my mind: results change with/without printf() statementsMichael S
| | |`* Re: Losing my mind: results change with/without printf() statementsBart
| | | `- Re: Losing my mind: results change with/without printf() statementsMichael S
| | `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |  +- Re: Losing my mind: results change with/without printf() statementsDFS
| |  +- Re: Losing my mind: results change with/without printf() statementsBart
| |  `* Re: Losing my mind: results change with/without printf() statementsKaz Kylheku
| |   `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |    `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |     `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |      `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |       `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |        `* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |         `* Re: Losing my mind: results change with/without printf() statementsKent Dickey
| |          `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |           `* Re: Losing my mind: results change with/without printf() statementsKent Dickey
| |            `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |             +- Re: Losing my mind: results change with/without printf() statementsDFS
| |             `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |              `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               +* Re: Losing my mind: results change with/without printf() statementsKent Dickey
| |               |`* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               | `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               |  `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               |   `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               |    +* Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |               |    |`* Re: Losing my mind: results change with/without printf() statementsMichael S
| |               |    | `- Re: Losing my mind: results change with/without printf() statementsBen Bacarisse
| |               |    `- Re: Losing my mind: results change with/without printf() statementsMichael S
| |               +* Re: Losing my mind: results change with/without printf() statementsDFS
| |               |`- Re: Losing my mind: results change with/without printf() statementsMichael S
| |               `* Re: Losing my mind: results change with/without printf() statementsMike Terry
| |                `* Re: Losing my mind: results change with/without printf() statementsMichael S
| |                 `* Re: Losing my mind: results change with/without printf() statementsMike Terry
| |                  +- Re: Losing my mind: results change with/without printf() statementsMike Terry
| |                  `- Re: Losing my mind: results change with/without printf() statementsMike Terry
| +* Re: Losing my mind: results change with/without printf() statementsDavid Brown
| |`* Re: Losing my mind: results change with/without printf() statementsDFS
| | `* Re: Losing my mind: results change with/without printf() statementsBart
| |  `- Re: Losing my mind: results change with/without printf() statementsDFS
| `- Re: Losing my mind: results change with/without printf() statementsluser droog
+* Re: Losing my mind: results change with/without printf() statementsBarry Schwarz
|`* Re: Losing my mind: results change with/without printf() statementsDFS
| +- Re: Losing my mind: results change with/without printf() statementsBarry Schwarz
| `- Re: Losing my mind: results change with/without printf() statementsDavid Brown
`- Re: Losing my mind: results change with/without printf() statementsRosario19

Pages:123
Losing my mind: results change with/without printf() statements

<vW0DI.53$h45.18@fx16.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17135&group=comp.lang.c#17135

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
Newsgroups: comp.lang.c
X-Mozilla-News-Host: news://usnews.blocknews.net:119
From: nos...@dfs.com (DFS)
Subject: Losing my mind: results change with/without printf() statements
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Lines: 89
Message-ID: <vW0DI.53$h45.18@fx16.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Wed, 30 Jun 2021 16:26:03 UTC
Organization: blocknews - www.blocknews.net
Date: Wed, 30 Jun 2021 12:26:02 -0400
X-Received-Bytes: 2759
 by: DFS - Wed, 30 Jun 2021 16:26 UTC

My program identifies all the 'one-child' numbers in a range (a
one-child number has only one substring evenly divisible by the length
of the number).

968 has these substrings: [9, 96, 968, 6, 68, 8]
9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number

5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
Only 56 is evenly divisible by 4, so 5671 is a one-child number

$ prog start end [1|2]
examples
$ prog 1 100 1 (no use of printf(), gives incorrect results)
$ prog 1 100 2 (use printf(), gives correct results)
================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//get substring
char *psubstr(char *string, int pos, int length)
{ int cnt;
char *p;
p = malloc(length+1);
while (cnt < length)
{
*(p+cnt) = *(string+pos);
string++;
cnt++;
}
*(p+cnt) = '\0';
return p;
}

int main(int argc, char *argv[])
{ int start = atoi(argv[1]);
int end = atoi(argv[2]);
int printType = atoi(argv[3]);
int i,j,k,len,ssnbr;
int div=0,ocn=0;
char s[10] = "";
char ss[8] = "";
char *pss;

for(i=start;i<=end;i++)
{
//convert number to char
sprintf(s,"%d",i);
len = strlen(s);

//build and evaluate each substring
if(printType == 2) {printf("%s. [",s);}
for (j=0;j<len;j++)
{
for (k=1;k<=len-j;k++)
{
pss = psubstr(s,j,k);
ssnbr = atoi(pss);
if(ssnbr % len == 0) {div+=1;}
if(printType == 2) {printf("%d ",ssnbr);}
}
}
//increment counter, optional print
if(div==1)
{
ocn++;
if(printType == 2) {printf("] *\n");}
}
else
{
if(printType == 2) {printf("]\n");}
}
//reset
div=0;
}

//total count
printf("\n%d one-child numbers from %d to %d", ocn,start,end);
free(pss);
return(0);
} ================================================================

Re: Losing my mind: results change with/without printf() statements

<sbi84p$ajn$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17137&group=comp.lang.c#17137

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Wed, 30 Jun 2021 19:03:20 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <sbi84p$ajn$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 30 Jun 2021 17:03:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b0e3b71792d96e0a09c27d7a4fcf832f";
logging-data="10871"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Pk/bpormY7g5gCK7MEv4vrWltZHDFUDU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:/ilKeByDR0UyKTA4ycjqKESrIGI=
In-Reply-To: <vW0DI.53$h45.18@fx16.iad>
Content-Language: en-GB
 by: David Brown - Wed, 30 Jun 2021 17:03 UTC

On 30/06/2021 18:26, DFS wrote:
> My program identifies all the 'one-child' numbers in a range (a
> one-child number has only one substring evenly divisible by the length
> of the number).
>
> 968 has these substrings: [9, 96, 968, 6, 68, 8]
> 9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number
>
> 5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
> Only 56 is evenly divisible by 4, so 5671 is a one-child number
>
> $ prog start end [1|2]
> examples
> $ prog 1 100 1   (no use of printf(), gives incorrect results)
> $ prog 1 100 2   (use printf(), gives correct results)
> ================================================================
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> //get substring
> char *psubstr(char *string, int pos, int length)
> {
>    int cnt;
>    char *p;
>    p = malloc(length+1);
>    while (cnt < length)
>    {
>       *(p+cnt) = *(string+pos);
>       string++;
>       cnt++;
>    }
>    *(p+cnt) = '\0';
>    return p;
> }
>

What do you see when you compile with warnings enabled? A quick test
with gcc points out that you are using "cnt" here without initialising
it - thus the behaviour of the code depends entirely on what might
happen to be in registers or the stack when this function is called.

Your main() is also going to leak like a sieve, as you allocate memory
on each call to psubstr but only deallocate the last one. And it
defines "ss" but does not use it, which is almost certainly a bug.

I haven't read the code or attempted to understand it - first pick off
the low-lying fruit that can be found without effort.

Re: Losing my mind: results change with/without printf() statements

<Yg2DI.3867$mR.1082@fx33.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17139&group=comp.lang.c#17139

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!feeder5.feed.usenet.farm!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx33.iad.POSTED!not-for-mail
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
From: nos...@dfs.com (DFS)
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <sbi84p$ajn$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 101
Message-ID: <Yg2DI.3867$mR.1082@fx33.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Wed, 30 Jun 2021 17:58:16 UTC
Organization: blocknews - www.blocknews.net
Date: Wed, 30 Jun 2021 13:58:15 -0400
X-Received-Bytes: 4008
 by: DFS - Wed, 30 Jun 2021 17:58 UTC

On 6/30/2021 1:03 PM, David Brown wrote:
> On 30/06/2021 18:26, DFS wrote:
>> My program identifies all the 'one-child' numbers in a range (a
>> one-child number has only one substring evenly divisible by the length
>> of the number).
>>
>> 968 has these substrings: [9, 96, 968, 6, 68, 8]
>> 9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number
>>
>> 5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
>> Only 56 is evenly divisible by 4, so 5671 is a one-child number
>>
>> $ prog start end [1|2]
>> examples
>> $ prog 1 100 1   (no use of printf(), gives incorrect results)
>> $ prog 1 100 2   (use printf(), gives correct results)
>> ================================================================
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> //get substring
>> char *psubstr(char *string, int pos, int length)
>> {
>>    int cnt;
>>    char *p;
>>    p = malloc(length+1);
>>    while (cnt < length)
>>    {
>>       *(p+cnt) = *(string+pos);
>>       string++;
>>       cnt++;
>>    }
>>    *(p+cnt) = '\0';
>>    return p;
>> }
>>
>
> What do you see when you compile with warnings enabled? A quick test
> with gcc points out that you are using "cnt" here without initialising
> it - thus the behaviour of the code depends entirely on what might
> happen to be in registers or the stack when this function is called.
>
> Your main() is also going to leak like a sieve, as you allocate memory
> on each call to psubstr but only deallocate the last one. And it
> defines "ss" but does not use it, which is almost certainly a bug.
>
> I haven't read the code or attempted to understand it - first pick off
> the low-lying fruit that can be found without effort.

using TinyCC 0.9.27 on Windows

$tcc -Wall prog.c -o prog.exe

no warnings whatsoever

(I think you or someone else on clc warned me away from tcc in the past,
but it's so handy)

I made the fixes you spotted

* initialized cnt to 0
* free(pss) in the k loop
* removed the unused char 'ss'

and it now works. Thanks man!

I'm gonna guess the uninitialized variable is the culprit... checked...
that was it.

I'm a little surprised about the performance of this C program, though.
A python version (below) that also doesn't print anything until the
end result is 2x faster for smaller values of 'end'. But when 'end' is
10^5 and up, the C code smokes the python.

Later I'll try it on Linux/gcc, where I expect the performance to be
much better.

================================================================
import sys
start = int(sys.argv[1])
end = int(sys.argv[2])
div,ocn = 0,0
for i in range(start,end+1):
istr = str(i)
ilen = len(istr)
for j in range(0,ilen):
for k in range(j+1,ilen+1):
if int(istr[j:k]) % ilen == 0:
div+=1
if div == 1: ocn+=1
div = 0
print("\n%d one-child numbers from %d to %d" % (ocn,start,end))
================================================================

Re: Losing my mind: results change with/without printf() statements

<c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17140&group=comp.lang.c#17140

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Wed, 30 Jun 2021 11:36:53 -0700
Organization: A noiseless patient Spider
Lines: 113
Message-ID: <c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com>
References: <vW0DI.53$h45.18@fx16.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="4733920d12a608ad9c160e1f82d325ba";
logging-data="16612"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DAYVa69mIlwKbUW87eFUb8qshIGy1XRE="
Cancel-Lock: sha1:v7ZaJ9sNvJMm1kV7duzxS8wWys8=
X-Newsreader: Forte Agent 4.2/32.1118
 by: Barry Schwarz - Wed, 30 Jun 2021 18:36 UTC

On Wed, 30 Jun 2021 12:26:02 -0400, DFS <nospam@dfs.com> wrote:

>My program identifies all the 'one-child' numbers in a range (a
>one-child number has only one substring evenly divisible by the length
>of the number).
>
>968 has these substrings: [9, 96, 968, 6, 68, 8]
>9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number
>
>5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
>Only 56 is evenly divisible by 4, so 5671 is a one-child number
>
>$ prog start end [1|2]
>examples
>$ prog 1 100 1 (no use of printf(), gives incorrect results)
>$ prog 1 100 2 (use printf(), gives correct results)
>================================================================
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>//get substring
>char *psubstr(char *string, int pos, int length)
>{
> int cnt;
> char *p;
> p = malloc(length+1);
> while (cnt < length)

Since cnt is not initialized or assigned a value, you have no idea how
often the loop is executed, if at all. Didn't your compiler warn you
about using an indeterminate value? If not, you should up the warning
level.

When I initialize cnt to 0, the program prints 29 in either case.

The difference is not caused by the use of printf. It is caused by
the random value that happens to be in the memory occupied by cnt.

> {
> *(p+cnt) = *(string+pos);
> string++;
> cnt++;
> }
> *(p+cnt) = '\0';
> return p;
>}
>
>
>int main(int argc, char *argv[])
>{
> int start = atoi(argv[1]);
> int end = atoi(argv[2]);
> int printType = atoi(argv[3]);
> int i,j,k,len,ssnbr;
> int div=0,ocn=0;
> char s[10] = "";
> char ss[8] = "";
> char *pss;
>
> for(i=start;i<=end;i++)
> {
> //convert number to char
> sprintf(s,"%d",i);
> len = strlen(s);
>
> //build and evaluate each substring
> if(printType == 2) {printf("%s. [",s);}
> for (j=0;j<len;j++)
> {
> for (k=1;k<=len-j;k++)
> {
> pss = psubstr(s,j,k);

pss now points to memory that was allocated in psubstr. psubstr will
allocate new memory each iteration of this loop. The memory is not
freed until the very end of the program. The address of the previous
allocation is lost. You have multiple memory leaks.

> ssnbr = atoi(pss);
> if(ssnbr % len == 0) {div+=1;}
> if(printType == 2) {printf("%d ",ssnbr);}
> }
> }
>
> //increment counter, optional print
> if(div==1)
> {
> ocn++;
> if(printType == 2) {printf("] *\n");}
> }
> else
> {
> if(printType == 2) {printf("]\n");}
> }
>
> //reset
> div=0;
>
> }
>
>//total count
>printf("\n%d one-child numbers from %d to %d", ocn,start,end);
>free(pss);

This frees only the last memory allocated.

>return(0);
>}
>================================================================

--
Remove del for email

Re: Losing my mind: results change with/without printf() statements

<hY2DI.505463$N5n7.405017@fx05.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17141&group=comp.lang.c#17141

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!news.uzoreto.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!nntp.speedium.network!feeder01!81.171.65.16.MISMATCH!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx05.ams4.POSTED!not-for-mail
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me> <Yg2DI.3867$mR.1082@fx33.iad>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <Yg2DI.3867$mR.1082@fx33.iad>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 8bit
X-Antivirus: AVG (VPS 210630-20, 30/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 65
Message-ID: <hY2DI.505463$N5n7.405017@fx05.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Wed, 30 Jun 2021 18:44:29 UTC
Organization: virginmedia.com
Date: Wed, 30 Jun 2021 19:44:23 +0100
X-Received-Bytes: 3090
 by: Bart - Wed, 30 Jun 2021 18:44 UTC

On 30/06/2021 18:58, DFS wrote:
> On 6/30/2021 1:03 PM, David Brown wrote:

>> What do you see when you compile with warnings enabled?  A quick test
>> with gcc points out that you are using "cnt" here without initialising
>> it - thus the behaviour of the code depends entirely on what might
>> happen to be in registers or the stack when this function is called.
>>
>> Your main() is also going to leak like a sieve, as you allocate memory
>> on each call to psubstr but only deallocate the last one.  And it
>> defines "ss" but does not use it, which is almost certainly a bug.
>>
>> I haven't read the code or attempted to understand it - first pick off
>> the low-lying fruit that can be found without effort.
>
>
>
> using TinyCC 0.9.27 on Windows
>
> $tcc -Wall prog.c -o prog.exe
>
> no warnings whatsoever
>
> (I think you or someone else on clc warned me away from tcc in the past,
> but it's so handy)

You're allowed to use gcc with lots of options from time to time to
check that all's well. Then go back to the much faster compiler.

>
> I made the fixes you spotted
>
> * initialized cnt to 0
> * free(pss) in the k loop
> * removed the unused char 'ss'
>
>
> and it now works.  Thanks man!

I added this at the start of main:

if (argc<4) {
printf("Usage: %s start end 1/2\n",argv[0]);
exit(0);
}

Otherwise it crashes if run with no inputs.

> I'm a little surprised about the performance of this C program, though.
>  A python version (below) that also doesn't print anything until the
> end result is 2x faster for smaller values of 'end'.  But when 'end' is
> 10^5 and up, the C code smokes the python.

> Later I'll try it on Linux/gcc, where I expect the performance to be
> much better.

I tried it on N=10,000,000, and gcc-O3 took 41 seconds. PyPy
(accelerated version of Python) took 17 seconds.

tcc took 56 seconds, only 35% slower than gcc-O3.

But I suspect that in all cases, what is dominant is the conversion from
int to text, from text back to int, and applying the mod operator. All
things that happen outside of the code generated from your source.

Re: Losing my mind: results change with/without printf() statements

<v64DI.12622$Vj7.1255@fx46.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17142&group=comp.lang.c#17142

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!peer03.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad>
<c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com>
From: nos...@dfs.com (DFS)
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Lines: 74
Message-ID: <v64DI.12622$Vj7.1255@fx46.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Wed, 30 Jun 2021 20:03:39 UTC
Organization: blocknews - www.blocknews.net
Date: Wed, 30 Jun 2021 16:03:37 -0400
X-Received-Bytes: 3015
 by: DFS - Wed, 30 Jun 2021 20:03 UTC

On 6/30/2021 2:36 PM, Barry Schwarz wrote:
> On Wed, 30 Jun 2021 12:26:02 -0400, DFS <nospam@dfs.com> wrote:
>
>> My program identifies all the 'one-child' numbers in a range (a
>> one-child number has only one substring evenly divisible by the length
>> of the number).
>>
>> 968 has these substrings: [9, 96, 968, 6, 68, 8]
>> 9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number
>>
>> 5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
>> Only 56 is evenly divisible by 4, so 5671 is a one-child number
>>
>> $ prog start end [1|2]
>> examples
>> $ prog 1 100 1 (no use of printf(), gives incorrect results)
>> $ prog 1 100 2 (use printf(), gives correct results)
>> ================================================================
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> //get substring
>> char *psubstr(char *string, int pos, int length)
>> {
>> int cnt;
>> char *p;
>> p = malloc(length+1);
>> while (cnt < length)
>
> Since cnt is not initialized or assigned a value, you have no idea how
> often the loop is executed, if at all. Didn't your compiler warn you
> about using an indeterminate value? If not, you should up the warning
> level.
>
> When I initialize cnt to 0, the program prints 29 in either case.

Right. Thanks for your testing.

> The difference is not caused by the use of printf.

Any idea, then, why using printf() returns correct results?

The substr function is called the same number of times regardless.

> It is caused by the random value that happens to be in the
> memory occupied by cnt.

I didn't note in my op but the incorrect value shown with no printf() is
always 9, which is the number of one-child values in F(10^1).

Without printf()
F(10^1) = 9
F(10^2) = 9
F(10^3) = 9
F(10^4) = 9
F(10^5) = 9

With printf()
F(10^1) = 9
F(10^2) = 29
F(10^3) = 389
F(10^4) = 3090
F(10^5) = 7186

So it doesn't seem to be random.

But since cnt isn't initialized, does the compiler auto-assign a 0 on
the first call to cnt++?

Re: Losing my mind: results change with/without printf() statements

<Gi4DI.1867$t64.414@fx13.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17143&group=comp.lang.c#17143

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx13.iad.POSTED!not-for-mail
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
From: nos...@dfs.com (DFS)
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <hY2DI.505463$N5n7.405017@fx05.ams4>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 84
Message-ID: <Gi4DI.1867$t64.414@fx13.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Wed, 30 Jun 2021 20:16:38 UTC
Organization: blocknews - www.blocknews.net
Date: Wed, 30 Jun 2021 16:16:36 -0400
X-Received-Bytes: 3631
 by: DFS - Wed, 30 Jun 2021 20:16 UTC

On 6/30/2021 2:44 PM, Bart wrote:
> On 30/06/2021 18:58, DFS wrote:
>> On 6/30/2021 1:03 PM, David Brown wrote:
>
>>> What do you see when you compile with warnings enabled?  A quick test
>>> with gcc points out that you are using "cnt" here without initialising
>>> it - thus the behaviour of the code depends entirely on what might
>>> happen to be in registers or the stack when this function is called.
>>>
>>> Your main() is also going to leak like a sieve, as you allocate memory
>>> on each call to psubstr but only deallocate the last one.  And it
>>> defines "ss" but does not use it, which is almost certainly a bug.
>>>
>>> I haven't read the code or attempted to understand it - first pick off
>>> the low-lying fruit that can be found without effort.
>>
>>
>>
>> using TinyCC 0.9.27 on Windows
>>
>> $tcc -Wall prog.c -o prog.exe
>>
>> no warnings whatsoever
>>
>> (I think you or someone else on clc warned me away from tcc in the
>> past, but it's so handy)
>
> You're allowed to use gcc with lots of options from time to time to
> check that all's well. Then go back to the much faster compiler.

For my little code it's about a half-second either way.

>> I made the fixes you spotted
>>
>> * initialized cnt to 0
>> * free(pss) in the k loop
>> * removed the unused char 'ss'
>>
>>
>> and it now works.  Thanks man!
>
> I added this at the start of main:
>
>     if (argc<4) {
>         printf("Usage: %s start end 1/2\n",argv[0]);
>         exit(0);
>     }
>
> Otherwise it crashes if run with no inputs.

Thou must read the instructions:

$ prog start end [1|2]

>> I'm a little surprised about the performance of this C program,
>> though.   A python version (below) that also doesn't print anything
>> until the end result is 2x faster for smaller values of 'end'.  But
>> when 'end' is 10^5 and up, the C code smokes the python.
>
>> Later I'll try it on Linux/gcc, where I expect the performance to be
>> much better.
>
> I tried it on N=10,000,000, and gcc-O3 took 41 seconds. PyPy
> (accelerated version of Python) took 17 seconds.

Not bad numbers for python and F(10^7), but this code is part of me
trying to solve Project Euler 413, which asks for F(10^19).

You can't brute force that with a desktop PC.

> tcc took 56 seconds, only 35% slower than gcc-O3.
>
> But I suspect that in all cases, what is dominant is the conversion from
> int to text, from text back to int, and applying the mod operator. All
> things that happen outside of the code generated from your source.

I don't understand 'happen outside'.

Thanks for looking at it.

Re: Losing my mind: results change with/without printf() statements

<4qjpdglks5eogms5l61v0qjqe008gnb9nu@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17144&group=comp.lang.c#17144

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Wed, 30 Jun 2021 13:22:26 -0700
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <4qjpdglks5eogms5l61v0qjqe008gnb9nu@4ax.com>
References: <vW0DI.53$h45.18@fx16.iad> <c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com> <v64DI.12622$Vj7.1255@fx46.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="4733920d12a608ad9c160e1f82d325ba";
logging-data="28435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18m9ECwocWAi5+wu0fo4Vyj1cAWg+ZEg4M="
Cancel-Lock: sha1:ttI9Y8gqcJImccm0zYy3pIzc16g=
X-Newsreader: Forte Agent 4.2/32.1118
 by: Barry Schwarz - Wed, 30 Jun 2021 20:22 UTC

On Wed, 30 Jun 2021 16:03:37 -0400, DFS <nospam@dfs.com> wrote:

>On 6/30/2021 2:36 PM, Barry Schwarz wrote:
>> On Wed, 30 Jun 2021 12:26:02 -0400, DFS <nospam@dfs.com> wrote:
>>
>>> My program identifies all the 'one-child' numbers in a range (a
>>> one-child number has only one substring evenly divisible by the length
>>> of the number).
>>>
>>> 968 has these substrings: [9, 96, 968, 6, 68, 8]
>>> 9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child number
>>>
>>> 5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
>>> Only 56 is evenly divisible by 4, so 5671 is a one-child number
>>>
>>> $ prog start end [1|2]
>>> examples
>>> $ prog 1 100 1 (no use of printf(), gives incorrect results)
>>> $ prog 1 100 2 (use printf(), gives correct results)
>>> ================================================================
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <string.h>
>>>
>>> //get substring
>>> char *psubstr(char *string, int pos, int length)
>>> {
>>> int cnt;
>>> char *p;
>>> p = malloc(length+1);
>>> while (cnt < length)
>>
>> Since cnt is not initialized or assigned a value, you have no idea how
>> often the loop is executed, if at all. Didn't your compiler warn you
>> about using an indeterminate value? If not, you should up the warning
>> level.
>>
>> When I initialize cnt to 0, the program prints 29 in either case.
>
>Right. Thanks for your testing.
>
>
>> The difference is not caused by the use of printf.
>
>Any idea, then, why using printf() returns correct results?
>
>The substr function is called the same number of times regardless.

Once you have undefined behavior, all bets are off. While it may be
true that psubstr is called the same number of times, the while will
definitely iterate a different number of times depending an the random
value in cnt.

Furthermore, after the first call to psubstr returns, cnt will equal
length. If the memory occupied by cnt is unchanged, on subsequent
calls the while loop will terminate immediately since cnt is not less
than length (until length changes as in going from 9 to 10).

The bottom line is that undefined behavior can have any result. One
of the worst results is to sometimes do what you want or expect.

> > It is caused by the random value that happens to be in the
> > memory occupied by cnt.
>
>I didn't note in my op but the incorrect value shown with no printf() is
>always 9, which is the number of one-child values in F(10^1).

When I ran my test case with cnt initialized to 3 (a random value), I
also got 9 as the result, both with and without printf.
>Without printf()
>F(10^1) = 9
>F(10^2) = 9
>F(10^3) = 9
>F(10^4) = 9
>F(10^5) = 9
>
>
>With printf()
>F(10^1) = 9
>F(10^2) = 29
>F(10^3) = 389
>F(10^4) = 3090
>F(10^5) = 7186
>
>
>So it doesn't seem to be random.
>
>But since cnt isn't initialized, does the compiler auto-assign a 0 on
>the first call to cnt++?

--
Remove del for email

Re: Losing my mind: results change with/without printf() statements

<87im1vj6q3.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17145&group=comp.lang.c#17145

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 01 Jul 2021 00:03:16 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <87im1vj6q3.fsf@bsb.me.uk>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="a2d8b1c21ccbe83bdb1b6c6beabf5900";
logging-data="23602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189JtIZ1oOP/SaW341FjhRgFPR5F5MpDIs="
Cancel-Lock: sha1:LrBko/0OABmtVeb6w8JirsWni3o=
sha1:zI/6WKE4cGCU8B13aYbtI4RuNLA=
X-BSB-Auth: 1.9be0d0d8eda7624009f9.20210701000316BST.87im1vj6q3.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 30 Jun 2021 23:03 UTC

DFS <nospam@dfs.com> writes:

> On 6/30/2021 2:44 PM, Bart wrote:
<cut>
>> But I suspect that in all cases, what is dominant is the conversion
>> from int to text, from text back to int, and applying the mod
>> operator. All things that happen outside of the code generated from
>> your source.
>
> I don't understand 'happen outside'.

I think he means that your calls to atoi are doing most of the work, so
you can't get much more speed from tinkering with the code you actually
wrote.

That's not quite right in that you can get a good speed-up by not
generating all those sub-strings. You already have the full number in
's' so you can get sub-string numbers by doing this:

char save = s[j+k];
s[j+k] = 0;
ssnbr = atoi(s+j);
s[j+k] = save;

Having said that, Bart's point still holds. This can be seen as a
purely arithmetical problem. There is no need for strings conversions
(and hence atoi) at all. Whether that would make the code faster is not
obvious, but I suspect it might. I've not have time to try it.

> Thanks for looking at it.

Your code looks a little old fashioned. But even sticking to old C
standards you could tidy it up a but. For example, it's neater to
declare int div = 0; in the block that needs it, rather than "resetting"
at the bottom of the loop.

--
Ben.

Re: Losing my mind: results change with/without printf() statements

<O%aDI.906$tE4.1@fx29.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17148&group=comp.lang.c#17148

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.uzoreto.com!news-out.netnews.com!newsin.alt.net!fdcspool1.netnews.com!news-out.netnews.com!news.alt.net!fdc3.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx29.iad.POSTED!not-for-mail
From: nos...@dfs.com (DFS)
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk>
X-Mozilla-News-Host: news://usnews.blocknews.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <87im1vj6q3.fsf@bsb.me.uk>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Lines: 57
Message-ID: <O%aDI.906$tE4.1@fx29.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Thu, 01 Jul 2021 03:54:22 UTC
Organization: blocknews - www.blocknews.net
Date: Wed, 30 Jun 2021 23:54:21 -0400
X-Received-Bytes: 2932
 by: DFS - Thu, 1 Jul 2021 03:54 UTC

On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
>
>> On 6/30/2021 2:44 PM, Bart wrote:
> <cut>
>>> But I suspect that in all cases, what is dominant is the conversion
>>> from int to text, from text back to int, and applying the mod
>>> operator. All things that happen outside of the code generated from
>>> your source.
>>
>> I don't understand 'happen outside'.
>
> I think he means that your calls to atoi are doing most of the work, so
> you can't get much more speed from tinkering with the code you actually
> wrote.
>
> That's not quite right in that you can get a good speed-up by not
> generating all those sub-strings. You already have the full number in
> 's' so you can get sub-string numbers by doing this:
>
> char save = s[j+k];
> s[j+k] = 0;
> ssnbr = atoi(s+j);
> s[j+k] = save;

Geez, that block means less code overall, and the prog runs
significantly faster. Where do you mad geniuses come from?

> Having said that, Bart's point still holds. This can be seen as a
> purely arithmetical problem. There is no need for strings conversions
> (and hence atoi) at all. Whether that would make the code faster is not
> obvious, but I suspect it might. I've not have time to try it.

Careful of gotchas. These are all the substrings of 104, including the
leading-zero dropped '04':

[1 10 104 0 4 4]

Note that it's a one-child number because out of all those substrings,
only 0 is evenly divisible by 3 (the length of 104).

>> Thanks for looking at it.
>
> Your code looks a little old fashioned. But even sticking to old C
> standards you could tidy it up a but. For example, it's neater to
> declare int div = 0; in the block that needs it, rather than "resetting"
> at the bottom of the loop.

First I had it just above the j loop, but moved it down to be near its
last use.

Re: Losing my mind: results change with/without printf() statements

<sbjvd3$bgu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17151&group=comp.lang.c#17151

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 1 Jul 2021 10:46:26 +0200
Organization: A noiseless patient Spider
Lines: 123
Message-ID: <sbjvd3$bgu$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 1 Jul 2021 08:46:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c8f8f43462db8e38d0e8cadf1c3650f9";
logging-data="11806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yXv/j0xccHehMpZKzLrfblfvu3a5opgE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:CxUv3GvDoMITlNXUqKx8AbTxBKE=
In-Reply-To: <Yg2DI.3867$mR.1082@fx33.iad>
Content-Language: en-GB
 by: David Brown - Thu, 1 Jul 2021 08:46 UTC

On 30/06/2021 19:58, DFS wrote:
> On 6/30/2021 1:03 PM, David Brown wrote:
>> On 30/06/2021 18:26, DFS wrote:
>>> My program identifies all the 'one-child' numbers in a range (a
>>> one-child number has only one substring evenly divisible by the length
>>> of the number).
>>>
>>> 968 has these substrings: [9, 96, 968, 6, 68, 8]
>>> 9 and 96 and 6 are evenly divisible by 3, so 968 is not a one-child
>>> number
>>>
>>> 5671 has these substrings: [5, 56, 567, 5671, 6, 67, 671, 7, 71, 1]
>>> Only 56 is evenly divisible by 4, so 5671 is a one-child number
>>>
>>> $ prog start end [1|2]
>>> examples
>>> $ prog 1 100 1   (no use of printf(), gives incorrect results)
>>> $ prog 1 100 2   (use printf(), gives correct results)
>>> ================================================================
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <string.h>
>>>
>>> //get substring
>>> char *psubstr(char *string, int pos, int length)
>>> {
>>>     int cnt;
>>>     char *p;
>>>     p = malloc(length+1);
>>>     while (cnt < length)
>>>     {
>>>        *(p+cnt) = *(string+pos);
>>>        string++;
>>>        cnt++;
>>>     }
>>>     *(p+cnt) = '\0';
>>>     return p;
>>> }
>>>
>>
>> What do you see when you compile with warnings enabled?  A quick test
>> with gcc points out that you are using "cnt" here without initialising
>> it - thus the behaviour of the code depends entirely on what might
>> happen to be in registers or the stack when this function is called.
>>
>> Your main() is also going to leak like a sieve, as you allocate memory
>> on each call to psubstr but only deallocate the last one.  And it
>> defines "ss" but does not use it, which is almost certainly a bug.
>>
>> I haven't read the code or attempted to understand it - first pick off
>> the low-lying fruit that can be found without effort.
>
>
>
> using TinyCC 0.9.27 on Windows
>
> $tcc -Wall prog.c -o prog.exe
>
> no warnings whatsoever
>
> (I think you or someone else on clc warned me away from tcc in the past,
> but it's so handy)

That was probably me. Using tcc for development is a silly idea - get a
decent compiler that will help your development. A screwdriver kit from
a Christmas cracker may be handy when you want something very small and
light, but you don't use it for fixing your car.

At the very least, paste your code into <https://godbolt.org> and use
"gcc -O2 -Wall -Wextra" there.

>
>
> I made the fixes you spotted
>
> * initialized cnt to 0
> * free(pss) in the k loop
> * removed the unused char 'ss'
>
>
> and it now works.  Thanks man!
>
>
> I'm gonna guess the uninitialized variable is the culprit... checked...
> that was it.

Yes. The other points were wastage and inefficiency, but would not
affect the correctness.

>
> I'm a little surprised about the performance of this C program, though.
>  A python version (below) that also doesn't print anything until the end
> result is 2x faster for smaller values of 'end'.  But when 'end' is 10^5
> and up, the C code smokes the python.
>

There are many things that can affect performance, and Python can be
surprisingly efficient at times. It's fun to try this kind of thing and
see how you can improve the speeds. (You could try pypy, for example.)

> Later I'll try it on Linux/gcc, where I expect the performance to be
> much better.
>
>
> ================================================================
> import sys
> start = int(sys.argv[1])
> end   = int(sys.argv[2])
> div,ocn = 0,0
> for i in range(start,end+1):
>     istr = str(i)
>     ilen = len(istr)
>     for j in range(0,ilen):
>         for k in range(j+1,ilen+1):
>             if int(istr[j:k]) % ilen == 0:
>                 div+=1
>     if div == 1: ocn+=1
>     div = 0
> print("\n%d one-child numbers from %d to %d" % (ocn,start,end))
> ================================================================
>

Re: Losing my mind: results change with/without printf() statements

<j20rdgpa1d2rfojqtn5rlopjmjsqri879b@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17152&group=comp.lang.c#17152

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Ros...@invalid.invalid (Rosario19)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 01 Jul 2021 10:49:10 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <j20rdgpa1d2rfojqtn5rlopjmjsqri879b@4ax.com>
References: <vW0DI.53$h45.18@fx16.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="fcf05627d2c1f36505bc92f9111c75fb";
logging-data="13172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/EMsCEhXDrewQcpmAC7Ag/DOmY+GhRZU8="
Cancel-Lock: sha1:QDdCp6IlNOBwX2/+Is7x5WMzTUg=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
 by: Rosario19 - Thu, 1 Jul 2021 08:49 UTC

On Wed, 30 Jun 2021 12:26:02 -0400, DFS <nospam@dfs.com> wrote:

>//get substring
>char *psubstr(char *string, int pos, int length)
>{
> int cnt;

if length is 10 and cnt is 1000

> char *p;
> p = malloc(length+1);
> while (cnt < length)
> {
> *(p+cnt) = *(string+pos);
> string++;
> cnt++;
> }
> *(p+cnt) = '\0';

this write one 0 out the memory allocated

if cnt=0 in the start at end of loop cnt=length=3
0 1 2 3 index space memory reserved (4 spaces)
at end of loop cnt=length=3 ok

> return p;
>}

Re: Losing my mind: results change with/without printf() statements

<sbk00a$m0i$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17153&group=comp.lang.c#17153

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 1 Jul 2021 10:56:42 +0200
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <sbk00a$m0i$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk>
<O%aDI.906$tE4.1@fx29.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 1 Jul 2021 08:56:42 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c8f8f43462db8e38d0e8cadf1c3650f9";
logging-data="22546"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jGf+xp1Ed7V6tMTVdzkyYB8/CvPLKAoM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:WnvVZhceaVaDia4BBgYiyuFOZWc=
In-Reply-To: <O%aDI.906$tE4.1@fx29.iad>
Content-Language: en-GB
 by: David Brown - Thu, 1 Jul 2021 08:56 UTC

On 01/07/2021 05:54, DFS wrote:
> On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
>> DFS <nospam@dfs.com> writes:
>>
>>> On 6/30/2021 2:44 PM, Bart wrote:
>> <cut>
>>>> But I suspect that in all cases, what is dominant is the conversion
>>>> from int to text, from text back to int, and applying the mod
>>>> operator. All things that happen outside of the code generated from
>>>> your source.
>>>
>>> I don't understand 'happen outside'.
>>
>> I think he means that your calls to atoi are doing most of the work, so
>> you can't get much more speed from tinkering with the code you actually
>> wrote.
>>
>> That's not quite right in that you can get a good speed-up by not
>> generating all those sub-strings.  You already have the full number in
>> 's' so you can get sub-string numbers by doing this:
>>
>>    char save = s[j+k];
>>    s[j+k] = 0;
>>    ssnbr = atoi(s+j);
>>    s[j+k] = save;
>
>
> Geez, that block means less code overall, and the prog runs
> significantly faster.  Where do you mad geniuses come from?
>

I haven't studied the code (or the problem), but if that change also
removes the call to the function with malloc(), it will help significantly.

Whenever you see "malloc" in your code, think "slow". When you have a
malloc in the middle of your inner loop, think "very slow". Avoid it if
at all possible - and in your original code, it is useless.

To see the cost of malloc, go back to the original code (with the "int
cnt = 0;" fix). Figure out the maximum length (9, for 32-bit int) and
have a single "static char buff[9];". Use that instead of malloc in
psubstr, and measure the difference in time.

>
>> Having said that, Bart's point still holds.  This can be seen as a
>> purely arithmetical problem.  There is no need for strings conversions
>> (and hence atoi) at all.  Whether that would make the code faster is not
>> obvious, but I suspect it might.  I've not have time to try it.
>
> Careful of gotchas.  These are all the substrings of 104, including the
> leading-zero dropped '04':
>
> [1 10 104 0 4 4]
>
> Note that it's a one-child number because out of all those substrings,
> only 0 is evenly divisible by 3 (the length of 104).
>
>
>
>
>>> Thanks for looking at it.
>>
>> Your code looks a little old fashioned.  But even sticking to old C
>> standards you could tidy it up a but.  For example, it's neater to
>> declare int div = 0; in the block that needs it, rather than "resetting"
>> at the bottom of the loop.
>
> First I had it just above the j loop, but moved it down to be near its
> last use.
>
>

Re: Losing my mind: results change with/without printf() statements

<sbk0dc$svp$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17154&group=comp.lang.c#17154

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 1 Jul 2021 11:03:39 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <sbk0dc$svp$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad>
<c57pdgtkv7vfrq69nt5c7kvec00jngfokv@4ax.com> <v64DI.12622$Vj7.1255@fx46.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 1 Jul 2021 09:03:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c8f8f43462db8e38d0e8cadf1c3650f9";
logging-data="29689"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186gtgW2prgAKEzKzg+sUfeaYJU5MqRL9U="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:IZVcxM9B/sU5QjaCV43KtcXX8hc=
In-Reply-To: <v64DI.12622$Vj7.1255@fx46.iad>
Content-Language: en-GB
 by: David Brown - Thu, 1 Jul 2021 09:03 UTC

On 30/06/2021 22:03, DFS wrote:
> On 6/30/2021 2:36 PM, Barry Schwarz wrote:
>> On Wed, 30 Jun 2021 12:26:02 -0400, DFS <nospam@dfs.com> wrote:
>>

>>>
>>> //get substring
>>> char *psubstr(char *string, int pos, int length)
>>> {
>>>     int cnt;
>>>     char *p;
>>>     p = malloc(length+1);
>>>     while (cnt < length)
>>

> So it doesn't seem to be random.

The value of "cnt" here is "indeterminate" - C says nothing about what
it might be. In practice, it will be a left-over from whatever happened
before this function was called. By luck - nothing but luck - the call
to printf had left zero in the register or stack address used by cnt. A
different compiler, or different flags, or different source code could
result in completely different answers.

>
> But since cnt isn't initialized, does the compiler auto-assign a 0 on
> the first call to cnt++?
>

No.

Non-static local data is not automatically initialised.

Re: Losing my mind: results change with/without printf() statements

<8735syjrwf.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17155&group=comp.lang.c#17155

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Thu, 01 Jul 2021 10:38:08 +0100
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <8735syjrwf.fsf@bsb.me.uk>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk>
<O%aDI.906$tE4.1@fx29.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="a2d8b1c21ccbe83bdb1b6c6beabf5900";
logging-data="12360"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18f8qcfxlO387yZp7lGvlB3FPeKxUlW3fc="
Cancel-Lock: sha1:467kTcrLHtUuLOVCEpz5fwpHP/M=
sha1:CEnmRlG9UDW159b60F3t8MesrpU=
X-BSB-Auth: 1.f750ad97a3841c20ad46.20210701103808BST.8735syjrwf.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 1 Jul 2021 09:38 UTC

DFS <nospam@dfs.com> writes:

> On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
>> DFS <nospam@dfs.com> writes:
>>
>>> On 6/30/2021 2:44 PM, Bart wrote:
>> <cut>
>>>> But I suspect that in all cases, what is dominant is the conversion
>>>> from int to text, from text back to int, and applying the mod
>>>> operator. All things that happen outside of the code generated from
>>>> your source.
>>>
>>> I don't understand 'happen outside'.
>> I think he means that your calls to atoi are doing most of the work, so
>> you can't get much more speed from tinkering with the code you actually
>> wrote.
>> That's not quite right in that you can get a good speed-up by not
>> generating all those sub-strings. You already have the full number in
>> 's' so you can get sub-string numbers by doing this:
>> char save = s[j+k];
>> s[j+k] = 0;
>> ssnbr = atoi(s+j);
>> s[j+k] = save;
>
> Geez, that block means less code overall, and the prog runs
> significantly faster. Where do you mad geniuses come from?

Actually there is no genius involved (IMO). It's mostly the result of
having written and (possibly more importantly) read code for more than
40 years. You pick up stuff along the way.

>> Having said that, Bart's point still holds. This can be seen as a
>> purely arithmetical problem. There is no need for strings conversions
>> (and hence atoi) at all. Whether that would make the code faster is not
>> obvious, but I suspect it might. I've not have time to try it.
>
> Careful of gotchas. These are all the substrings of 104, including
> the leading-zero dropped '04':

Whether that's a gotcha or not depends on how you tackle the problem.

I've written an arithmetic version (i.e. no strings) and it is
significantly faster again. I thought it probably would be. You might
want to have a go...

>>> Thanks for looking at it.
>> Your code looks a little old fashioned. But even sticking to old C
>> standards you could tidy it up a but. For example, it's neater to
>> declare int div = 0; in the block that needs it, rather than "resetting"
>> at the bottom of the loop.
>
> First I had it just above the j loop, but moved it down to be near its
> last use.

I'm not sure that's really my point. div is used only in that loop so
this pattern

for (....) {
int count = 0;
....
code that might do count += 1;
....
}

is clearer and better overall than this pattern:

int count = 0;
....
for (....) {
....
code that might do count += 1;
....
count = 0;
}

--
Ben.

Re: Losing my mind: results change with/without printf() statements

<54aa8ed0-7069-44c8-a582-d05aa8541ac8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17168&group=comp.lang.c#17168

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:40ce:: with SMTP id g14mr4583774qko.436.1625219397446; Fri, 02 Jul 2021 02:49:57 -0700 (PDT)
X-Received: by 2002:ae9:de47:: with SMTP id s68mr4540518qkf.39.1625219397306; Fri, 02 Jul 2021 02:49:57 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 2 Jul 2021 02:49:57 -0700 (PDT)
In-Reply-To: <8735syjrwf.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=87.68.182.191; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 87.68.182.191
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me> <Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4> <Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk> <O%aDI.906$tE4.1@fx29.iad> <8735syjrwf.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <54aa8ed0-7069-44c8-a582-d05aa8541ac8n@googlegroups.com>
Subject: Re: Losing my mind: results change with/without printf() statements
From: already5...@yahoo.com (Michael S)
Injection-Date: Fri, 02 Jul 2021 09:49:57 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 77
 by: Michael S - Fri, 2 Jul 2021 09:49 UTC

On Thursday, July 1, 2021 at 12:38:21 PM UTC+3, Ben Bacarisse wrote:
> DFS <nos...@dfs.com> writes:
>
> > On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
> >> DFS <nos...@dfs.com> writes:
> >>
> >>> On 6/30/2021 2:44 PM, Bart wrote:
> >> <cut>
> >>>> But I suspect that in all cases, what is dominant is the conversion
> >>>> from int to text, from text back to int, and applying the mod
> >>>> operator. All things that happen outside of the code generated from
> >>>> your source.
> >>>
> >>> I don't understand 'happen outside'.
> >> I think he means that your calls to atoi are doing most of the work, so
> >> you can't get much more speed from tinkering with the code you actually
> >> wrote.
> >> That's not quite right in that you can get a good speed-up by not
> >> generating all those sub-strings. You already have the full number in
> >> 's' so you can get sub-string numbers by doing this:
> >> char save = s[j+k];
> >> s[j+k] = 0;
> >> ssnbr = atoi(s+j);
> >> s[j+k] = save;
> >
> > Geez, that block means less code overall, and the prog runs
> > significantly faster. Where do you mad geniuses come from?
> Actually there is no genius involved (IMO). It's mostly the result of
> having written and (possibly more importantly) read code for more than
> 40 years. You pick up stuff along the way.
> >> Having said that, Bart's point still holds. This can be seen as a
> >> purely arithmetical problem. There is no need for strings conversions
> >> (and hence atoi) at all. Whether that would make the code faster is not
> >> obvious, but I suspect it might. I've not have time to try it.
> >
> > Careful of gotchas. These are all the substrings of 104, including
> > the leading-zero dropped '04':
> Whether that's a gotcha or not depends on how you tackle the problem.
>
> I've written an arithmetic version (i.e. no strings) and it is
> significantly faster again.

Does it include replacement of division by reciprocal multiplication?

> I thought it probably would be. You might
> want to have a go...
> >>> Thanks for looking at it.
> >> Your code looks a little old fashioned. But even sticking to old C
> >> standards you could tidy it up a but. For example, it's neater to
> >> declare int div = 0; in the block that needs it, rather than "resetting"
> >> at the bottom of the loop.
> >
> > First I had it just above the j loop, but moved it down to be near its
> > last use.
> I'm not sure that's really my point. div is used only in that loop so
> this pattern
>
> for (....) {
> int count = 0;
> ....
> code that might do count += 1;
> ....
> }
>
> is clearer and better overall than this pattern:
>
> int count = 0;
> ....
> for (....) {
> ....
> code that might do count += 1;
> ....
> count = 0;
> }
>
> --
> Ben.

Re: Losing my mind: results change with/without printf() statements

<98224c67-f264-4a56-8436-475792bb8fc4n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17169&group=comp.lang.c#17169

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:a223:: with SMTP id f32mr4482461qva.8.1625221252163;
Fri, 02 Jul 2021 03:20:52 -0700 (PDT)
X-Received: by 2002:ae9:f30c:: with SMTP id p12mr4747019qkg.19.1625221252008;
Fri, 02 Jul 2021 03:20:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 2 Jul 2021 03:20:51 -0700 (PDT)
In-Reply-To: <Gi4DI.1867$t64.414@fx13.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=87.68.182.191; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 87.68.182.191
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4> <Gi4DI.1867$t64.414@fx13.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <98224c67-f264-4a56-8436-475792bb8fc4n@googlegroups.com>
Subject: Re: Losing my mind: results change with/without printf() statements
From: already5...@yahoo.com (Michael S)
Injection-Date: Fri, 02 Jul 2021 10:20:52 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Michael S - Fri, 2 Jul 2021 10:20 UTC

On Wednesday, June 30, 2021 at 11:16:53 PM UTC+3, DFS wrote:
> On 6/30/2021 2:44 PM, Bart wrote:
> > On 30/06/2021 18:58, DFS wrote:
> >> On 6/30/2021 1:03 PM, David Brown wrote:
> >
> >>> What do you see when you compile with warnings enabled? A quick test
> >>> with gcc points out that you are using "cnt" here without initialising
> >>> it - thus the behaviour of the code depends entirely on what might
> >>> happen to be in registers or the stack when this function is called.
> >>>
> >>> Your main() is also going to leak like a sieve, as you allocate memory
> >>> on each call to psubstr but only deallocate the last one. And it
> >>> defines "ss" but does not use it, which is almost certainly a bug.
> >>>
> >>> I haven't read the code or attempted to understand it - first pick off
> >>> the low-lying fruit that can be found without effort.
> >>
> >>
> >>
> >> using TinyCC 0.9.27 on Windows
> >>
> >> $tcc -Wall prog.c -o prog.exe
> >>
> >> no warnings whatsoever
> >>
> >> (I think you or someone else on clc warned me away from tcc in the
> >> past, but it's so handy)
> >
> > You're allowed to use gcc with lots of options from time to time to
> > check that all's well. Then go back to the much faster compiler.
> For my little code it's about a half-second either way.
> >> I made the fixes you spotted
> >>
> >> * initialized cnt to 0
> >> * free(pss) in the k loop
> >> * removed the unused char 'ss'
> >>
> >>
> >> and it now works. Thanks man!
> >
> > I added this at the start of main:
> >
> > if (argc<4) {
> > printf("Usage: %s start end 1/2\n",argv[0]);
> > exit(0);
> > }
> >
> > Otherwise it crashes if run with no inputs.
> Thou must read the instructions:
> $ prog start end [1|2]
> >> I'm a little surprised about the performance of this C program,
> >> though. A python version (below) that also doesn't print anything
> >> until the end result is 2x faster for smaller values of 'end'. But
> >> when 'end' is 10^5 and up, the C code smokes the python.
> >
> >> Later I'll try it on Linux/gcc, where I expect the performance to be
> >> much better.
> >
> > I tried it on N=10,000,000, and gcc-O3 took 41 seconds. PyPy
> > (accelerated version of Python) took 17 seconds.
> Not bad numbers for python and F(10^7), but this code is part of me
> trying to solve Project Euler 413, which asks for F(10^19).
>
> You can't brute force that with a desktop PC.

If you understand that it can't be done by brute force then why are you trying to do just that?
Supposedly, the speed of your code could be improved by factor of 10 or, with a bit of algorithmic
smarts, even by factor of 100. It's still does not bring your to the point where we can tackle the challenge
either with a single desktop PC or with networks of few thousands of 50-core servers.

> > tcc took 56 seconds, only 35% slower than gcc-O3.
> >
> > But I suspect that in all cases, what is dominant is the conversion from
> > int to text, from text back to int, and applying the mod operator. All
> > things that happen outside of the code generated from your source.
> I don't understand 'happen outside'.
>
> Thanks for looking at it.

Re: Losing my mind: results change with/without printf() statements

<sbmqje$q34$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17170&group=comp.lang.c#17170

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Fri, 2 Jul 2021 11:42:45 +0100
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <sbmqje$q34$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad>
<98224c67-f264-4a56-8436-475792bb8fc4n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 2 Jul 2021 10:42:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="43845afab179f91ac4dad940c011128c";
logging-data="26724"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QP5LGMIXUEgIum6SyvJNBhr/BkFQmwwQ="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:AlknImfPWlmeeKfgh+eOnkeEgkI=
In-Reply-To: <98224c67-f264-4a56-8436-475792bb8fc4n@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210702-0, 02/07/2021), Outbound message
 by: Bart - Fri, 2 Jul 2021 10:42 UTC

On 02/07/2021 11:20, Michael S wrote:
> On Wednesday, June 30, 2021 at 11:16:53 PM UTC+3, DFS wrote:
>> On 6/30/2021 2:44 PM, Bart wrote:

>>> I tried it on N=10,000,000, and gcc-O3 took 41 seconds. PyPy
>>> (accelerated version of Python) took 17 seconds.
>> Not bad numbers for python and F(10^7), but this code is part of me
>> trying to solve Project Euler 413, which asks for F(10^19).
>>
>> You can't brute force that with a desktop PC.
>
> If you understand that it can't be done by brute force then why are you trying to do just that?
> Supposedly, the speed of your code could be improved by factor of 10 or, with a bit of algorithmic
> smarts, even by factor of 100. It's still does not bring your to the point where we can tackle the challenge
> either with a single desktop PC or with networks of few thousands of 50-core servers.

It wouldn't get very far using 'int' types either, which limits it to
F(10^9). It would need to use uint64_t (with changes to atoi etc) to
manage F(10^19).

I guess that's why that limit was chosen.

Python of course could go well beyond F10^19), given enough time.

Re: Losing my mind: results change with/without printf() statements

<99f4da00-9206-4702-8ac7-515c5b21af92n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17171&group=comp.lang.c#17171

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:9c85:: with SMTP id f127mr5040720qke.294.1625226469430; Fri, 02 Jul 2021 04:47:49 -0700 (PDT)
X-Received: by 2002:ae9:e911:: with SMTP id x17mr4937631qkf.371.1625226469300; Fri, 02 Jul 2021 04:47:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 2 Jul 2021 04:47:49 -0700 (PDT)
In-Reply-To: <sbmqje$q34$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=87.68.182.191; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 87.68.182.191
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me> <Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4> <Gi4DI.1867$t64.414@fx13.iad> <98224c67-f264-4a56-8436-475792bb8fc4n@googlegroups.com> <sbmqje$q34$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <99f4da00-9206-4702-8ac7-515c5b21af92n@googlegroups.com>
Subject: Re: Losing my mind: results change with/without printf() statements
From: already5...@yahoo.com (Michael S)
Injection-Date: Fri, 02 Jul 2021 11:47:49 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 35
 by: Michael S - Fri, 2 Jul 2021 11:47 UTC

On Friday, July 2, 2021 at 1:43:06 PM UTC+3, Bart wrote:
> On 02/07/2021 11:20, Michael S wrote:
> > On Wednesday, June 30, 2021 at 11:16:53 PM UTC+3, DFS wrote:
> >> On 6/30/2021 2:44 PM, Bart wrote:
>
> >>> I tried it on N=10,000,000, and gcc-O3 took 41 seconds. PyPy
> >>> (accelerated version of Python) took 17 seconds.
> >> Not bad numbers for python and F(10^7), but this code is part of me
> >> trying to solve Project Euler 413, which asks for F(10^19).
> >>
> >> You can't brute force that with a desktop PC.
> >
> > If you understand that it can't be done by brute force then why are you trying to do just that?
> > Supposedly, the speed of your code could be improved by factor of 10 or, with a bit of algorithmic
> > smarts, even by factor of 100. It's still does not bring your to the point where we can tackle the challenge
> > either with a single desktop PC or with networks of few thousands of 50-core servers.
> It wouldn't get very far using 'int' types either, which limits it to
> F(10^9). It would need to use uint64_t (with changes to atoi etc) to
> manage F(10^19).
>
> I guess that's why that limit was chosen.
>
> Python of course could go well beyond F10^19), given enough time.

Read posts of Ben Bacarisse. atai() or similar functions are not needed at all.
If you are a bit smart about algorithms then 64-bit arithmetic is not needed either.
In fact, even 32-bit arithmetic is not necessary.
Suppose, you have a substring "abc...x". You have its reminder r1 already calculated.
Then, for a substring "abc...xy" reminder is equal to (r1*10+x) % nDig.
For nDig<=19 all numbers involved in calculations fit in uint8_t.
As a next step, you can replace modulo operation with table lookup, which would be quicker,
because lookup table is small and easily fits in L1D cache.
But all that wouldn't help to even remotely approach a problem of size 10**19.

Re: Losing my mind: results change with/without printf() statements

<87fswwhlu4.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17172&group=comp.lang.c#17172

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Fri, 02 Jul 2021 14:44:19 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <87fswwhlu4.fsf@bsb.me.uk>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk>
<O%aDI.906$tE4.1@fx29.iad> <8735syjrwf.fsf@bsb.me.uk>
<54aa8ed0-7069-44c8-a582-d05aa8541ac8n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="eebc8e7d3a672789a70c7da0a5748a69";
logging-data="2581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0FaeCt88iWAObpojxQVZyG3PPwkVBGBU="
Cancel-Lock: sha1:OGvzFh5Dg/1alfAuaTCNp0NMrKw=
sha1:bwPjsyRTcBTeN/FXNdxUi4N1QcU=
X-BSB-Auth: 1.e78a05f993bf2ed7163f.20210702144419BST.87fswwhlu4.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 2 Jul 2021 13:44 UTC

Michael S <already5chosen@yahoo.com> writes:

> On Thursday, July 1, 2021 at 12:38:21 PM UTC+3, Ben Bacarisse wrote:
>> DFS <nos...@dfs.com> writes:
>>
>> > On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
>> >> DFS <nos...@dfs.com> writes:
>> >>
>> >>> On 6/30/2021 2:44 PM, Bart wrote:
>> >> <cut>
>> >>>> But I suspect that in all cases, what is dominant is the conversion
>> >>>> from int to text, from text back to int, and applying the mod
>> >>>> operator. All things that happen outside of the code generated from
>> >>>> your source.
>> >>>
>> >>> I don't understand 'happen outside'.
>> >> I think he means that your calls to atoi are doing most of the work, so
>> >> you can't get much more speed from tinkering with the code you actually
>> >> wrote.
>> >> That's not quite right in that you can get a good speed-up by not
>> >> generating all those sub-strings. You already have the full number in
>> >> 's' so you can get sub-string numbers by doing this:
>> >> char save = s[j+k];
>> >> s[j+k] = 0;
>> >> ssnbr = atoi(s+j);
>> >> s[j+k] = save;
>> >
>> > Geez, that block means less code overall, and the prog runs
>> > significantly faster. Where do you mad geniuses come from?
>> Actually there is no genius involved (IMO). It's mostly the result of
>> having written and (possibly more importantly) read code for more than
>> 40 years. You pick up stuff along the way.
>> >> Having said that, Bart's point still holds. This can be seen as a
>> >> purely arithmetical problem. There is no need for strings conversions
>> >> (and hence atoi) at all. Whether that would make the code faster is not
>> >> obvious, but I suspect it might. I've not have time to try it.
>> >
>> > Careful of gotchas. These are all the substrings of 104, including
>> > the leading-zero dropped '04':
>> Whether that's a gotcha or not depends on how you tackle the problem.
>>
>> I've written an arithmetic version (i.e. no strings) and it is
>> significantly faster again.
>
> Does it include replacement of division by reciprocal multiplication?

No, it's a plain integer / and % version. Have you got one that does
something clever with the divisions?

--
Ben.

Re: Losing my mind: results change with/without printf() statements

<87a6n4hk4c.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17174&group=comp.lang.c#17174

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Fri, 02 Jul 2021 15:21:23 +0100
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <87a6n4hk4c.fsf@bsb.me.uk>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="eebc8e7d3a672789a70c7da0a5748a69";
logging-data="20197"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Rl8SJNN3uF3w1sVkgRUMwugjVmkO7N4s="
Cancel-Lock: sha1:c+A0tnEtQsB76GmoGhNiwozeQSY=
sha1:Ip+/q+XHWU7t2tX7pG6sm9vdGO4=
X-BSB-Auth: 1.a82590d6fb9354585042.20210702152123BST.87a6n4hk4c.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 2 Jul 2021 14:21 UTC

DFS <nospam@dfs.com> writes:

> Not bad numbers for python and F(10^7), but this code is part of me
> trying to solve Project Euler 413, which asks for F(10^19).

I didn't know that. As you say, a different algorithm is called for.

(My best naive version gets F(10^9) in 4m21.489s and F(10^7) in 2.177s.)

--
Ben.

Re: Losing my mind: results change with/without printf() statements

<bf39ecc9-f6c8-499a-8fed-c17d725b10fbn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17179&group=comp.lang.c#17179

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:118a:: with SMTP id m10mr105865qtk.186.1625237333945;
Fri, 02 Jul 2021 07:48:53 -0700 (PDT)
X-Received: by 2002:a37:ad14:: with SMTP id f20mr194183qkm.400.1625237333803;
Fri, 02 Jul 2021 07:48:53 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 2 Jul 2021 07:48:53 -0700 (PDT)
In-Reply-To: <87fswwhlu4.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=87.68.182.191; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 87.68.182.191
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87im1vj6q3.fsf@bsb.me.uk> <O%aDI.906$tE4.1@fx29.iad>
<8735syjrwf.fsf@bsb.me.uk> <54aa8ed0-7069-44c8-a582-d05aa8541ac8n@googlegroups.com>
<87fswwhlu4.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bf39ecc9-f6c8-499a-8fed-c17d725b10fbn@googlegroups.com>
Subject: Re: Losing my mind: results change with/without printf() statements
From: already5...@yahoo.com (Michael S)
Injection-Date: Fri, 02 Jul 2021 14:48:53 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Michael S - Fri, 2 Jul 2021 14:48 UTC

On Friday, July 2, 2021 at 4:44:33 PM UTC+3, Ben Bacarisse wrote:
> Michael S <already...@yahoo.com> writes:
>
> > On Thursday, July 1, 2021 at 12:38:21 PM UTC+3, Ben Bacarisse wrote:
> >> DFS <nos...@dfs.com> writes:
> >>
> >> > On 6/30/2021 7:03 PM, Ben Bacarisse wrote:
> >> >> DFS <nos...@dfs.com> writes:
> >> >>
> >> >>> On 6/30/2021 2:44 PM, Bart wrote:
> >> >> <cut>
> >> >>>> But I suspect that in all cases, what is dominant is the conversion
> >> >>>> from int to text, from text back to int, and applying the mod
> >> >>>> operator. All things that happen outside of the code generated from
> >> >>>> your source.
> >> >>>
> >> >>> I don't understand 'happen outside'.
> >> >> I think he means that your calls to atoi are doing most of the work, so
> >> >> you can't get much more speed from tinkering with the code you actually
> >> >> wrote.
> >> >> That's not quite right in that you can get a good speed-up by not
> >> >> generating all those sub-strings. You already have the full number in
> >> >> 's' so you can get sub-string numbers by doing this:
> >> >> char save = s[j+k];
> >> >> s[j+k] = 0;
> >> >> ssnbr = atoi(s+j);
> >> >> s[j+k] = save;
> >> >
> >> > Geez, that block means less code overall, and the prog runs
> >> > significantly faster. Where do you mad geniuses come from?
> >> Actually there is no genius involved (IMO). It's mostly the result of
> >> having written and (possibly more importantly) read code for more than
> >> 40 years. You pick up stuff along the way.
> >> >> Having said that, Bart's point still holds. This can be seen as a
> >> >> purely arithmetical problem. There is no need for strings conversions
> >> >> (and hence atoi) at all. Whether that would make the code faster is not
> >> >> obvious, but I suspect it might. I've not have time to try it.
> >> >
> >> > Careful of gotchas. These are all the substrings of 104, including
> >> > the leading-zero dropped '04':
> >> Whether that's a gotcha or not depends on how you tackle the problem.
> >>
> >> I've written an arithmetic version (i.e. no strings) and it is
> >> significantly faster again.
> >
> > Does it include replacement of division by reciprocal multiplication?
> No, it's a plain integer / and % version. Have you got one that does
> something clever with the divisions?
>
> --
> Ben.

No, I didn't.
Have more interesting things to do.
Besides, brute force is so obvious dead end...

Re: Losing my mind: results change with/without printf() statements

<GoGDI.4118$D71.3961@fx06.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17181&group=comp.lang.c#17181

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx06.iad.POSTED!not-for-mail
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87a6n4hk4c.fsf@bsb.me.uk>
From: nos...@dfs.com (DFS)
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <87a6n4hk4c.fsf@bsb.me.uk>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Lines: 23
Message-ID: <GoGDI.4118$D71.3961@fx06.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Fri, 02 Jul 2021 15:37:10 UTC
Organization: blocknews - www.blocknews.net
Date: Fri, 2 Jul 2021 11:37:09 -0400
X-Received-Bytes: 1715
 by: DFS - Fri, 2 Jul 2021 15:37 UTC

On 7/2/2021 10:21 AM, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
>
>> Not bad numbers for python and F(10^7), but this code is part of me
>> trying to solve Project Euler 413, which asks for F(10^19).
>
> I didn't know that. As you say, a different algorithm is called for.
>
> (My best naive version gets F(10^9) in 4m21.489s and F(10^7) in 2.177s.)

That's very good.

"Each problem has been designed according to a 'one-minute rule', which
means that although it may take several hours to design a successful
algorithm with more difficult problems, an efficient implementation will
allow a solution to be obtained on a modestly powered computer in less
than one minute."

https://projecteuler.net

Re: Losing my mind: results change with/without printf() statements

<0DGDI.11997$NP.2951@fx42.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17182&group=comp.lang.c#17182

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!ecngs!feeder2.ecngs.de!178.20.174.213.MISMATCH!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
From: nos...@dfs.com (DFS)
Subject: Re: Losing my mind: results change with/without printf() statements
Newsgroups: comp.lang.c
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <sbjvd3$bgu$1@dont-email.me>
X-Mozilla-News-Host: news://usnews.blocknews.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <sbjvd3$bgu$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 45
Message-ID: <0DGDI.11997$NP.2951@fx42.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Fri, 02 Jul 2021 15:52:28 UTC
Organization: blocknews - www.blocknews.net
Date: Fri, 2 Jul 2021 11:52:26 -0400
X-Received-Bytes: 2289
 by: DFS - Fri, 2 Jul 2021 15:52 UTC

On 7/1/2021 4:46 AM, David Brown wrote:
> On 30/06/2021 19:58, DFS wrote:

> At the very least, paste your code into <https://godbolt.org> and use
> "gcc -O2 -Wall -Wextra" there.

Incredible site - thanks for the link.

-Wall -Wextra is pretty strict. It even warned of unused 'argc' from
main().

> There are many things that can affect performance, and Python can be
> surprisingly efficient at times. It's fun to try this kind of thing and
> see how you can improve the speeds. (You could try pypy, for example.)

I saw Bart tried pypy and got good results. I tried pypy3 on the below
and got a 13.8x speed increase over python for F(10^6) and F(10^7).

>> ================================================================
>> import sys
>> start = int(sys.argv[1])
>> end   = int(sys.argv[2])
>> div,ocn = 0,0
>> for i in range(start,end+1):
>>     istr = str(i)
>>     ilen = len(istr)
>>     for j in range(0,ilen):
>>         for k in range(j+1,ilen+1):
>>             if int(istr[j:k]) % ilen == 0:
>>                 div+=1
>>     if div == 1: ocn+=1
>>     div = 0
>> print("\n%d one-child numbers from %d to %d" % (ocn,start,end))
>> ================================================================
>>
>

Re: Losing my mind: results change with/without printf() statements

<sbndho$1ip$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17183&group=comp.lang.c#17183

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Losing my mind: results change with/without printf() statements
Date: Fri, 2 Jul 2021 17:06:07 +0100
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <sbndho$1ip$1@dont-email.me>
References: <vW0DI.53$h45.18@fx16.iad> <sbi84p$ajn$1@dont-email.me>
<Yg2DI.3867$mR.1082@fx33.iad> <hY2DI.505463$N5n7.405017@fx05.ams4>
<Gi4DI.1867$t64.414@fx13.iad> <87a6n4hk4c.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 2 Jul 2021 16:06:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="43845afab179f91ac4dad940c011128c";
logging-data="1625"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uhCMM2IBzpZzSuQrhhT2xEzGIf32Frrc="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:H1zyz+FUtOE5uaacNu4cnxCSNZ8=
In-Reply-To: <87a6n4hk4c.fsf@bsb.me.uk>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210702-0, 02/07/2021), Outbound message
 by: Bart - Fri, 2 Jul 2021 16:06 UTC

On 02/07/2021 15:21, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
>
>> Not bad numbers for python and F(10^7), but this code is part of me
>> trying to solve Project Euler 413, which asks for F(10^19).
>
> I didn't know that. As you say, a different algorithm is called for.
>
> (My best naive version gets F(10^9) in 4m21.489s and F(10^7) in 2.177s.)
>

For F(10^7) I got 9.4 seconds with gcc-O3 with the C version below that
uses atoi equivalents. I seem to remember that my machine is quite a bit
slower than yours. Use of u64 rather than int doesn't affect the results.

I started on a version with no text conversions, but it got messy.

---------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned long long u64;

u64 strsubstring(char* s, int length) {
u64 result;
char c;

c=s[length];
s[length]=0;
result=atoll(s);
s[length]=c;
return result;
}

int onechild(u64 n) {
char s[128];
int slen=sprintf(s,"%llu",n);
int count;
u64 m;

count=(n%slen)==0;

for (int w=0; w<(slen-1); ++w) {
for (int i=0; i<slen-w; ++i) {
m=strsubstring(s+i,w+1);
if ((m%slen)==0) {
++count;
if (count>1) return 0;
}
}
}

return count==1;
}

int main(void) {
int count=0;
u64 n=10000000;

for (int i=1; i<n; ++i) {
count+=onechild(i);
}

printf("F(%llu) = %d\n",n, count);
}

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor