Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

RADIO SHACK LEVEL II BASIC READY >_


devel / comp.lang.c / Re: This prog is shortest to solve a complex problem!!

SubjectAuthor
* This prog is shortest to solve a complex problem!!yohan ston
+- Re: This prog is shortest to solve a complex problem!!Stefan Ram
+* Re: This prog is shortest to solve a complex problem!!Stefan Ram
|`- Re: This prog is shortest to solve a complex problem!!Bonita Montero
+- Re: This prog is shortest to solve a complex problem!!Manfred
+- Re: This prog is shortest to solve a complex problem!!Lew Pitcher
`- Re: This prog is shortest to solve a complex problem!!Lew Pitcher

1
This prog is shortest to solve a complex problem!!

<stng74$1297$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!bU2I7+U2Lztfeei1gwcHPg.user.46.165.242.91.POSTED!not-for-mail
From: yohanst...@gmail.com (yohan ston)
Newsgroups: comp.lang.c
Subject: This prog is shortest to solve a complex problem!!
Date: Sun, 6 Feb 2022 03:39:35 +0000
Organization: Aioe.org NNTP Server
Message-ID: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="35111"; posting-host="bU2I7+U2Lztfeei1gwcHPg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Content-Language: en-ca
X-Notice: Filtered by postfilter v. 0.9.2
 by: yohan ston - Sun, 6 Feb 2022 03:39 UTC

No COVID or No dodgy investment scheme in this post!

I think this program is quite neat to solve a complex problem:

<=============================================================>
#include <stdio.h>
#include <math.h>

int main()
{ /*
This program is designed to solve:
sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7
The answer to this problem is: 8738
*/

double sum = 0.0;
double baseNumber = 2.0;

for (int i = 0; i < 8; i += 2)
{
sum += pow(baseNumber, i) * pow(baseNumber, i + 1);
}
printf("The sum is: %.0f", sum);
} <=============================================================>

Re: This prog is shortest to solve a complex problem!!

<neat-20220206050602@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: 6 Feb 2022 04:07:20 GMT
Organization: Stefan Ram
Lines: 18
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <neat-20220206050602@ram.dialup.fu-berlin.de>
References: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de rKznM3kHMpJjULhJZrzc9wT2EX9S8f84aj/Nc+UvCdPxrr
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sun, 6 Feb 2022 04:07 UTC

yohan ston <yohanston5@gmail.com> writes:
>I think this program is quite neat to solve a complex problem:
....
>printf("The sum is: %.0f", sum);

You are writing to a text stream there.

A text stream is an ordered sequence of characters composed
into lines, each line consisting of zero or more characters
plus /a terminating new-line character/. Whether the last
line requires a terminating new-line character is /implementation-
defined/.

You are not writing a terminating new-line character to the
end of your last line. Therefore, your "neat" program might
violate a requirement on some implementations!

Re: This prog is shortest to solve a complex problem!!

<evalprint-20220206054510@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: 6 Feb 2022 04:46:09 GMT
Organization: Stefan Ram
Lines: 44
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <evalprint-20220206054510@ram.dialup.fu-berlin.de>
References: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de oJP3ED8P5S7vs4ubTebkrQg8Dk5dFTWbZkoXBnHUSxIljH
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sun, 6 Feb 2022 04:46 UTC

yohan ston <yohanston5@gmail.com> writes:
>sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7

One could also write an "evalprint" function that takes such
an expression with single-digit numbers, spaces, and the
three operators "+", "*", and "^", evaluates it, and then
prints the result. This would allow one to directly evaluate
the expression as given in the comment quoted above and
thereby minimize the risk of errors that might be introduce
when translating it into a C program manually.

#include <stdio.h>
#include <math.h>
#include <string.h>
double xpow( double const x, double const y ){ return pow( x, y ); }
double xmul( double const x, double const y ){ return x * y; }
double xadd( double const x, double const y ){ return x + y; }
typedef double( *operator )( double, double );
struct ex { char name; operator op; int left; }ex[] =
{ { 0, 0, 0 }, { '^', xpow, 0 },{ '*', xmul, 1 }, { '+', xadd, 1 }};
struct ex lookup( char const name )
{ for( size_t i = 0; i < sizeof ex/sizeof 0[ ex ]; ++i )
if( ex[ i ].name == name )return ex[ i ]; return ex[ 0 ]; }
char const * g = 0; char next( void ){ while( *g == ' ' )++g; }
char get( void ){ next(); return *g++; }
char peek( void ){ next(); return *g; }
char check( char const * const op )
{ return strchr( op, peek() ) ?get(): 0; }
double numeral( void ){ return get() - '0'; }
double parse( char const * const op, double( *next ) () )
{ double result =( *next )();
for( char sym; sym = check( op ); )result = lookup( sym ).op
( result, lookup( sym ).left?( next )(): parse( op, next ));
return result; }
double power( void ){ return parse( "^", numeral ); }
double product( void ){ return parse( "*", power ); }
double sum( void ){ return parse( "+", product ); }
double start( void ){ return sum(); }
void evalprint( char const * expression )
{ g = expression; printf( "%g\n", start() ); }
int main( void )
{ evalprint( "2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7" ); }

Re: This prog is shortest to solve a complex problem!!

<stp6a3$11rb$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: Sun, 6 Feb 2022 20:07:47 +0100
Organization: Aioe.org NNTP Server
Message-ID: <stp6a3$11rb$1@gioia.aioe.org>
References: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="34667"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Sun, 6 Feb 2022 19:07 UTC

On 2/6/2022 4:39 AM, yohan ston wrote:
> No COVID or No dodgy investment scheme in this post!
>
> I think this program is quite neat to solve a complex problem:
>
> <=============================================================>
> #include <stdio.h>
> #include <math.h>
>
> int main()
> {
> /*
> This program is designed to solve:
> sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7
> The answer to this problem is: 8738
> */
>
> double sum = 0.0;
> double baseNumber = 2.0;
>
> for (int i = 0; i < 8; i += 2)
> {
> sum += pow(baseNumber, i) * pow(baseNumber, i + 1);
> }
> printf("The sum is: %.0f", sum);
> }
> <=============================================================>
>

#include <stdio.h>

int main()
{ /*
This program is designed to solve:
sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7
The answer to this problem is: 8738
*/

const int N = 8;
int sum = 0;

for (int i = 0, n = 1; N/2 > i; ++i, n *= 4)
{
sum = sum + 2*n*n;
}

printf("The sum is: %d\n", sum);
}

Apart of compactness, the advantage is full integer arithmetic, and no
pow() call.

Re: This prog is shortest to solve a complex problem!!

<stp6f7$b36$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: Sun, 6 Feb 2022 19:10:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <stp6f7$b36$1@dont-email.me>
References: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 6 Feb 2022 19:10:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="72428d9a3a1e7b8d6456fd62ff96af66";
logging-data="11366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yh5/1aTudQOy+CRvHkbpLM0Lt6+qXiU8="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:PhDnRJoFKwm4p2HG90cjq4vqNHM=
 by: Lew Pitcher - Sun, 6 Feb 2022 19:10 UTC

On Sun, 06 Feb 2022 03:39:35 +0000, yohan ston wrote:

> No COVID or No dodgy investment scheme in this post!
>
> I think this program is quite neat to solve a complex problem:
>
> <=============================================================>
> #include <stdio.h>
> #include <math.h>
>
> int main()
> {
> /*
> This program is designed to solve:
> sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7 The answer to
> this problem is: 8738 */
>
> double sum = 0.0;
> double baseNumber = 2.0;
>
> for (int i = 0; i < 8; i += 2)
> {
> sum += pow(baseNumber, i) * pow(baseNumber, i + 1);
> }
> printf("The sum is: %.0f", sum);
> } <=============================================================>

Nope.
Mine's shorter

--------------------- code -------------------------
#include <stdio.h>

#define POW2(n,m) (1 << ((n)+(m)))

int main(void)
{
unsigned int result = POW2(0,1) + POW2(2,3) + POW2(4,5) + POW2(6,7);

printf("The sum is %u\n",result);

return 0;
}

--------------------- results -------------------------
14:08 $ ./b
The sum is 8738

--
Lew Pitcher
"In Skills, We Trust"

Re: This prog is shortest to solve a complex problem!!

<stp7p1$b36$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: Sun, 6 Feb 2022 19:32:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <stp7p1$b36$2@dont-email.me>
References: <stng74$1297$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 6 Feb 2022 19:32:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="72428d9a3a1e7b8d6456fd62ff96af66";
logging-data="11366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187qNKkzkzIo/wfmbWrDzxeGYBDB4yyFIU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:AgPMgcVHHS69xUQZkqRo8NFdYFs=
 by: Lew Pitcher - Sun, 6 Feb 2022 19:32 UTC

On Sun, 06 Feb 2022 03:39:35 +0000, yohan ston wrote:

> No COVID or No dodgy investment scheme in this post!
>
> I think this program is quite neat to solve a complex problem:
>
> <=============================================================>
> #include <stdio.h>
> #include <math.h>
>
> int main()
> {
> /*
> This program is designed to solve:
> sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7 The answer to
> this problem is: 8738 */
[snip]

This problem can be solved easily by recognizing the equivalences between
calculating a power-of-2 and of leftshifting 1 by a number of bits.

2^0 == 1 == 1 << 0
2^1 == 2 == 1 << 1
2^2 == 4 == 1 << 2
....
2^n == .... 1 << n

So, the individual terms of the equation can be expressed either as
powers of 2, /or/ as leftshifts of 1.

Now, we can express the equation as a sum of terms, each of which is a
power of 2 multiple of a power of 2.

So we can interpret the equation as

(2^0 * 2^1) + ... + (2^6 * 2^7)
or
((1<<0) * (1<<1)) + ... + ((1<<6) * (1<<7))

Now, each term is a power of 2 multiplied by a power of 2, so it's result
will also be a power of 2. If we look at the resulting values, we see
that, in general
((2^n) * (2^m))
can be expressed as
(1 << (n+m))
so the equivalent leftshift equation becomes
(1 << (0+1)) + (1 << (2+3)) + (1 << (4+5)) + (1 << (6+7))

and, we can compute /this/ without loops, exponentiation, or floatingpoint
arithmetic. In fact, we can compute this /statically/ within the compile
and simply output the resulting number.

HTH
--
Lew Pitcher
"In Skills, We Trust"

Re: This prog is shortest to solve a complex problem!!

<stp8at$34i$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: This prog is shortest to solve a complex problem!!
Date: Sun, 6 Feb 2022 20:42:18 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <stp8at$34i$1@dont-email.me>
References: <stng74$1297$1@gioia.aioe.org>
<evalprint-20220206054510@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 6 Feb 2022 19:42:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7c0946bd22da94fd85fda0b46fdca001";
logging-data="3218"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kzuzagr/SjiVwgRZQ3bytpmfHpbOT/wg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:qP/bLGLRtRGmAcl+/IhXg1MTB0M=
In-Reply-To: <evalprint-20220206054510@ram.dialup.fu-berlin.de>
Content-Language: de-DE
 by: Bonita Montero - Sun, 6 Feb 2022 19:42 UTC

Am 06.02.2022 um 05:46 schrieb Stefan Ram:
> yohan ston <yohanston5@gmail.com> writes:
>> sum = 2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7
>
> One could also write an "evalprint" function that takes such
> an expression with single-digit numbers, spaces, and the
> three operators "+", "*", and "^", evaluates it, and then
> prints the result. This would allow one to directly evaluate
> the expression as given in the comment quoted above and
> thereby minimize the risk of errors that might be introduce
> when translating it into a C program manually.
>
> #include <stdio.h>
> #include <math.h>
> #include <string.h>
> double xpow( double const x, double const y ){ return pow( x, y ); }
> double xmul( double const x, double const y ){ return x * y; }
> double xadd( double const x, double const y ){ return x + y; }
> typedef double( *operator )( double, double );
> struct ex { char name; operator op; int left; }ex[] =
> { { 0, 0, 0 }, { '^', xpow, 0 },{ '*', xmul, 1 }, { '+', xadd, 1 }};
> struct ex lookup( char const name )
> { for( size_t i = 0; i < sizeof ex/sizeof 0[ ex ]; ++i )
> if( ex[ i ].name == name )return ex[ i ]; return ex[ 0 ]; }
> char const * g = 0; char next( void ){ while( *g == ' ' )++g; }
> char get( void ){ next(); return *g++; }
> char peek( void ){ next(); return *g; }
> char check( char const * const op )
> { return strchr( op, peek() ) ?get(): 0; }
> double numeral( void ){ return get() - '0'; }
> double parse( char const * const op, double( *next ) () )
> { double result =( *next )();
> for( char sym; sym = check( op ); )result = lookup( sym ).op
> ( result, lookup( sym ).left?( next )(): parse( op, next ));
> return result; }
> double power( void ){ return parse( "^", numeral ); }
> double product( void ){ return parse( "*", power ); }
> double sum( void ){ return parse( "+", product ); }
> double start( void ){ return sum(); }
> void evalprint( char const * expression )
> { g = expression; printf( "%g\n", start() ); }
> int main( void )
> { evalprint( "2^0 * 2^1 + 2^2 * 2^3 + 2^4 * 2^5 + 2^6 * 2^7" ); }
>

Syntax-castastrophy !

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor