Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

You will be successful in your work.


devel / comp.lang.python / Re: on writing a number as 2^s * q, where q is odd

SubjectAuthor
* on writing a number as 2^s * q, where q is oddJulieta Shem
+* Re: on writing a number as 2^s * q, where q is oddDom Grigonis
|`- Re: on writing a number as 2^s * q, where q is oddjak
+- Re: on writing a number as 2^s * q, where q is odd2QdxY4RzWzUUiLuE
`* Re: on writing a number as 2^s * q, where q is oddAlan Bawden
 `* Re: on writing a number as 2^s * q, where q is oddjak
  `* Re: on writing a number as 2^s * q, where q is oddAlan Bawden
   `* Re: on writing a number as 2^s * q, where q is oddJulieta Shem
    +* Re: on writing a number as 2^s * q, where q is oddjak
    |`* Re: on writing a number as 2^s * q, where q is oddJulieta Shem
    | `- Re: on writing a number as 2^s * q, where q is oddjak
    `* Re: on writing a number as 2^s * q, where q is oddOscar Benjamin
     `* Re: on writing a number as 2^s * q, where q is oddjak
      `* Re: on writing a number as 2^s * q, where q is oddAlan Bawden
       `- Re: on writing a number as 2^s * q, where q is oddjak

1
on writing a number as 2^s * q, where q is odd

<87sf4oysry.fsf@yaxenu.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jsh...@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.python
Subject: on writing a number as 2^s * q, where q is odd
Date: Wed, 29 Nov 2023 21:44:01 -0300
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <87sf4oysry.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="451e210e57316e96edf58375b3333a5c";
logging-data="1115053"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/t/h6x9nnKxHTYzMZvqXmiTTIrMLopJ6Q="
Cancel-Lock: sha1:hl4ZysNa8NI1Psa9SnTRir4DIm0=
sha1:EhHrF3tjbwfa1kyR4FT0ucKxiGQ=
 by: Julieta Shem - Thu, 30 Nov 2023 00:44 UTC

How would you write this procedure?

--8<---------------cut here---------------start------------->8---
def powers_of_2_in(n):
s = 0
while "I still find factors of 2 in n...":
q, r = divmod(n, 2)
if r == 0:
s = s + 1
n = n // 2
else:
return s, n
--8<---------------cut here---------------end--------------->8---

Re: on writing a number as 2^s * q, where q is odd

<mailman.320.1701309988.3828.python-list@python.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: dom.grig...@gmail.com (Dom Grigonis)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Thu, 30 Nov 2023 04:06:24 +0200
Lines: 26
Message-ID: <mailman.320.1701309988.3828.python-list@python.org>
References: <87sf4oysry.fsf@yaxenu.org>
<0C8F1949-D992-4A8D-A8EB-8384C6A208A9@gmail.com>
Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\))
Content-Type: text/plain;
charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de aypTNaJ8Ufxfeyt+QcEy1QOZ0NSOzZ37ko9Uk3BcKJhw==
Cancel-Lock: sha1:XdGXXYKGOESYFUxRBOgX6qSJFqI= sha256:qNLqX/BuyDuaYXamPkDpcWTi1i3xJF+jT26f65O0EdE=
Return-Path: <dom.grigonis@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=DoA3Xxh2;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'cc:addr
:python-list': 0.09; 'else:': 0.09; 'subject:writing': 0.09;
'url:mailman': 0.15; 'cc:name:python': 0.16; 'received:apple':
0.16; 'received:smtpclient.apple': 0.16; 'skip:h 40': 0.16;
'wrote:': 0.16; 'message-id:@gmail.com': 0.18;
'cc:addr:python.org': 0.20; 'skip:p 30': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'email addr:python.org&gt;': 0.28; 'url-ip:188/8': 0.31; 'python-
list': 0.32; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'still': 0.40; 'non': 0.75;
'factors': 0.76; 'email name:&lt;python-list': 0.84;
'received:88': 0.84; 'subject:^': 0.84; 'subject:where': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1701309986; x=1701914786; darn=python.org;
h=references:to:cc:in-reply-to:date:subject:mime-version:message-id
:from:from:to:cc:subject:date:message-id:reply-to;
bh=HOc77a2FNeWiHxFotzuV+mKViBFYuRV9a2WZ8tiBJU8=;
b=DoA3Xxh2yay6po8D8rKVfJxXkQMr0jgPzQqFygKXl4WqpfO3/NdW/UQAvDvS6Dh1Is
a34mJOWnycWpgwfOS6y67LzfoP9RFFxgDk4xPQHVxIHBaBQ6Wxm5delBhhf90Ff1AKd9
59p4CdJBIMiChOety1QfCAveZY6ZswjbYxHYdhzcvCER42yFPT9xPmDq8xHqErkaXkhH
O8CjYVW4eSDOt6tAxJnIt6OP1W7zmL3Z5jJXo5VzBWDYX5hoqId/2AziCfbsQ4Uh9fnI
3H5+F7zIKtwaminletWn/h9h5nxj8zlVkLVuHfrUkYon0UFSA/vLqw+LJOlQxpPyd6Zl
+u2w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1701309986; x=1701914786;
h=references:to:cc:in-reply-to:date:subject:mime-version:message-id
:from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=HOc77a2FNeWiHxFotzuV+mKViBFYuRV9a2WZ8tiBJU8=;
b=QwHj0+/wKsVXBv+U4EpiFB9lYIAwQytSj2UJHasqpIYqVQ+mbAf+SCoz/2DWnwOwVL
g+pWZPdObgYSWsk6+NqujHMB3wDNbN6mddr7Mv3Nf4Vfajo7K0pWTo2BMFRrRCVvXNrl
hwwFjjfSCxul/hMUpgOahK5z0XyzWINWpwVbb+zuXcoc4bV0Fb57uzGCj4fQY7xbUg9L
kD4plzAVsKH0jfNE6p3nRDSPtpxzsLBYgwZhJVjMv476TZONJ+/Bm2BQcwwSz4tKeV3Z
NJxK+BqHIVGLWB4sYHoZ//9EhiEkBaXtE0IamPgsvdrzgNS439zDBQPCDNRa4K50th7u
DwHw==
X-Gm-Message-State: AOJu0YwLWp9/X5rUThGxuu2jhJGfVdRSWMEVgOvK6l56zT+UKBSC54Lr
mOWxkAZzJ4B0daORdJglTmK9BFkKTRc=
X-Google-Smtp-Source: AGHT+IH8CBzjoL9CKBFbQ4ODWU2lWCk5FlwmpJ5i3HFO3azKJOJ5ME7ZS5D7r/JpRtFW8Sdm9Z7IRw==
X-Received: by 2002:a17:907:9496:b0:9d3:f436:6809 with SMTP id
dm22-20020a170907949600b009d3f4366809mr16196303ejc.39.1701309986364;
Wed, 29 Nov 2023 18:06:26 -0800 (PST)
In-Reply-To: <87sf4oysry.fsf@yaxenu.org>
X-Mailer: Apple Mail (2.3654.120.0.1.15)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0C8F1949-D992-4A8D-A8EB-8384C6A208A9@gmail.com>
X-Mailman-Original-References: <87sf4oysry.fsf@yaxenu.org>
 by: Dom Grigonis - Thu, 30 Nov 2023 02:06 UTC

def powers_of_2_in(n):
s = 0
while n % 2 == 0:
s += 1
n = n // 2
return s, n

> On 30 Nov 2023, at 02:44, Julieta Shem via Python-list <python-list@python.org> wrote:
>
> How would you write this procedure?
>
> --8<---------------cut here---------------start------------->8---
> def powers_of_2_in(n):
> s = 0
> while "I still find factors of 2 in n...":
> q, r = divmod(n, 2)
> if r == 0:
> s = s + 1
> n = n // 2
> else:
> return s, n
> --8<---------------cut here---------------end--------------->8---
> --
> https://mail.python.org/mailman/listinfo/python-list

Re: on writing a number as 2^s * q, where q is odd

<mailman.321.1701311506.3828.python-list@python.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4Rz...@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Wed, 29 Nov 2023 21:10:26 -0500
Lines: 18
Message-ID: <mailman.321.1701311506.3828.python-list@python.org>
References: <87sf4oysry.fsf@yaxenu.org>
<ZWfvEq3RlL-gk0i7@anomaly>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de U4ZZ0YVzee1CHLlyHekp4Q2a1GgoIrfFlM+cnHH/fViA==
Cancel-Lock: sha1:53+ELwFbDcWyEpGBa6xBsohAAbk= sha256:yN/KY5qChAiISSVkmVE7GNYOubZP1TH7A5EnVdtqaN8=
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=potatochowder.com header.i=@potatochowder.com
header.b=CMbVpFOw; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'else:': 0.09;
'received:78': 0.09; 'subject:writing': 0.09;
'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com':
0.16; 'received:136.243': 0.16; 'received:172.58': 0.16;
'received:78.46': 0.16; 'received:78.46.172': 0.16;
'received:78.46.172.2': 0.16; 'received:sslproxy05.your-
server.de': 0.16; 'received:www458.your-server.de': 0.16;
'received:your-server.de': 0.16; 'wrote:': 0.16; 'to:addr:python-
list': 0.20; "what's": 0.22; 'received:de': 0.23; 'cc:2**0': 0.25;
'wrong': 0.28; 'python-list': 0.32; 'received:136': 0.32; 'header
:In-Reply-To:1': 0.34; 'still': 0.40; 'factors': 0.76;
'subject:^': 0.84; 'subject:where': 0.84
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=potatochowder.com; s=default2305; h=In-Reply-To:Content-Type:MIME-Version:
References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:
Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:
Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID;
bh=R2yIJsGJlr00fpmPp7NtMdvgWP+koGvfWj+hne0hFEE=; b=CMbVpFOwYR/WLleCvvIbrqyk2R
FIqX/Ma0A3XuCS0RnV3A2T/CSznuf0bxXCgGbCxRfIMj+SGOAcPrl9O9/PMxBEyi/eLs1ej0cbwJQ
kWqjCgaJrHustvyWLMVuOHKVbRyH5+P4kW3v2m5IMNihdOv5J8tro9Qmrv5pNNqqZwGxsB1xNHteJ
/A6jk32y6E2+08PU2YPmdKAvDoqG2Os8qD4Jpfeq6euqL8NBDBR2MOIn0KdZuOcNKGTUwV2ZgKQRe
skb7L+C30DQTIPZvJqWfQ2jHpdhXkMTiknFXCTGIoietr0SQR6gd/fjUIyPPj+0UEqLOKw0m7eaoS
6gseU6dA==;
Mail-Followup-To: python-list@python.org, Julieta Shem <jshem@yaxenu.org>
Content-Disposition: inline
In-Reply-To: <87sf4oysry.fsf@yaxenu.org>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.10/27108/Wed Nov 29 09:40:15 2023)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ZWfvEq3RlL-gk0i7@anomaly>
X-Mailman-Original-References: <87sf4oysry.fsf@yaxenu.org>
 by: 2QdxY4Rz...@potatochowder.com - Thu, 30 Nov 2023 02:10 UTC

On 2023-11-29 at 21:44:01 -0300,
Julieta Shem via Python-list <python-list@python.org> wrote:

> How would you write this procedure?
>
> --8<---------------cut here---------------start------------->8---
> def powers_of_2_in(n):
> s = 0
> while "I still find factors of 2 in n...":
> q, r = divmod(n, 2)
> if r == 0:
> s = s + 1
> n = n // 2
> else:
> return s, n
> --8<---------------cut here---------------end--------------->8---

What's wrong with what you have?

Re: on writing a number as 2^s * q, where q is odd

<86leagt1d9.fsf@williamsburg.bawden.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bawden.eternal-september.org!.POSTED!not-for-mail
From: ala...@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Wed, 29 Nov 2023 21:34:58 -0500
Organization: ITS Preservation Society
Lines: 8
Message-ID: <86leagt1d9.fsf@williamsburg.bawden.org>
References: <87sf4oysry.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: bawden.eternal-september.org; posting-host="1807cd7f01b19210e6a02c06591f3507";
logging-data="1140226"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eK3CVK2uFZFd6mTd0dIVT"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:WiJF6DbEQ3tJm9DZFjVniTxrbcA=
sha1:dlW+6PRldEamw1JElPrwytjnM70=
 by: Alan Bawden - Thu, 30 Nov 2023 02:34 UTC

Julieta Shem <jshem@yaxenu.org> writes:

How would you write this procedure?
def powers_of_2_in(n):
...

def powers_of_2_in(n):
return (n ^ (n - 1)).bit_count() - 1

Re: on writing a number as 2^s * q, where q is odd

<uk94pq$181gr$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Thu, 30 Nov 2023 05:58:34 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <uk94pq$181gr$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<0C8F1949-D992-4A8D-A8EB-8384C6A208A9@gmail.com>
<mailman.320.1701309988.3828.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 30 Nov 2023 04:58:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b4e1e4c77312758513009fd3e75e674c";
logging-data="1312283"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18eFQIqAUZrUBCaur3R6pxi"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:DhOLdMI1IyUls9iL+ssW7xIWijM=
In-Reply-To: <mailman.320.1701309988.3828.python-list@python.org>
 by: jak - Thu, 30 Nov 2023 04:58 UTC

Dom Grigonis ha scritto:
> def powers_of_2_in(n):
> s = 0
> while n % 2 == 0:
> s += 1
> n = n // 2
> return s, n
>

Good solution, unfortunately if the input data is zero, the function
never ends.

>> On 30 Nov 2023, at 02:44, Julieta Shem via Python-list <python-list@python.org> wrote:
>>
>> How would you write this procedure?
>>
>> --8<---------------cut here---------------start------------->8---
>> def powers_of_2_in(n):
>> s = 0
>> while "I still find factors of 2 in n...":
>> q, r = divmod(n, 2)
>> if r == 0:
>> s = s + 1
>> n = n // 2
>> else:
>> return s, n
>> --8<---------------cut here---------------end--------------->8---
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>

Re: on writing a number as 2^s * q, where q is odd

<uk959k$185nm$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Thu, 30 Nov 2023 06:07:00 +0100
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <uk959k$185nm$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 30 Nov 2023 05:07:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b4e1e4c77312758513009fd3e75e674c";
logging-data="1316598"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18u3+QvSK73fZgHbkkR7uhw"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:0VGCDARsRYsMGD6UDHKHIssy284=
In-Reply-To: <86leagt1d9.fsf@williamsburg.bawden.org>
 by: jak - Thu, 30 Nov 2023 05:07 UTC

Alan Bawden ha scritto:
> Julieta Shem <jshem@yaxenu.org> writes:
>
> How would you write this procedure?
> def powers_of_2_in(n):
> ...
>
> def powers_of_2_in(n):
> return (n ^ (n - 1)).bit_count() - 1
>

Great solution, unfortunately the return value is not a tuple as in the
OP version. Maybe in this way?

def powers_of_2_inB(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, int(n / (1 << bc))

Re: on writing a number as 2^s * q, where q is odd

<86h6l3u55e.fsf@williamsburg.bawden.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bawden.eternal-september.org!.POSTED!not-for-mail
From: ala...@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Thu, 30 Nov 2023 01:27:57 -0500
Organization: ITS Preservation Society
Lines: 25
Message-ID: <86h6l3u55e.fsf@williamsburg.bawden.org>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: bawden.eternal-september.org; posting-host="1807cd7f01b19210e6a02c06591f3507";
logging-data="1334771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nuqNXZCYfXGV5a2lOawSg"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:gwVFSuFvyu1UFG3H06S4XI1Lr9E=
sha1:f1POhLRhC+Et9VevNiKL9eFHOuY=
 by: Alan Bawden - Thu, 30 Nov 2023 06:27 UTC

jak <nospam@please.ty> writes:

Alan Bawden ha scritto:
> Julieta Shem <jshem@yaxenu.org> writes:
>
> How would you write this procedure?
> def powers_of_2_in(n):
> ...
>
> def powers_of_2_in(n):
> return (n ^ (n - 1)).bit_count() - 1
>

Great solution, unfortunately the return value is not a tuple as in the
OP version. Maybe in this way?

def powers_of_2_inB(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, int(n / (1 << bc))

Good point. I overlooked that. I should have written:

def powers_of_2_in(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, n >> bc

Re: on writing a number as 2^s * q, where q is odd

<87sf4k2ex5.fsf@yaxenu.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jsh...@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Sat, 02 Dec 2023 23:33:26 -0300
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <87sf4k2ex5.fsf@yaxenu.org>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="516c32e4f9c735e483605e0a1343ae9a";
logging-data="2725580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WPhMoH/adqbcm+CLp0jVBXskEwVAqT04="
Cancel-Lock: sha1:xpw6AmXMBikRlqANDzM2Fqds4CU=
sha1:FicdJrCB5SRcPN1F3cgSWKr8NO4=
 by: Julieta Shem - Sun, 3 Dec 2023 02:33 UTC

Alan Bawden <alan@csail.mit.edu> writes:

> jak <nospam@please.ty> writes:
>
> Alan Bawden ha scritto:
> > Julieta Shem <jshem@yaxenu.org> writes:
> >
> > How would you write this procedure?
> > def powers_of_2_in(n):
> > ...
> >
> > def powers_of_2_in(n):
> > return (n ^ (n - 1)).bit_count() - 1
> >
>
> Great solution, unfortunately the return value is not a tuple as in the
> OP version. Maybe in this way?
>
> def powers_of_2_inB(n):
> bc = (n ^ (n - 1)).bit_count() - 1
> return bc, int(n / (1 << bc))
>
> Good point. I overlooked that. I should have written:
>
> def powers_of_2_in(n):
> bc = (n ^ (n - 1)).bit_count() - 1
> return bc, n >> bc

That's pretty fancy and likely the fastest.

I was pretty happy with a recursive version. If I'm not mistaken,
nobody has offered a recursive version so far. It's my favorite
actually because it seems to be the clearest one.

--8<---------------cut here---------------start------------->8---
def powers_of_2_in(n):
if remainder(n, 2) != 0:
return 0, n
else:
s, r = powers_of_2_in(n // 2)
return 1 + s, r
--8<---------------cut here---------------end--------------->8---

Re: on writing a number as 2^s * q, where q is odd

<ukgvp3$2npse$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Sun, 3 Dec 2023 05:21:56 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <ukgvp3$2npse$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org> <uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 3 Dec 2023 04:21:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c5d235450084ff266a1f4ed9a2e666fb";
logging-data="2877326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2PCZzhGwMDNkYroFngU2l"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:A/UzXIFqTks/FlPBM/xppwSKoi4=
In-Reply-To: <87sf4k2ex5.fsf@yaxenu.org>
 by: jak - Sun, 3 Dec 2023 04:21 UTC

Julieta Shem ha scritto:
> Alan Bawden <alan@csail.mit.edu> writes:
>
>> jak <nospam@please.ty> writes:
>>
>> Alan Bawden ha scritto:
>> > Julieta Shem <jshem@yaxenu.org> writes:
>> >
>> > How would you write this procedure?
>> > def powers_of_2_in(n):
>> > ...
>> >
>> > def powers_of_2_in(n):
>> > return (n ^ (n - 1)).bit_count() - 1
>> >
>>
>> Great solution, unfortunately the return value is not a tuple as in the
>> OP version. Maybe in this way?
>>
>> def powers_of_2_inB(n):
>> bc = (n ^ (n - 1)).bit_count() - 1
>> return bc, int(n / (1 << bc))
>>
>> Good point. I overlooked that. I should have written:
>>
>> def powers_of_2_in(n):
>> bc = (n ^ (n - 1)).bit_count() - 1
>> return bc, n >> bc
>
> That's pretty fancy and likely the fastest.
>
> I was pretty happy with a recursive version. If I'm not mistaken,
> nobody has offered a recursive version so far. It's my favorite
> actually because it seems to be the clearest one.
>
> --8<---------------cut here---------------start------------->8---
> def powers_of_2_in(n):
> if remainder(n, 2) != 0:
> return 0, n
> else:
> s, r = powers_of_2_in(n // 2)
> return 1 + s, r
> --8<---------------cut here---------------end--------------->8---
>

for n = 0 your function get stack overflow

Re: on writing a number as 2^s * q, where q is odd

<87h6kz3maw.fsf@yaxenu.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jsh...@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Sun, 03 Dec 2023 02:08:39 -0300
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <87h6kz3maw.fsf@yaxenu.org>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
<ukgvp3$2npse$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="516c32e4f9c735e483605e0a1343ae9a";
logging-data="2885914"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195NxI1MbSLnLrrMz9t2gtOOQ+Kv19khwc="
Cancel-Lock: sha1:A96fvsdLVu3Zd4w17N/VgxGAn3o=
sha1:/mmfgzHAX9BtOFi5WwVxvWSIhDw=
 by: Julieta Shem - Sun, 3 Dec 2023 05:08 UTC

jak <nospam@please.ty> writes:

[...]

>> --8<---------------cut here---------------start------------->8---
>> def powers_of_2_in(n):
>> if remainder(n, 2) != 0:
>> return 0, n
>> else:
>> s, r = powers_of_2_in(n // 2)
>> return 1 + s, r
>> --8<---------------cut here---------------end--------------->8---
>
> for n = 0 your function get stack overflow

That's right. Let's say ``assert n > 0'' before we say ``if''.

Re: on writing a number as 2^s * q, where q is odd

<ukh723$2ol5i$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Sun, 3 Dec 2023 07:26:11 +0100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <ukh723$2ol5i$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org> <uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
<ukgvp3$2npse$1@dont-email.me> <87h6kz3maw.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 3 Dec 2023 06:26:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c5d235450084ff266a1f4ed9a2e666fb";
logging-data="2905266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rTv+jXdO4kMUYh8wWlN17"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:boOqIr9iygzwhAWE+fQn4ygeuMg=
In-Reply-To: <87h6kz3maw.fsf@yaxenu.org>
 by: jak - Sun, 3 Dec 2023 06:26 UTC

Julieta Shem ha scritto:
> jak <nospam@please.ty> writes:
>
> [...]
>
>>> --8<---------------cut here---------------start------------->8---
>>> def powers_of_2_in(n):
>>> if remainder(n, 2) != 0:
>>> return 0, n
>>> else:
>>> s, r = powers_of_2_in(n // 2)
>>> return 1 + s, r
>>> --8<---------------cut here---------------end--------------->8---
>>
>> for n = 0 your function get stack overflow
>
> That's right. Let's say ``assert n > 0'' before we say ``if''.
>

....or just:

....
if n == 0 or remainder(n, 2) != 0:
....

Re: on writing a number as 2^s * q, where q is odd

<mailman.322.1701608830.3828.python-list@python.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: oscar.j....@gmail.com (Oscar Benjamin)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Sun, 3 Dec 2023 13:06:56 +0000
Lines: 52
Message-ID: <mailman.322.1701608830.3828.python-list@python.org>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me> <86h6l3u55e.fsf@williamsburg.bawden.org>
<87sf4k2ex5.fsf@yaxenu.org>
<CAHVvXxSMyRjXBaiqwfE9uQCm8RGaMKgmMFDts5eoAz8HVhPubg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de phRlQ2PjIuoD78Vb+Z/j/gRa6WtvTktcOxq8GzwtgPQg==
Cancel-Lock: sha1:QAQuxeYYWuPHOX4n0PrCVqUFD7M= sha256:XvOjjSVz+IeHH7sDSdB9qmgr3oN25se/+Z4lyJojQYI=
Return-Path: <oscar.j.benjamin@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=X31qgXTi;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'def': 0.04; 'variable': 0.05; '2023': 0.07; 'hitting': 0.07;
'sun,': 0.07; 'subject:writing': 0.09; 'writes:': 0.09; 'steps':
0.11; 'url:github': 0.14; 'url-ip:140/8': 0.15; 'bits': 0.16;
'integer': 0.16; 'linear': 0.16; 'redundant': 0.16; 'relatively':
0.16; 'shifts': 0.16; 'size.': 0.16; 'wrote:': 0.16; 'instead':
0.17; 'to:addr:python-list': 0.20; 'code': 0.23; 'depends': 0.25;
'library': 0.26; 'bit': 0.27; 'expect': 0.28; 'dec': 0.31; 'half':
0.32; 'python-list': 0.32; 'message-id:@mail.gmail.com': 0.32;
'but': 0.32; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'could': 0.38; 'list': 0.39;
'use': 0.39; 'alan': 0.40; 'exact': 0.40; 'likely': 0.61; 'here':
0.62; 'skip:b 10': 0.63; 'probability': 0.64; 'generally': 0.67;
'operations': 0.68; 'further': 0.69; 'addition': 0.71; 'low':
0.74; 'man': 0.74; 'implemented': 0.76; 'potentially': 0.76;
'exp': 0.84; 'oscar': 0.84; 'quadratic': 0.84; 'subject:^': 0.84;
'subject:where': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1701608828; x=1702213628; darn=python.org;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=lGPJm6z5VTx0Mms72VQeFuDlEUZxDmSZylQmfRFSqRg=;
b=X31qgXTivYjuTZ8Hy6LFEb0ropXaOV5uwUXcrkHhFzcske9h8Mv6g6LhsNX1fmlNX1
bu55P8hSuXVKKf7Yd/4VFu1hyVYvTyxJdWwft7p84y+dNlg7veMbQWGG4hY4n5cJdcgL
Jiu78LwKz0w8pk+dlMFFQULGWWDxnCp0TalftCFkJ3JM62nnoEhCnRXQj3GHd/AS2hLd
n0PNOe+em0Iwkgu4oKbU+g6sKVv8OGlTLUJW5PzkwvQLyzszkVJg09jwfqEpZz3U3fdt
T+n99yCQFtlMWHk01+bnphUntE7wG5CRab0k9aDMWgaMt2jGV1eC9+IbOFXief8L/+ET
+zWw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1701608828; x=1702213628;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=lGPJm6z5VTx0Mms72VQeFuDlEUZxDmSZylQmfRFSqRg=;
b=FeNNH6HCuhUofEeHZ+vMB2uhgeOkPyWb2xZnv8VM3I/EjG0DOkTgfPfeWfhCIPPb3m
M2BuCB7HBBIuDmgZwAVxH/GWvIjReI/75qWytQL6qNRQYlJGkFmqBHOhgWAJLkagmLwI
1j1ePFUmyZKNNG9mWg5bRv0hvYVvK9Xxy6kPHTKEksVL+dhbZCiwj8//Us4sI+n3RHyn
zEVQm+OPn//p55Q4hj9kMIYIJd9GByNMBxH30Dss7VAs4CvxVJzYEN5xlC8iJy9WqyC2
hPsDV88oGEjVkGNbUYHryeImwwpat1PRgEzwvk675rxzW8PfBd0423DE4QbuXDESeKAM
57Ow==
X-Gm-Message-State: AOJu0YxGbE7RV9pxzwAqyHKc2J7uofUXtItOKCPV0+Y3eT7LJ3f/DCbB
/JyjLoVtCUsQ2QHcnBLwldbXHn8HDI1Zt1OQ4c0cY8no198=
X-Google-Smtp-Source: AGHT+IHUJPcOt3FQW9hSo6rHEDifb3o1D3FLuEgEcGGUlfYh6j0PK77P0veSEFoBddOFxTeqiAq+6E4hRawNA3ZIQrY=
X-Received: by 2002:a19:a408:0:b0:50b:d764:8817 with SMTP id
q8-20020a19a408000000b0050bd7648817mr1824022lfc.99.1701608827567; Sun, 03 Dec
2023 05:07:07 -0800 (PST)
In-Reply-To: <87sf4k2ex5.fsf@yaxenu.org>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAHVvXxSMyRjXBaiqwfE9uQCm8RGaMKgmMFDts5eoAz8HVhPubg@mail.gmail.com>
X-Mailman-Original-References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me> <86h6l3u55e.fsf@williamsburg.bawden.org>
<87sf4k2ex5.fsf@yaxenu.org>
 by: Oscar Benjamin - Sun, 3 Dec 2023 13:06 UTC

On Sun, 3 Dec 2023 at 10:25, Julieta Shem via Python-list
<python-list@python.org> wrote:
>
> Alan Bawden <alan@csail.mit.edu> writes:
> >
> > def powers_of_2_in(n):
> > bc = (n ^ (n - 1)).bit_count() - 1
> > return bc, n >> bc
>
> That's pretty fancy and likely the fastest.

It might be the fastest but it depends how big you expect n to be and
how many trailing zeros you expect. If n is a very large integer then
this does three large integer operations in (n^(n-1)).bit_count().
They are relatively fast operations but all linear in bit size. By
contrast a check like n & 1 is O(1) and half the time proves that no
further steps are necessary.

The mpmath library needs this exact operation and is generally
intended for the large n case so I checked how it is implemented
there:

https://github.com/mpmath/mpmath/blob/f13ea4dc925d522062ac734bd19a0a3cc23f9c04/mpmath/libmp/libmpf.py#L160-L177

That code is:

# Strip trailing bits
if not man & 1:
t = trailtable[man & 255]
if not t:
while not man & 255:
man >>= 8
exp += 8
bc -= 8
t = trailtable[man & 255]
man >>= t
exp += t
bc -= t

The trailtable variable is a pre-initialised list of shifts needed to
remove zeros from an 8-bit integer. The bc variable here is just
bc=man.bit_length() which is redundant but this code predates the
addition of the int.bit_length() method.

In principle this could use a large number of man>>=8 shifts which
would potentially be quadratic in the bit size of man. In practice the
probability of hitting the worst case is very low so the code is
instead optimised for the expected common case of large man with few
trailing zeros.

--
Oscar

Re: on writing a number as 2^s * q, where q is odd

<ukk3m7$3bop7$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Mon, 4 Dec 2023 09:47:03 +0100
Organization: A noiseless patient Spider
Lines: 118
Message-ID: <ukk3m7$3bop7$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org> <uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
<CAHVvXxSMyRjXBaiqwfE9uQCm8RGaMKgmMFDts5eoAz8HVhPubg@mail.gmail.com>
<mailman.322.1701608830.3828.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 4 Dec 2023 08:47:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="660b850be563ab975fedde664ddabb04";
logging-data="3531559"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jOuGY/iunOOpjZ1TfrRNJ"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:1HKszJYb7VC4vuZKaLouYZva1S8=
In-Reply-To: <mailman.322.1701608830.3828.python-list@python.org>
 by: jak - Mon, 4 Dec 2023 08:47 UTC

Oscar Benjamin ha scritto:
> On Sun, 3 Dec 2023 at 10:25, Julieta Shem via Python-list
> <python-list@python.org> wrote:
>>
>> Alan Bawden <alan@csail.mit.edu> writes:
>>>
>>> def powers_of_2_in(n):
>>> bc = (n ^ (n - 1)).bit_count() - 1
>>> return bc, n >> bc
>>
>> That's pretty fancy and likely the fastest.
>
> It might be the fastest but it depends how big you expect n to be and
> how many trailing zeros you expect. If n is a very large integer then
> this does three large integer operations in (n^(n-1)).bit_count().
> They are relatively fast operations but all linear in bit size. By
> contrast a check like n & 1 is O(1) and half the time proves that no
> further steps are necessary.
>
> The mpmath library needs this exact operation and is generally
> intended for the large n case so I checked how it is implemented
> there:
>
> https://github.com/mpmath/mpmath/blob/f13ea4dc925d522062ac734bd19a0a3cc23f9c04/mpmath/libmp/libmpf.py#L160-L177
>
> That code is:
>
> # Strip trailing bits
> if not man & 1:
> t = trailtable[man & 255]
> if not t:
> while not man & 255:
> man >>= 8
> exp += 8
> bc -= 8
> t = trailtable[man & 255]
> man >>= t
> exp += t
> bc -= t
>
> The trailtable variable is a pre-initialised list of shifts needed to
> remove zeros from an 8-bit integer. The bc variable here is just
> bc=man.bit_length() which is redundant but this code predates the
> addition of the int.bit_length() method.
>
> In principle this could use a large number of man>>=8 shifts which
> would potentially be quadratic in the bit size of man. In practice the
> probability of hitting the worst case is very low so the code is
> instead optimised for the expected common case of large man with few
> trailing zeros.
>
> --
> Oscar
>

HI,
I would turn the question to you: how big do you expect the value to
manage?
In a 'big numbers' context or when you need to convert a large amount of
values at the same time, your comment is absolutely valid, it is less
valid if the values can be represented with 64 bits.
I'll try to imagine the worst case in 64 bit:

b64Max=0xFFFFFFFFFFFFFFFF
i=1
while True:
i *= 2
if not i <= b64Max:
break
else:
n=i
n 9223372036854775808

If we now use the function being discussed:

powers_of_2_in(n)
(63, 1)

we can see that the bit_count() method had to do 63 iterations to count
the bits. It probably had to find the highest bit first but what if it
had a simple algorithm inside like this:

def h_bit(val):
tbits = 0
bits = 64 // 2
b64Max = 0xFFFFFFFFFFFFFFFFF
while bits > 0:
if (b64Max << bits) & val:
val >>= bits
tbits += bits
bits //= 2
return tbits

would only add 6 more iterations for values needing 64 bits (7
interactions for 128 bits, 3 if 8 bits)

If we use the library you suggest for a single value or a non-'big
numbers' value (e.g. 2048 bit) the initialization alone would be slower
than the function in question. The table you are talking about is
initialized in the following way:

trailtable = [trailing(n) for n in range(256)]

(it calls a function 256 times)

This is why I think that what you said is true but it depends a lot on
the context and to all this I would add that the best cases are much
greater than the worst cases.

e.g.:
powers_of_2_in(n + 1)
(0, 9223372036854775809)
powers_of_2_in(n - 1)
(0, 9223372036854775807)

(probably only 1 interaction)

Re: on writing a number as 2^s * q, where q is odd

<86cyvltdq3.fsf@williamsburg.bawden.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bawden.eternal-september.org!.POSTED!not-for-mail
From: ala...@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Tue, 05 Dec 2023 00:33:56 -0500
Organization: ITS Preservation Society
Lines: 37
Message-ID: <86cyvltdq3.fsf@williamsburg.bawden.org>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org>
<uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
<CAHVvXxSMyRjXBaiqwfE9uQCm8RGaMKgmMFDts5eoAz8HVhPubg@mail.gmail.com>
<mailman.322.1701608830.3828.python-list@python.org>
<ukk3m7$3bop7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: bawden.eternal-september.org; posting-host="ef21b3f205303ac8622ccfc8c00a8f56";
logging-data="4006376"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jNRfww9/RwwvBA3MZeVAR"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:rlft+5dr9ei0zBJdfAvNm9bKljY=
sha1:5jMIyl86yrrgv8uN/xd9fMMr++k=
 by: Alan Bawden - Tue, 5 Dec 2023 05:33 UTC

jak <nospam@please.ty> writes:

Oscar Benjamin ha scritto:
...
If we now use the function being discussed:

powers_of_2_in(n)
(63, 1)

we can see that the bit_count() method had to do 63 iterations to count
the bits....

I certainly hope that the bit_count method doesn't count bits by
iterating over them one-by-one. You can count bits _much_ faster than
that.

You can count the bits in a 62-bit number as follows:

def bit_count_62(n):
n = (n - ((n >> 1) & 0o333333333333333333333)
- ((n >> 2) & 0o111111111111111111111))
n = ( (n & 0o307070707070707070707)
+ ((n & 0o070707070707070707070) >> 3))
return n % 63

Then if you want to count the bits in arbitrarily large integers, you
iterate over them in N-bit chunks, for some N <= 62. Your choice of N
will depend on how you represent your big integers. Probably N is 56 or
48 or 32.

And why 62 bits? Because the bit_count method is certainly written in
C, where every step in bit_count_62 would use 64-bit integers.

If you like this sort of stuff, check out the book "Hacker's Delight" by
Henry Warren. See <https://en.wikipedia.org/wiki/Hacker%27s_Delight>.

- Alan

Re: on writing a number as 2^s * q, where q is odd

<ukn0e5$4iuk$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: on writing a number as 2^s * q, where q is odd
Date: Tue, 5 Dec 2023 12:09:57 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <ukn0e5$4iuk$1@dont-email.me>
References: <87sf4oysry.fsf@yaxenu.org>
<86leagt1d9.fsf@williamsburg.bawden.org> <uk959k$185nm$1@dont-email.me>
<86h6l3u55e.fsf@williamsburg.bawden.org> <87sf4k2ex5.fsf@yaxenu.org>
<CAHVvXxSMyRjXBaiqwfE9uQCm8RGaMKgmMFDts5eoAz8HVhPubg@mail.gmail.com>
<mailman.322.1701608830.3828.python-list@python.org>
<ukk3m7$3bop7$1@dont-email.me> <86cyvltdq3.fsf@williamsburg.bawden.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Dec 2023 11:09:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5854f13969db886e8278ce981024284";
logging-data="150484"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZpWUvPrWmxB9G1oPnSUm1"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:nlqBTtSHORZ8RIE0MORE8mcQkw0=
In-Reply-To: <86cyvltdq3.fsf@williamsburg.bawden.org>
 by: jak - Tue, 5 Dec 2023 11:09 UTC

Alan Bawden ha scritto:
> If you like this sort of stuff, check out the book "Hacker's Delight" by
> Henry Warren. See<https://en.wikipedia.org/wiki/Hacker%27s_Delight>.

Thank you for your suggestion. Really interesting. Just for fun I tried
to port the function to 64 bit:

def bit_count_64(n):
lt = n >> 62
n &= (~(0x3f << 62)) & ((1 << 63) - 1)
n = (n - ((n >> 1) & 0o333333333333333333333)
- ((n >> 2) & 0o111111111111111111111))
n = ( (n & 0o307070707070707070707)
+ ((n & 0o070707070707070707070) >> 3))
return (n % 63) + (0, 1, 1, 2)[lt]

n=0xffffffffffffffff
bit_count_64(n)
64

n=0x3ffffffffffffffe
bit_count_64(n)
61

bit_count_64(1 << 63)
1

....in C it would have been simpler :^)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor