Beispiel #1
0
primeFactors = euler.primeFactors(230, primes)
assert primeFactors == primeFactors0

primeFactors0 = [2, 2, 3, 3]
primeFactors = euler.primeFactors(36, primes)
assert primeFactors == primeFactors0

primePowerFactorization0 = {2: 2, 3: 2}
primePowerFactorization = euler.primePowerFactorization(primeFactors)
assert primePowerFactorization0 == primePowerFactorization

assert (euler.descendingFactorial(5, 2) == 20)
assert (euler.descendingFactorial(5, 3) == 60)
assert (euler.ascendingFactorial(5, 2) == 30)
assert (euler.ascendingFactorial(5, 3) == 210)

assert (euler.descendingFactorial(5, -2) == 1 / 42)
assert (euler.descendingFactorial(5, -3) == 1 / 336)
assert (euler.ascendingFactorial(5, -2) == 1 / 12)
assert (euler.ascendingFactorial(5, -3) == 1 / 24)

assert (euler.factors(20, []) == [1, 2, 4, 5, 10, 20])

assert (euler.appendDigits(37465, []) == [3, 4, 5, 6, 7])
assert (euler.appendDigitsUnsorted(37465, []) == [3, 7, 4, 6, 5])

primes = euler.readPrimes()
assert (primes[0:20] == [
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
])
Beispiel #2
0
#!/usr/bin/python

from euler import readPrimes

PRIMES = readPrimes()
PRIMES.reverse()

ways = {}


def countWays(money, coinIndex, coins):
    while money < coins[coinIndex] and coinIndex < len(coins) - 1:
        coinIndex += 1
    if money == 0:
        return 1
    if coinIndex == len(coins) - 1:
        if money % coins[coinIndex] == 0:
            return 1
        else:
            return 0
    p = (money, coinIndex)
    if p in ways:
        return ways[p]
    else:
        number = 0
        coin = coins[coinIndex]
        maxNumber = money // coin
        way = 0
        while number <= maxNumber:
            way += countWays(money - number * coin, coinIndex + 1, coins)
            number += 1
Beispiel #3
0
#!/usr/bin/python

from euler import readPrimes, primeFactors, primePowerFactorization
import math

UPPER = 10**7
LIMIT = UPPER
PRIMES = readPrimes( LIMIT )

def totient( n, primes ):
    pFs = primeFactors( n, primes )
    ppFm = primePowerFactorization( pFs )
    ppFmKeys = ppFm.keys()
    totient = n
    for primeFactor in ppFmKeys:
        totient = totient - totient / primeFactor
    return totient

assert( totient( 1, PRIMES ) == 1 )
assert( totient( 9, PRIMES ) == 6 )
assert( totient( 87109, PRIMES ) == 79180 )

def ascending( s ):
    ss = list( s )
    ss.sort()
    return ''.join( ss )

assert( ascending( '56623104' ) == '01234566')

minimum = math.inf
Beispiel #4
0
            if ascending(str(n)) == ascending(str(totient)):
                p = n / (n - totient)
                if primeMin < p:
                    primeMin = p
                    nPerm = n
            n *= prime
            totient *= prime
    n2totient0.update(n2totient)
    return n2totient0, upper, primeMin, nPerm


PRIME_MIN = 1000.0
primeMin = PRIME_MIN

UPPER = 10**7  # UPPER = 10 # UPPER = 10**5 #
PRIMES = readPrimes(UPPER / int(primeMin))
PRIMES.reverse()

n2totient0 = dict({1: 1})  # totientRatio = phi(n) / n
nPerm = 0

start = time.time()

i = 0
while i < len(PRIMES) and primeMin <= PRIMES[i]:
    n2totient0, upper, primeMin, nPerm = addPrime(PRIMES[i], n2totient0, UPPER,
                                                  primeMin, nPerm)
    i += 1

end = time.time()
Beispiel #5
0
#!/usr/bin/python

from collections import Counter
from itertools import combinations, product
from euler import readPrimes, isPrime
from copy import deepcopy

primes = readPrimes()  # complete set of primes up to 15485863 digits
primea = set(primes)
DIGIT_BIG = 7

BASE = 10
DIGIT_S = range(0, BASE)


# Does the list of digits match in the positions indexed by c?
def primesWithDigitCount(d):
    return [str(p_i) for p_i in primes if BASE**(d - 1) <= p_i < BASE**d]


def toDigit2Count(prime_c):
    return Counter(list(prime_c))


c0 = Counter({'6': 2, '8': 2, '4': 1, '5': 1, '2': 1})
assert (toDigit2Count('4656882') == c0)


def primeWithRepeat_is(numberOfDigits_i, digit_i):
    digitOther_is = list(DIGIT_S)
    digitOther_is.remove(digit_i)
Beispiel #6
0
#!/usr/bin/python

from euler import readPrimes
from bisect import insort
import time

UPPER = 10**6  # UPPER = 8 #
PRIMES = readPrimes(UPPER)


# Constructs table of non-0 mobius function values.
# Time-complexity is O( P * A * (log A)**2 )
#   P = #{ primes <= upper }, A = #{ stored Mobius arguments }
def n2mobius(upper, primes):
    n2mobius = dict({1: 1})
    keys = [1]
    for p in primes:
        if upper < p:
            break
        keys0 = []
        for n in keys:
            if upper < p * n:
                break
            n2mobius[p * n] = -n2mobius[n]
            keys0.append(p * n)
        for key0 in keys0:
            insort(keys, key0)
    return n2mobius


def mobiusFctAll(n, mobiusFct):
Beispiel #7
0
#!/usr/bin/python

from euler import readPrimes
from euler import isPrime

UPPER = 100

def appendDiagonal( ns ):
    delta = ns[ -1 ] - ns[ -2 ]
    delta += 8
    ns.append( ns[ -1 ] + delta )

PRIMES = readPrimes() # complete set of primes up to MAX_PRIME_DIGITCOUNT digits
MAX_PRIME = max( PRIMES )
assert( MAX_PRIME == 15485863 )
PRIME_SET = set( PRIMES )
MAX_TEST = MAX_PRIME * MAX_PRIME

def isTestedPrime( n ):
    if n <= MAX_PRIME:
        return n in PRIME_SET
    elif n <= MAX_TEST:
        return isPrime( n, PRIMES )
    else:
        raise Exception( 'n is too large to test.' )
        
diagonals = [ [ 3, 13, 31 ], [ 5, 17, 37 ], [ 7, 21, 43 ] ]

numerator = 8 
denominator = 13 
ratio = numerator / denominator
Beispiel #8
0
#!/usr/bin/python

from euler import readPrimes, primeFactors, primePowerFactorization
from operator import mul
from functools import reduce

UPPER = 1000 

primes = readPrimes( UPPER )

d0 = 1

for n in range( 2, UPPER ):
    pfs = primeFactors(n, primes)
    p2power = primePowerFactorization(pfs)
    values = list( p2power.values() )
    if len( values ) == 1:
        sigma0 = 1 + values[ 0 ]
    else:       
        sigma0 = reduce( mul, [ (1 + value ) for value in values ] )
    if d0 < sigma0:
        d0 = sigma0
        print( n )
Beispiel #9
0
#!/usr/bin/python

from euler import readPrimes

primes = readPrimes(10**6)

pr0 = 1
for p in primes:
    pr = p * pr0
    if pr > 10**6:
        break
    pr0 = pr

print(pr0)
Beispiel #10
0
#!/usr/bin/python

from euler import isPrime
from euler import readPrimes

#PRIME_LIMIT = 10**6
#PRIMES = readPrimes( PRIME_LIMIT ) # list of primes up to PRIME_LIMIT
PRIMES = readPrimes() # list of 1st million primes
PRIMECS = list( map( str, PRIMES ) ) # list PRIMES as strings
PRIMECA = set( PRIMECS ) # set of PRIMECS 

assert( PRIMECS[ 0 ] == '2' )

def isPrimeString( s ):
    if s in PRIMECA:
        return True
    n = int( s )
    if PRIMES[ -1 ] * PRIMES[ -1 ] < n:
        raise Exception( 'isPrimeString: PRIMES[ -1 ] * PRIMES[ -1 ] < n' )
    else:
        return isPrime( n, PRIMES )

def isConcatenable( p0, p1 ):
    return isPrimeString( p0 + p1 ) and isPrimeString( p1 + p0 )

assert( isConcatenable( '3', '7' ) )
assert( isConcatenable( '109', '673' ) )
assert( not isConcatenable( '2', '37' ) )

def concatenableTuple( SIZE ):
# Returns the first set with size size of concatenable PRIMECS not exceeding maximum.