def test_upsert_from_empty(self, newDatabase): db: DivisorDb = newDatabase() records = [ RiemannDivisorSum(n=1, divisor_sum=1, witness_value=1), RiemannDivisorSum(n=2, divisor_sum=2, witness_value=2), ] db.upsert(records) assert set(db.load()) == set(records) db.teardown()
def test_compute_riemann_divisor_sums(): expected = [ RiemannDivisorSum(n=10080, divisor_sum=39312, witness_value=1.75581), RiemannDivisorSum(n=10081, divisor_sum=10692, witness_value=0.47749), RiemannDivisorSum(n=10082, divisor_sum=15339, witness_value=0.68495), ] actual = compute_riemann_divisor_sums(start_n=10080, end_n=10081) for ex, ac in zip(expected, actual): assert ex.approx_equal(ac)
def test_hash_riemann_divisor_sums(): sums = [ RiemannDivisorSum(n=10080, divisor_sum=39312, witness_value=1.75581), RiemannDivisorSum(n=10081, divisor_sum=10692, witness_value=0.47749), RiemannDivisorSum(n=10082, divisor_sum=15339, witness_value=0.68495), ] expected = sha256(bytes("10080,1.7558,10081,0.4775,10082,0.6849", 'utf-8')).hexdigest() assert expected == hash_divisor_sums(sums)
def test_summarize_nonempty(self, newDatabase): db: DivisorDb = newDatabase() records = [ RiemannDivisorSum(n=9, divisor_sum=3, witness_value=3), RiemannDivisorSum(n=4, divisor_sum=4, witness_value=4), ] db.upsert(records) expected = SummaryStats(largest_computed_n=records[0], largest_witness_value=records[1]) assert expected == db.summarize() db.teardown()
def test_summarize_nonempty(self, db): self.populate_search_blocks(db) block = db.claim_next_search_block( search_index_type='ExhaustiveSearchIndex') records = [ RiemannDivisorSum(n=9, divisor_sum=3, witness_value=3), RiemannDivisorSum(n=4, divisor_sum=4, witness_value=4), ] db.finish_search_block(block, records) expected = SummaryStats(largest_computed_n=records[0], largest_witness_value=records[1]) assert expected == db.summarize()
def test_finish_search_block_only_store_large_witness_values(self, db): self.populate_search_blocks(db) block = db.claim_next_search_block( search_index_type='ExhaustiveSearchIndex') records = [ RiemannDivisorSum(n=1, divisor_sum=1, witness_value=1), RiemannDivisorSum(n=2, divisor_sum=2, witness_value=2), ] db.finish_search_block(block, records) stored = list(db.load()) assert len(stored) == 1 assert stored[0].n == 2
def compute_riemann_divisor_sum( factorization: PrimeFactorization) -> RiemannDivisorSum: '''Compute a divisor sum.''' n = reduce(lambda x, y: x * y, (p**a for (p, a) in factorization)) ds = prime_factor_divisor_sum(factorization) wv = ds / (n * math.log(math.log(n))) return RiemannDivisorSum(n=n, divisor_sum=ds, witness_value=wv)
def test_upsert_overrides(self, newDatabase): db: DivisorDb = newDatabase() records = [ RiemannDivisorSum(n=1, divisor_sum=1, witness_value=1), RiemannDivisorSum(n=2, divisor_sum=2, witness_value=2), ] db.upsert(records) new_records = [ RiemannDivisorSum(n=1, divisor_sum=3, witness_value=3), RiemannDivisorSum(n=4, divisor_sum=4, witness_value=4), ] db.upsert(new_records) assert set(db.load()) == set([records[1]] + new_records) db.teardown()
def test_finish_search_block_hash_updated(self, db): self.populate_search_blocks(db) block = db.claim_next_search_block( search_index_type='ExhaustiveSearchIndex') records = [ RiemannDivisorSum(n=1, divisor_sum=1, witness_value=1), RiemannDivisorSum(n=2, divisor_sum=2, witness_value=2), ] db.finish_search_block(block, records) metadata = [ x for x in db.load_metadata() if x.state == SearchBlockState.FINISHED ][0] assert metadata.block_hash != None
def test_claim_and_finish_search_block(self, db): self.populate_search_blocks(db) block = db.claim_next_search_block( search_index_type='ExhaustiveSearchIndex') records = [ RiemannDivisorSum(n=1, divisor_sum=1, witness_value=1), RiemannDivisorSum(n=2, divisor_sum=2, witness_value=2), ] db.finish_search_block(block, records) assert len(list(db.load())) > 0 next_block = db.claim_next_search_block( search_index_type='ExhaustiveSearchIndex') assert block.starting_search_index != next_block.starting_search_index
def test_upsert_mpz(self, newDatabase): db: DivisorDb = newDatabase() records = [ RiemannDivisorSum(n=mpz(1), divisor_sum=mpz(1), witness_value=1), ] db.upsert(records) assert set(db.load()) == set(records) db.teardown()
def load(self) -> Iterable[RiemannDivisorSum]: cursor = self.connection.cursor("load_divisor_sums") cursor.itersize = 1000000 cursor.execute(''' SELECT n, divisor_sum, witness_value FROM RiemannDivisorSums; ''') for row in cursor: yield RiemannDivisorSum(n=mpz(row[0]), divisor_sum=mpz(row[1]), witness_value=row[2])
def compute_riemann_divisor_sums(start_n: int, end_n: int) -> List[RiemannDivisorSum]: '''Compute a batch of divisor sums.''' output = [] for n in range(start_n, end_n + 1): ds = divisor_sum(n) wv = witness_value(n, precomputed_divisor_sum=ds) output.append(RiemannDivisorSum(n=n, divisor_sum=ds, witness_value=wv)) return output
def test_search_block_with_mpz_values(self, db): metadatas = [ SearchMetadata(search_index_type='SuperabundantEnumerationIndex', starting_search_index=SuperabundantEnumerationIndex( level=i, index_in_level=0), ending_search_index=SuperabundantEnumerationIndex( level=i + 1, index_in_level=0)) for i in range(1, 10, 2) ] db.insert_search_blocks(metadatas) block = db.claim_next_search_block( search_index_type='SuperabundantEnumerationIndex') records = [ RiemannDivisorSum(n=mpz(1), divisor_sum=mpz(1), witness_value=1), RiemannDivisorSum(n=mpz(2), divisor_sum=mpz(2), witness_value=2), ] db.finish_search_block(block, records) stored = list(db.load()) assert len(stored) == 1 assert stored[0].n == 2
def convert_records(self, rows): return [ RiemannDivisorSum(n=mpz(row[0]), divisor_sum=mpz(row[1]), witness_value=row[2]) for row in rows ]