Beispiel #1
0
 def test_ones(self):
     n = randint(1, 100000)
     a = bitarray(n)
     a.setall(1)
     self.assertEqual(count_n(a, n), n)
     self.assertRaises(ValueError, count_n, a, n + 1)
     for _ in range(20):
         i = randint(0, n)
         self.assertEqual(count_n(a, i), i)
Beispiel #2
0
 def test_count_n3(self):
     N = 100000
     for _ in range(10):
         a = bitarray(N)
         a.setall(0)
         self.assertEqual(count_n(a, 0), 0)
         self.assertRaises(ValueError, count_n, a, 1)
         i = randint(0, N - 1)
         a[i] = 1
         self.assertEqual(count_n(a, 1), i + 1)
         self.assertRaises(ValueError, count_n, a, 2)
Beispiel #3
0
 def test_count_n1_frozen(self):
     a = frozenbitarray('001111101111101111101111100111100')
     self.assertEqual(len(a), 33)
     self.assertEqual(a.count(), 24)
     self.assertRaises(TypeError, count_n, '', 0)
     self.assertEqual(count_n(a, 0), 0)
     self.assertEqual(count_n(a, 10), 13)
     self.assertEqual(count_n(a, 24), 31)
     self.assertRaises(ValueError, count_n, a, -1)  # n < 0
     self.assertRaises(ValueError, count_n, a, 25)  # n > a.count()
     self.assertRaises(ValueError, count_n, a, 34)  # n > len(a)
     self.assertRaises(TypeError, count_n, a, "7")
Beispiel #4
0
 def test_large(self):
     for N in list(range(100)) + [1000, 10000, 100000]:
         a = bitarray(N)
         v = randint(0, 1)
         a.setall(not v)
         for _ in range(randint(0, min(N, 100))):
             a[randint(0, N - 1)] = v
         tc = a.count()  # total count
         n = randint(0, tc)
         self.check_result(a, n, count_n(a, n))
         self.check_result(a, tc, count_n(a, tc))
         self.assertRaises(ValueError, count_n, a, tc + 1)
Beispiel #5
0
 def test_one_set(self):
     n = randint(1, 100000)
     a = bitarray(n)
     a.setall(0)
     self.assertEqual(count_n(a, 0), 0)
     self.assertRaises(ValueError, count_n, a, 1)
     for _ in range(20):
         a.setall(0)
         i = randint(0, n - 1)
         a[i] = 1
         self.assertEqual(count_n(a, 1), i + 1)
         self.assertRaises(ValueError, count_n, a, 2)
Beispiel #6
0
 def test_frozen(self):
     a = frozenbitarray('001111101111101111101111100111100')
     self.assertEqual(len(a), 33)
     self.assertEqual(a.count(), 24)
     self.assertEqual(count_n(a, 0), 0)
     self.assertEqual(count_n(a, 10), 13)
     self.assertEqual(count_n(a, 24), 31)
     self.assertRaises(ValueError, count_n, a, -1)  # n < 0
     self.assertRaises(ValueError, count_n, a, 25)  # n > a.count()
     self.assertRaises(ValueError, count_n, a, 34)  # n > len(a)
     for n in range(0, 25):
         self.check_result(a, n, count_n(a, n))
Beispiel #7
0
 def test_count_n2(self):
     for N in list(range(100)) + [1000, 10000, 100000]:
         a = bitarray(N)
         v = randint(0, 1)
         a.setall(v - 1)
         for _ in range(randint(0, min(N, 100))):
             a[randint(0, N - 1)] = v
         n = randint(0, a.count())
         self.check_result(a, n, count_n(a, n))
         # check for total count
         tc = a.count()
         self.assertTrue(count_n(a, tc) <= N)
         self.assertRaises(ValueError, count_n, a, tc + 1)
Beispiel #8
0
 def test_empty(self):
     a = bitarray()
     self.assertEqual(count_n(a, 0), 0)
     self.assertRaises(ValueError, count_n, a, 1)
     self.assertRaises(TypeError, count_n, '', 0)
     self.assertRaises(TypeError, count_n, a, 7.0)
     self.assertRaises(TypeError, count_n, a, 0, 0)
Beispiel #9
0
 def test_count_n1(self):
     a = bitarray('111110111110111110111110011110111110111110111000')
     b = a.copy()
     self.assertEqual(len(a), 48)
     self.assertEqual(a.count(), 37)
     self.assertRaises(TypeError, count_n, '', 0)
     self.assertEqual(count_n(a, 0), 0)
     self.assertEqual(count_n(a, 20), 23)
     self.assertEqual(count_n(a, 37), 45)
     self.assertRaises(ValueError, count_n, a, -1)  # n < 0
     self.assertRaises(ValueError, count_n, a, 38)  # n > a.count()
     self.assertRaises(ValueError, count_n, a, 49)  # n > len(a)
     self.assertRaises(TypeError, count_n, a, "7")
     for n in range(0, 37):
         i = count_n(a, n)
         self.check_result(a, n, i)
         self.assertEqual(a[:i].count(), n)
     self.assertEQUAL(a, b)
Beispiel #10
0
 def test_simple(self):
     a = bitarray('111110111110111110111110011110111110111110111000')
     b = a.copy()
     self.assertEqual(len(a), 48)
     self.assertEqual(a.count(), 37)
     self.assertEqual(count_n(a, 0), 0)
     self.assertEqual(count_n(a, 20), 23)
     self.assertEqual(count_n(a, 37), 45)
     self.assertRaisesMessage(ValueError, "non-negative integer expected",
                              count_n, a, -1)  # n < 0
     self.assertRaisesMessage(ValueError, "n larger than bitarray size",
                              count_n, a, 49)  # n > len(a)
     self.assertRaisesMessage(ValueError, "n exceeds total count", count_n,
                              a, 38)  # n > a.count()
     for n in range(0, 38):
         i = count_n(a, n)
         self.check_result(a, n, i)
         self.assertEqual(a[:i].count(), n)
         self.assertEqual(i, self.count_n(a, n))
     self.assertEQUAL(a, b)
Beispiel #11
0
def euler399(n):
    print("generating Fibonacci sieve")
    limit = int(n * 1.31)
    sffs = bitarray(limit)
    sffs.setall(True)  # square free Fibonacci sieve
    for p in primerange(1, 1500000):
        l = p * fiblen(p, limit // p + 1)
        if l:
            sffs[l - 1::l] = False
    print("counting")
    t0 = time()
    i = count_n(sffs, n)
    print("time:", time() - t0)
    print(i)
    print('%i,%.1fe%i' % ith_fib(i))
Beispiel #12
0
 def test_count_n4(self):
     for a in self.randombitarrays():
         n = a.count() // 2
         i = count_n(a, n)
         self.check_result(a, n, i)
Beispiel #13
0
import sys
from math import sqrt
if sys.version_info[0] == 2:
    range = xrange

from bitarray import bitarray
from bitarray.util import count_n

MILLION = 1000 * 1000
N = 100 * MILLION

# Each bit corresponds to whether or not a[i] is a prime
a = bitarray(N + 1)
a.setall(True)
# Zero and one are not prime
a[:2] = False
# Perform sieve
for i in range(2, int(sqrt(N)) + 1):
    if a[i]:  # i is prime, so all multiples are not
        a[i * i::i] = False

print('the first few primes are:')
print(a.search(1, 20))

# There are 5,761,455 primes up to 100 million
print('there are %d primes up to %d' % (a.count(), N))

# The 1 millionth prime number is 15,485,863
m = MILLION
print('the %dth prime is %d' % (m, count_n(a, m) - 1))
Beispiel #14
0
from bitarray import bitarray
from bitarray.util import count_n

N = 150_000

a = bitarray(N + 1)
a.setall(True)
a[:2] = False
for i in range(2, int(N**0.5) + 1):
    if a[i]:
        a[i * i::i] = False

m = 10_001
print(count_n(a, m) - 1)

# or using sympy
from sympy.ntheory import prime

print(prime(m))
        max_value = self.__queue[0]
        self.__exchange(0, len(self.__queue) - 1)
        self.__queue.pop()
        self.__sink(0)
        return max_value

    def __sink(self, i):
        while (j := i * 2 + 1) <= (length := len(self.__queue) - 1):
            if j < length and self.__less(j, j + 1):
                j += 1

            self.__exchange(i, j)
            i = j

    def __less(self, i, j):
        return self.__queue[i] < self.__queue[j]

    def __exchange(self, i, j):
        self.__queue[i], self.__queue[j] = self.__queue[j], self.__queue[i]


b = MaxProrityQueue().push((2, "a")).push((3, "b")).push((4, "c"))
for v in b:
    print(v)

print(b.pop())

print(a := bitarray(15))
print(a.count(1))
print(util.count_n(a, 1))  # 取得第一個True的index