Sujet : Re: Suggested method for returning a string from a C program?
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : comp.lang.cDate : 22. Mar 2025, 14:25:43
Autres entêtes
Organisation : Fix this later
Message-ID : <vrmdoo$1cqb$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
On 22/03/2025 08:07, Peter 'Shaggy' Haywood wrote:
<snip>
Here's my solution, for what it's worth:
#include <stdio.h>
unsigned long weird(unsigned long n)
{
printf("%lu", n);
if(n & 1)
{
/* Odd - multiply by 3 & add 1. */
n = n * 3 + 1;
Or you can save yourself a multiplication
n = (n << 2) - (n - 1);
potentially shaving entire picoseconds off the runtime.
}
else
{
/* Even - divide by 2. */
n /= 2;
do
{
n >>= 1;
} while(~(n & 1));
...and there goes another attosecond.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within