Exemple #1
0
def test_iterator():
    it = primesieve.Iterator()
    assert it.next_prime() == 2
    assert it.next_prime() == 3
    assert it.next_prime() == 5
    assert it.next_prime() == 7
    assert it.next_prime() == 11
    assert it.previous_prime() == 7
    assert it.next_prime() == 11
def a(n):
    it = primesieve.Iterator()

    if n < 1: return None
    elif n == 1:
        it.skipto(int(math.pow(10, 14)))
        return it.next_prime()
    else:
        it.skipto(a(n - 1))
        return it.next_prime()
def first_prime_factor(n):
    it = primesieve.Iterator()
    max = math.ceil(math.sqrt(n))
    i = it.next_prime()
    while i <= max:
        if n % i:
            i = it.next_prime()
        else:
            n //= i
            return i
    if n > 1: return n
Exemple #4
0
def prime_stat(top):
    n = 0
    primes = primesieve.Iterator()
    beg = time.time()
    while True:
        if primes.next_prime() >= top:
            break
        n += 1
    end = time.time()
    print(
        f"{end-beg:.2f}s to enumerate {n} primes lower than {top} (ratio = {100*n/top} %)"
    )
Exemple #5
0
def get_divisors(num):
    divs = []
    for base in range(2, 11):
        converted = convert(num, base)
        it = primesieve.Iterator()
        prime = it.next_prime()
        while converted % prime != 0 and prime < THR:
            prime = it.next_prime()
        if prime > THR:
            return None
        else:
            divs.append(prime)
    return divs
def test_iterator():
    """Test iterator."""
    it = primesieve.Iterator()
    assert it.next_prime() == 2
    assert it.next_prime() == 3
    assert it.next_prime() == 5
    assert it.next_prime() == 7
    assert it.next_prime() == 11
    assert it.next_prime() == 13
    assert it.prev_prime() == 11
    assert it.prev_prime() == 7
    assert it.prev_prime() == 5
    assert it.prev_prime() == 3
    assert it.prev_prime() == 2
    assert it.prev_prime() == 0
Exemple #7
0
def calc_gaps(n: int) -> gap_histogram:
    """Calculates prime number gaps histogram from 0 up to nth prime number.

    Args:
        n (int): The nth prime number to which the function will calculate gaps.

    Returns:
        gap_histogram: Dictionary mapping gap sizes with their number of occurrences.
    """
    it: primesieve.Iterator = primesieve.Iterator()
    gap_histogram_data: dict = {}
    prime: int = it.next_prime()
    for i in range(int(n)):
        prev = prime
        prime = it.next_prime()
        gap = prime - prev
        if gap_histogram_data.get(gap):
            gap_histogram_data[gap] += 1
        else:
            gap_histogram_data[gap] = 1
    return gap_histogram_data
Exemple #8
0
def primes():
    it = primesieve.Iterator()
    while True:
        yield it.next_prime()
Exemple #9
0
import networkx as nx
import primesieve
import ulid

BuilderGraph = nx.DiGraph()
HierarchyGraph = nx.DiGraph()

prime_iterator = primesieve.Iterator()

ClassSet = dict()

CategorySet = dict()


def show(e):
    print(e)


class Class:
    def __init__(self,
                 builder_cats=(),
                 source_object=None,
                 builder_predicate=None):
        self.builder_predicate = builder_predicate
        self.id = ulid.new().str
        # link class with builder cat
        # insert in the class
        for cat in builder_cats:
            BuilderGraph.add_edge(cat.id, self.id, weight=None)
        ClassSet[self.id] = self
Exemple #10
0
def digit_data():
    print(
        "Do know this is very intensive, counting all the digits, but enjoy.")
    list_0, list_1, list_2, list_3, list_4, list_5, list_6, list_7, list_8, list_9 = [],[],[],[],[],[],[],[],[],[]
    g_count_0 = 0
    g_count_1 = 0
    g_count_2 = 0
    g_count_3 = 0
    g_count_4 = 0
    g_count_5 = 0
    g_count_6 = 0
    g_count_7 = 0
    g_count_8 = 0
    g_count_9 = 0
    lower = 0
    upper = int(correct_inputs("", "upper"))
    step = int(correct_inputs(upper, ))
    count = lower + step
    count_list = []
    print("Processing...")
    t_1 = time.perf_counter()
    it = primesieve.Iterator(0)
    while count <= upper:
        prime_str = ""
        x = it.next_prime()
        prime_list.append(x)
        prime_digitdata_list = list(dict.fromkeys(prime_list))
        global prime_save_list
        prime_save_list = prime_digitdata_list
        for x in prime_digitdata_list:
            prime_str += f"{x}"
            g_count_0 = int(prime_str.count("0"))
            g_count_1 = int(prime_str.count("1"))
            g_count_2 = int(prime_str.count("2"))
            g_count_3 = int(prime_str.count("3"))
            g_count_4 = int(prime_str.count("4"))
            g_count_5 = int(prime_str.count("5"))
            g_count_6 = int(prime_str.count("6"))
            g_count_7 = int(prime_str.count("7"))
            g_count_8 = int(prime_str.count("8"))
            g_count_9 = int(prime_str.count("9"))
        list_0.append(g_count_0), list_1.append(g_count_1), list_2.append(
            g_count_2), list_3.append(g_count_3), list_4.append(
                g_count_4), list_5.append(g_count_5), list_6.append(
                    g_count_6), list_7.append(g_count_7), list_8.append(
                        g_count_8), list_9.append(
                            g_count_9), count_list.append(count)
        count += step
    global highest_number_dict
    highest_number_dict = {
        0: g_count_0,
        1: g_count_1,
        2: g_count_2,
        3: g_count_3,
        4: g_count_4,
        5: g_count_5,
        6: g_count_6,
        7: g_count_7,
        8: g_count_8,
        9: g_count_9
    }
    max_set = max(highest_number_dict, key=highest_number_dict.get)
    t_2 = time.perf_counter()
    print("Getting your image ready...")
    digit_plotting(list_0, list_1, list_2, list_3, list_4, list_5, list_6,
                   list_7, list_8, list_9, count_list)
    t_3 = time.perf_counter()
    return t_1, t_2, t_3, max_set
Exemple #11
0
#
def pan(n):
    if n % 10 not in [3, 5, 7]:
        return False
    m = n
    for i in range(1, len(str(n)) + 1):
        if not isPrime(n % (10**i)):
            return False
        if not isPrime(m):
            return False
        m = int(m / 10)
    return True


if __name__ == "__main__":
    it = primesieve.Iterator()
    it.skipto(12)
    prime = it.next_prime()

    count = 0
    sum = 0

    while count != 11:
        if pan(prime):
            print(prime)
            sum += prime
            count += 1
        prime = it.next_prime()
        ll.append(prime)

    print(sum)
Exemple #12
0
def isprime(x):
    it = primesieve.Iterator()
    it.skipto(x - 1)
    return x == it.next_prime()
Exemple #13
0
 def __init__(self):
     self.it = primesieve.Iterator()