簡體   English   中英

Python:如何有效地計算1到n個數字的二進制表示形式中“ 1”的數量?

[英]Python: How to efficiently count the number of “1”s in the binary representation of 1 to n numbers?

例如,對於輸入5,輸出應為7(bin(1)= 1,bin(2)= 10 ... bin(5)= 101)-> 1 +1 + 2 +1 + 2 = 7

這是我嘗試過的方法,但是考慮到我為每個整數迭代一次循環,這並不是一種非常有效的算法。 我的代碼(Python 3):

i = int(input())
a = 0
for b in range(i+1):
  a = a + bin(b).count("1")
print(a)

謝謝!

這是基於OEIS的遞歸關系的解決方案:

def onecount(n):
    if n == 0:
        return 0
    if n % 2 == 0:
        m = n/2
        return onecount(m) + onecount(m-1) + m
    m = (n-1)/2
    return 2*onecount(m)+m+1

>>> [onecount(i) for i in range(30)]
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]

由於Alex Martella 等人的 緣故gmpy2似乎至少在我的Win10機器上表現更好。

from time import time
import gmpy2

def onecount(n):
    if n == 0:
        return 0
    if n % 2 == 0:
        m = n/2
        return onecount(m) + onecount(m-1) + m
    m = (n-1)/2
    return 2*onecount(m)+m+1

N = 10000

initial = time()
for _ in range(N):
    for i in range(30):
        onecount(i)
print (time()-initial)

initial = time()
for _ in range(N):
    total = 0
    for i in range(30):
        total+=gmpy2.popcount(i)
print (time()-initial)

這是輸出:

1.7816979885101318
0.07404899597167969

如果您想要一個列表,並且正在使用> Py3.2:

>>> from itertools import accumulate
>>> result = list(accumulate([gmpy2.popcount(_) for _ in range(30)]))
>>> result
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM