Beispiel #1
0
    def run(self):
        print('Caching primes...')
        # NOTE: This is slow right now... To speed this up, I guess I should check if it's cyclical
        #        while calculating the primes
        primes = Helper.primesLessThan(1000000)
        print('Finding cyclical primes...')
        count = 0
        iterations = 0
        for prime in primes:
            if prime == 1:
                continue

            iterations = iterations + 1
            if iterations % 10000 == 0:
                print("I'm up to: " + str(prime))
                        
            if primes[prime]:
                next_prime = self.cycle(prime)
                is_circular = True
                counter = 0
                while int(next_prime) != prime:
                    counter = counter + 1
                    if counter > 1000:
                        print('I seem to be stuck:')
                        print(prime)
                        print(next_prime)
                    if not primes[int(next_prime)]:
                        is_circular = False
                    next_prime = self.cycle(next_prime)    
                    
                if is_circular:
                    count = count + 1
                    
        print(count)
Beispiel #2
0
    def run(self):
        MAX = 1000000

        primesDict = Helper.primesLessThan(MAX)
        primes = [p for p in primesDict.keys() if primesDict[p]]
        maxSequence = 2
        print(len(primes))
        for i in range(0, len(primes)):
            if len(primes) - i < maxSequence:
                break
            sum = primes[i]
            j = i
            while True:
                j = j + 1
                sum = sum + primes[j]
                if sum >= MAX:
                    break
                seqLength = j - i + 1
                if primesDict[sum] and seqLength > maxSequence:
                    print(
                        "Found new max sequence: p="
                        + str(sum)
                        + " seqLength="
                        + str(seqLength)
                        + " seqStart="
                        + str(primes[i])
                    )
                    maxSequence = seqLength
Beispiel #3
0
 def run(self):
     max = 1000000
     self.primes = Helper.primesLessThan(max)
     
     count = 0
     for i in range (11, max):
         if self.isPrimeReducable(i):
             print(i)
             count = count + 1
     print(count)
Beispiel #4
0
    def findAnswer(self):
        candidates = Helper.primesLessThan(10000)
        bCandidates = [k for k in candidates if candidates[k] == True and k <= 1000]
        bCandidates.reverse()
        aCandidates = range(-999, 1000)
        
        print('Searching with a values: ' + str(aCandidates[0]) + ' <-> ' + str(aCandidates[-1]))
        
        maxPair = (0, 0)
        maxN = 0
        
        for b in bCandidates:
            print('Testing b candidate: ' + str(b))
            for a in aCandidates:
                n = self.biggestNStillPrime(a, b)
                if n > maxN:
                    print('New biggest n: ' + str(n) + ' for pair: ' + str(a) + ',' + str(b))
                    maxN = n
                    maxPair = (a, b)

        print(self.calcQuadratic(maxN, maxPair))
Beispiel #5
0
    def run(self):
        MAX = 1000000
        print(self.findPerms(5603))
        
        print('Caching primes...')
        primesDict = Helper.primesLessThan(MAX);
        maxFamily = 6
        primes = [p for p in primesDict.keys() if primesDict[p] and p > 56003]

        print('Starting search of ' + str(len(primes)) + ' primes...')
        count = 0
        for prime in primes:
            if len(str(prime)) < 3:
                continue
            count = count + 1
            if count % 1000 == 0:
                print('Processing Prime number: ' + str(count))
            family = self.findPrimeFamily(prime, primesDict)
            if family > maxFamily:
                print(family)
                print(prime)
                maxFamily = family
Beispiel #6
0
def print_primes():
    increment = 35000000
    iterations = 5
    iteration = 1
    allNumbers = Helper.primesLessThan(increment)

    primes = [num for num in allNumbers if allNumbers[num]]

    # Because of memory constraints, I can't apply the seive all at once, instead I apply it incrementally
    while iteration < iterations:
        # First reset the map to free up the memory
        allNumbers = defaultdict(trueFactory)

        # Now we have to eliminate multiples of all primes found so far from the current range (as if the seive had been applied all along)
        iteration = iteration + 1
        start = (iteration - 1) * increment
        maximum = increment * iteration
        allNumbers[start] = Helper.isPrime(start)
        for prime in primes:
            i = int(start / prime) + 1
            while i * prime < maximum:
                allNumbers[i * prime] = False
                i = i + 1

        # Finally we are ready to continue with the seive as per normal
        for number in range(start, int(maximum / 2) + 1):
            if allNumbers[number]:
                i = 2
                while i * number < maximum:
                    allNumbers[i * number] = False
        # Add these new primes to the ones we already now about, rinse and repeat
        primes = primes + [num for num in range(start, maximum) if allNumbers[num]]

    f = open("./primes", "w")
    for prime in primes:
        f.write(str(prime) + "\n")
    f.close()