コード例 #1
0
def main():
    """
    Entry point
    """
    power = 1
    match_count = 0
    while True:
        # See how many positive integers have same digits as power
        i = 1
        while True:
            i_p = i**power
            nd = num_digits(i_p)
            if nd == power:
                # Yep, this matches
                match_count += 1
            elif nd > power:
                # No more i values will have few enough digits
                break

            i += 1

        power += 1
        if num_digits(9**power) < power:
            # We're done
            break

    print(f"Number of digit-matched powers: {match_count}")
コード例 #2
0
def main():
    """
    Entry point
    """
    # We have been told that the largest pandigital must at least start with 9
    # and so we just need to try prefixes with 91 being our starting point!
    # The furthest we can conceivably go is a four-digit 9xxx times (1, 2)
    best_candidate = 918273645
    for start, end in [(91, 99), (912, 988), (9123, 9877)]:
        for i in range(start, end):
            prods = []
            num_digits_total = 0
            multiplier = 1
            while num_digits_total < 9:
                prod = i * multiplier
                prods.append(prod)
                num_digits_total += num_digits(prod)
                multiplier += 1

            if test_pandigital_9(*prods):
                new_candidate = 0
                for prod in prods:
                    new_candidate *= 10 ** num_digits(prod)
                    new_candidate += prod

                if new_candidate > best_candidate:
                    best_candidate = new_candidate

    print(f"Highest: {best_candidate}")
コード例 #3
0
def main():
    """
    Entry point
    """
    num_perms_to_find = 5

    # Start searching with the first 7 digit cube
    root = 100
    cubes = [100**3]
    while True:
        root += 1
        cube = root**3
        if num_digits(cube) > num_digits(cubes[-1]):
            # Search the existing cube list for permutations
            result = search_permutations(cubes, num_perms_to_find)
            if result:
                break

            # Now clear the cube list and start on the new number of digits!
            cubes.clear()

        cubes.append(cube)

    print(
        f"Smallest cube with 5 digit perms: {result[0]} (permutations: {result})"
    )
コード例 #4
0
def main():
    """
    Entry point
    """
    denom = 2
    heavy_num_count = 0
    for i in range(1000):
        root_2_approx = 1 + Fraction(1, denom)
        denom = 2 + Fraction(1, denom)
        if num_digits(root_2_approx.numerator) > num_digits(
                root_2_approx.denominator):
            heavy_num_count += 1

    print(f"Number of top-heavy approximations: {heavy_num_count}")
コード例 #5
0
def main():
    """
    Entry point
    """
    # Brute force: try all products to 10000x1000, skipping those when the digit total is too high
    products = set()
    for a in range(1, 1000):
        for b in range(a + 1, 10000):
            # Stop this loop once we're getting too many digits
            product = a * b
            if num_digits(a) + num_digits(b) + num_digits(product) > 9:
                break

            if test_pandigital_9(a, b, product):
                products.add(product)

    print(f"Sum of pandigital products: {sum(products)}")
    return
コード例 #6
0
def main():
    """
    Entry point
    """
    # Get a bunch of primes to test. Guess how many digits we might need to search up to.
    max_num_digits = 6
    family_size = 8
    primes = get_primes(10**max_num_digits)

    # We know the first prime with 8 family members will be > 56003, try 5 digits first
    for n_digits in range(5, max_num_digits + 1):
        print(f"Trying {n_digits} digits...")

        # Get the set of candidates for this digit count
        n_digit_primes = [p for p in primes if num_digits(p) == n_digits]
        n_digit_prime_set = set(n_digit_primes)

        for base_prime in n_digit_primes:
            # We want at least 8 primes, so we need to have the last digit as 1, 3, 5, or 7
            # (i.e. the last digit of this prime can't be a replacement candidate)
            for num_replacement_digits in range(1, n_digits):
                digit_pattern = [1] * num_replacement_digits + [0] * (
                    n_digits - 1 - num_replacement_digits)
                for replacement_pattern in set(
                        itertools.permutations(digit_pattern, n_digits - 1)):
                    # We need family_size primes, so as soon as we have (10 - family_size + 1) non-primes, we're "bust"
                    num_non_primes = 0
                    for replacement_digit in range(10):
                        # Make the replacement (counting digits from right to left)
                        candidate = 0
                        for digit in range(n_digits):
                            digit_multiplier = 10**digit
                            if digit == 0 or not replacement_pattern[-digit]:
                                # Use the original digit
                                candidate += (
                                    (base_prime // digit_multiplier) %
                                    10) * digit_multiplier
                            else:
                                # Use the replacement digit
                                candidate += replacement_digit * digit_multiplier

                        # Test it
                        if candidate not in n_digit_prime_set:
                            num_non_primes += 1
                            if num_non_primes > 10 - family_size:
                                # Next replacement pattern
                                break

                    if num_non_primes <= (10 - family_size):
                        # We found it!
                        print(
                            f"Found prime: {base_prime}, replacement pattern: {replacement_pattern}"
                        )
                        return

    print("Found nothing :-(")
コード例 #7
0
def main():
    """
    Entry point
    """
    x = 1
    while True:
        nd_x = num_digits(x)
        d_x = sorted(get_digits(x))

        # Get multiples 2x, 3x... 6x
        wrong_num_digits = False
        all_permutations = True
        for multiplier in range(2, 7):
            y = multiplier * x

            # Does it have the same number of digits?
            nd_y = num_digits(y)
            if nd_y != nd_x:
                wrong_num_digits = True
                break

            # Is it a permutation of the digits of x?
            d_y = sorted(get_digits(y))
            if d_y != d_x:
                all_permutations = False
                break

        if wrong_num_digits:
            # No point continuing with any more x values with this number of digits
            x = 10**num_digits(x)
            continue

        if all_permutations:
            # Looking good!
            break

        x += 1

    print(f"First integer with 2-6x permutation multiples: {x}")