Exemplo n.º 1
0
def smarter():
    a = sieve(100000)
    for k in np.arange(1, 20):
        for i in np.arange(0, 10):
            for j in np.arange(0, 9):
                c = a[0:9]
                c[j] = a[i]
                print i, j, k
                b = np.prod(c) * k
                if istriangle(b):
                    return b
Exemplo n.º 2
0
def solve(n=100, verbose=True):
    div = sieve(18)
    if verbose:
        assert check43(1406357289, div)
    pan = ''.join([str(a) for a in range(10)])
    result = 0
    for e in permutations(pan):
        e = ''.join(e)
        if check43(e, div):
            if verbose:
                print "found it!: ", e
            result = result + int(e)
    return result
Exemplo n.º 3
0
def smart(N, verbose=False):
    """
    Form product of primes up to N. Each prime factor has 
    a multiplicity that is given by its highest power that is still smaller than N.
    """
    result = 1
    logN = np.log(N)
    primes = sieve(N)
    for prime in primes:
        # every prime shows up ``exponent`` times in the final product
        exponent = np.floor(1. / np.log(prime) * logN)
        if verbose:
            print prime, exponent
        result *= prime**exponent
    return result
Exemplo n.º 4
0
def solve(n=1000000, verbose=False):
    results = []
    for num in sieve(n):
        if num not in results:
            if verbose:
                print "checking prime %i for circularity ..." % num
            relevant = True
            for rotated_prime in all_rotations(str(num)):
                if not is_prime(int(rotated_prime)):
                    relevant = False
            if relevant:
                if verbose:
                    print " ... it is circular!"
                    print rotations
                results += [int(e) for e in all_rotations(str(num))]
    result = len(np.unique(results))
    return result
Exemplo n.º 5
0
def dumb():
    #instead, generate all numbers with > 500 divisors and check if they are triangle numbers
    a = sieve(100000)
    b = np.prod(a[0:9])
    i = 1
    for crit in np.arange(10, 14):
        old = a[0:9]
        for e in itertools.permutations(a[0:crit]):
            f = sorted(e[0:9])
            print f, old, e
            if f != old:
                old = f
                b = np.prod(f)
                #b = b * i
                i = i + 1
                print f
                if istriangle(b):
                    print 'result: ', b
                    return b
Exemplo n.º 6
0
def bruteforce(n):
    a = sieve(20000000)
    start = time.time()
    total = i = 0
    trinum = 0
    j = 0
    while total < n:
        i = i + 1
        trinum = trinum + i
        if trinum != a[j]:
            total = findnumdiv(factors2(trinum))
            print total, i, trinum
        else:
            j = j + 1
    print 'result:'
    print i, trinum
    print 'elapsed time:'
    print time.time() - start
    return trinum
Exemplo n.º 7
0
def solve(verbose=True):
    """
    Find the smallest odd composite number that cannot be written
    as the sum of a prime and a two times a square.
    """
    upperLimit = 100000
    result = 1
    for num in xrange(2, upperLimit):
        if not is_prime(num) and num % 2:
            result = 0
            for e in sieve(num):
                for f in xrange(int(np.ceil(np.sqrt((num - e) / 2)) + 1)):
                    if verbose:
                        print num, e, f, result
                    result += num == e + 2 * f**2
        if not result:
            result = num
            break
    return result
Exemplo n.º 8
0
def solve(n=1000, verbose=False):
  #http://stackoverflow.com/questions/4545114
  # sqrt(1000000000) = 31622
  __primes = sieve(31622)
  def is_prime(n):
      # if prime is already in the list, just pick it
      if n <= 31622:
          i = bisect_left(__primes, n)
          return i != len(__primes) and __primes[i] == n
      # Divide by each known prime
      limit = int(n ** .5)
      for p in __primes:
          if p > limit: return True
          if n % p == 0: return False
      # fall back on trial division if n > 1 billion
      for f in range(31627, limit, 6): # 31627 is the next prime
            if n % f == 0 or n % (f + 4) == 0:
                return False
        return True

    result = 0
    winner = [0,0]
    for sign_a in [-1, 1]:
        for sign_b in [-1, 1]:
            for b in np.arange(0,1000):
                for a in np.arange(0, b):
                    b = sign_b * b
                    a = sign_a * a
                    stillprime = True
                    i = 0
                    while stillprime:
                        stillprime = is_prime(i*i + a*i + b)
                        i = i + 1
                    if i > result:
                        result = i
                        winner = a, b
                    if verbose:
                        print b, a, result, i
    return winner[0] * winner[1]
Exemplo n.º 9
0
                    default=1000000,
                    help='the main variable for our program')
parser.add_argument("-v",
                    "--verbose",
                    help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
n = args.n
start = time.time()
result = []

########

from Primes import is_prime, sieve
import gmpy
__primes = np.array(sieve(1000000))


def len_cons_primes(prim):
    #c  = sieve(prim)
    #print prim
    c = __primes[__primes < prim / 2]
    r = [0]

    for i in range(int(len(c)))[::-1]:
        a = prim
        j = i
        primsum = 0
        while primsum < prim and j < len(c):
            #a -= c[j]
Exemplo n.º 10
0
def solve(n=10000):
    return sieve(1000000)[n]