コード例 #1
0
ファイル: 50.py プロジェクト: slacker525600/project-euler
import pfp
#should probably just pickly the list of the first million primes ... but whatever. 

anPrimes = pfp.gen_prime_list(1000000)
print(len(anPrimes))

#can I search the whole space, start at max, back away, contain smallest, while this is too big keep reducing
nMaxLen = len(anPrimes) - 1

nToCheck = sum(anPrimes[:nMaxLen])
while nToCheck > anPrimes[-1]:
  #nToCheck = sum(anPrimes[:nMaxLen])
  nToCheck -= anPrimes[nMaxLen]
  nMaxLen -= 1
print(nMaxLen, 'first check') #

bFound = False
for nLen in range(nMaxLen,22,-1):
  #all ranged of lengths starting at max
  for nStart in range(0, len(anPrimes) - nLen): #start to end window sized
    nToCheck = sum(anPrimes[nStart:nStart+nLen])
    #could use same -= trick as above to speed this up, but not important pretty small search spaces. 
    if nToCheck in anPrimes:
      print('new winner ', nToCheck, nLen)
      bFound = True
      break
    if nToCheck > anPrimes[-1]: #no reason to keep searching range once past a million
      break
  if bFound:
    break 
    
コード例 #2
0
ファイル: 49.py プロジェクト: slacker525600/project-euler
def test(a,b,c):
  bToReturn = True
  if not pfp.is_prime(a):
    bToReturn = False
  if bToReturn and not pfp.is_prime(b):
    bToReturn = False
  if bToReturn and not pfp.is_prime(c):
    bToReturn = False
  if bToReturn and c-b != b-a:
    bToReturn = False
  comp_digits(a,b,c)



#start by generating list of all primes, 
anPrimes = pfp.gen_prime_list(9999)
#print(len(anPrimes), anPrimes)
#find arithmatic 
while anPrimes[0] < 1000:
  anPrimes.remove(anPrimes[0])

for  nIndex, nPrime in enumerate(anPrimes):
  #print(nIndex, nPrime)
  for nPrimeTwo in anPrimes[nIndex + 1:]:
    if (nPrimeTwo + (nPrimeTwo - nPrime)) in anPrimes:
      #all three are primes
      if comp_digits(nPrime,nPrimeTwo, nPrimeTwo + (nPrimeTwo - nPrime)):
        print(nPrime,nPrimeTwo, nPrimeTwo + (nPrimeTwo - nPrime))