<jupyter_start><jupyter_text>Useful Functions<jupyter_code>def primes_upto(n):
    """
    Generator for primes up to n.
    For primes under 'n', just use n-1.
    """
    if n < 2:
        return
    marked = [0] * (n+1);
    value = 3
    yield 2
    while value <= n:
        if marked[value] == 0:
            yield value
            i = value
            while i <= n:
                marked[i] = 1
                i += value
        value += 2

list(primes_upto(20))
def num_digits(n, base=10):
    """
    Number of digits for the number 'n' given some 'base'.
    """
    if n < 0:
        return num_digits(-n, base)
    if n < base:
        return 1
    return 1 + num_digits(n//base, base)

for b in [2, 8, 10, 16]:
    print("255 in base %d has %d digits."%(b, num_digits(255, b)))
import itertools

print('Permutations:', list(itertools.permutations([1,2,3])))
import math
def is_pentagonal(x):
    # See test for pentagonal numbers
    # https://en.wikipedia.org/wiki/Pentagonal_number
    n = math.sqrt(24 * x + 1) + 1
    return n/6 == n//6
import math
def is_hexagonal(x):
    # See test for pentagonal numbers
    # https://en.wikipedia.org/wiki/Hexagonal_number
    n = math.sqrt(8 * x + 1) + 1
    return n/4 == n//4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13575374901341752, 'fraction_numerical': 0.03314917127071823, 'mean_word_length': 2.1779448621553885, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '196827', 'license': 'MIT', 'n_tokens_mistral': 485, 'n_tokens_neox': 450, 'n_words': 131, 'path': 'solutions/jupyter/useful-functions.ipynb', 'repo_name': 'danui/project-euler', 'size': '1267'}