def IsPhiStrong2(phi): #if phi == 3132: print "here's 3132" #z = int(sqrt(phi))+2 # int(log(phi)/log(2))+1 z = int(phi / 4) + 1 #if phi == 2628: # print "Z:",z;exit() ctr = 0 for v in xrange(2, z): if not IsPrime(v): continue if phi % v == 0: #print "Fact found", v u = phi / v if IsPrime(u): if phi % (u * u) == 0: ctr += 1 else: return False if phi % (v * v) == 0: ctr += 1 else: #print "GRR",v return False #if phi==5000:print "ctr 5000",ctr if ctr > 1: return True #if phi%23==0 and (phi/529)*529!=phi: return False return False
def right(n): while len(str(n)) > 1: if n in TruncPrimes: return True if not IsPrime(n): return (False) n = int(str(n)[:-1]) return (IsPrime(n))
def left(n): while len(str(n)) > 1: if n in TruncPrimes: return True if not IsPrime(n): return (False) n = int(str(n)[1:]) return (IsPrime(n))
def TestPair(a, b): if not IsPrime(int(str(a) + str(b))): return (False) if not IsPrime(int(str(b) + str(a))): return (False) return (True)
def TestPair(a, b): if not IsPrime(int(str(a) + str(b))): return False if not IsPrime(int(str(b) + str(a))): return False return True
def F0(w, h): if IsPrime(w) and IsPrime(h): return 0 if w + h == 2: return 0 if w == 1 or h == 1: return 1 sum2 = 0 if w % (h - 1) == 0: sum2 += 1 if h > 1 and h % 2 == 0: sum2 += 1 if w > 1 and w % 2 == 0 and w != h: sum2 += 1 if sqrt(w) == h * 2: sum2 += 1 return sum2
def f(n): if not IsPrime(1 + n): return False if not IsPrime(2 + n / 2): return False D = divisors(n) D = D[:len(D) / 2] #l = len(D) for d in D: if not IsPrime(d + n / d): return False return True
def Solve(): beg = 1489 while beg < 10000: then = beg + 3330 end = then + 3330 if set([x for x in str(beg)]) == set([x for x in str(then)]) == set( [x for x in str(end)]): if IsPrime(beg) and IsPrime(then) and IsPrime(end): return (int(str(beg) + str(then) + str(end))) beg += 2
def IsTruncatable(num): ntrunc = True nstr = str(num) nlen = len(nstr) for i in range(0, nlen): if not IsPrime(int(nstr[i:])): return False if not IsPrime(int(nstr[:i + 1])): return False return True
def ConcPrimes(p1, p2): chk1, chk2 = False, False #print "Q",int(str(p1)+str(p2)),IsPrime(int(str(p1)+str(p2))) #print "T",int(str(p2)+str(p1)),IsPrime(int(str(p1)+str(p2))) #print "!",len(primes) if not IsPrime(int(str(p1) + str(p2))): return False if not IsPrime(int(str(p2) + str(p1))): return False return True
def pureFact(n): ilist = [] numfacts = 0 nstr = str(n) if n % 2 == 0: numfacts += 1 ilist.append(2) #ilist[0] = 1 #print n, list for i in range(3, int(n / 2) + 1, 2): if IsPrime(i) == False: continue if n % i == 0: if not i in ilist: numfacts += 1 ilist.append(i) #print n, ilist if numfacts >= 4: #print "#########" #print "n,Numfacts",n, numfacts return True else: return False
def factor3(N): N += 2 eqn_sieve = [k * k for k in xrange(N)] print eqn_sieve print eqfactlist = [()] * N print "!!!", type(eqfactlist[2]), eqfactlist[2] primes = RofPrimes(2, N + 1) for k in xrange(2, N): num = eqn_sieve[k] if num > 1: # number still needs factoring if IsPrime(k): j = k else: continue #j = primes[k] while j < N: while eqn_sieve[j] % j == 0: eqn_sieve[j] /= j z = eqfactlist[j] z += eval(str(j) + ",") eqfactlist[j] = z j += j return eqfactlist
def next_prime(n): while 1: if IsPrime(n): #print "prime",i return n else: n-=1
def Solve(): primes = [2] test = 1 while len(primes) <= 10_000: test += 2 if IsPrime(test): primes.append(test) return test
def primeproof(n,j,i): if n%2==0 or n%5==0: m=int(n/10.)*10 for p in [1,3,7,9]: #print n,m+p if IsPrime(m+p):return False else: s = str(n) l = len(s) for k in xrange(l-1): for l1 in xrange(10): if k==0 and l1==0:continue n1=int(s[:k]+str(l1)+s[k+1:]) if IsPrime(n1): #print "hit!",j,i,":",n,n1 return False return True
def PrimeCount(a, b): n = 0 def f(n): return (n**2 + a * n + b) while IsPrime(f(n)): n += 1 return (n)
def Solve(): # don't test 9 digits numbers. sum([1:9]) = 45 -> all 9-digit pandigital numbers are divisble by 9 for length in range(8, 0, -1): digits = range(1, length + 1) numbers = [x for x in permutations(digits)][::-1] for n in numbers: test = int(''.join([str(x) for x in n])) if IsPrime(test): return (test)
def Solve2(): limit = 10000 composites = [x for x in range(2,limit) if not IsPrime(x)] def totient(x): return len([p for p in range(1, x) if GCD(x,p) == 1]) ratios = [y/totient(y) for y in composites] m = max(ratios) return(composites[ratios.index(m)])
def Solve(): pos = 1 width = 2 prime = 0 total = 1 while True: for i in range(3): pos += width if IsPrime(pos): prime += 1 pos += width #we know this one isn't prime; it's a perfect square total += 4 if prime * 10 < total: return width + 1 width += 2
def next_prime(n): from Functions import IsPrime if n==1:return 2 n+=1 while 1: if IsPrime(n):return n if n%2==0: n+=1 else: n+=2
def Solve(): def PrimeCount(a, b): n = 0 def f(n): return (n**2 + a * n + b) while IsPrime(f(n)): n += 1 return (n) Counts = dict([(PrimeCount(a, b), a * b) for a in range(-999, -1, 2) for b in [p for p in range(1, 999, 2) if IsPrime(p)]]) return (Counts[max(Counts.keys())])
def consPrimes(a, b): numberofPrimes = 0 stillPrime = True ctr = 0 while stillPrime == True: x = ctr**2 + a * ctr + b if IsPrime(x): ctr += 1 else: stillPrime = False break return ctr
def IsGoldbach(n, showfact=False): isGold = False sq = int(sqrt(n) * sqrt(2)) for k in range(1, sq): x = n - 2 * (k * k) if IsPrime(x): isGold = True break if showfact == True: print n, k, x, x + (2 * (k**2)) return isGold
def IsGoldbach(n, showfact=False): isGold = False for k in range(2, n): if IsPrime(k): x = n - k y = x / 2 z = int(round(sqrt(y), 4)) if showfact: print n, k, x, y, z, k + (2 * (z**2)) if k + (2 * (z**2)) == n: isGold = True break return isGold
def FactorN(n): from Functions import IsPrime if IsPrime(n): return [1] Facts=[1] if float(n)/2.0==n/2: endrange = n/2 + 1 else: endrange = int(round(n**0.5,2)) + 1 #ctr=0 for i in range(2, int(n/2) + 1): if n%i==0: #ctr+=1 Facts.append(i) return Facts
def Solve(): primes = set([2]) n = 1 while True: n += 2 if IsPrime(n): primes.add(n) else: stop = True for p in primes: diff = n - p if ((diff/2)**.5).is_integer(): stop = False break if stop: return(n)
def pureFact(n): ilist = [] numfacts = 0 nstr = str(n) n0 = n if n % 2 == 0: numfacts += 1 ilist.append(2) n = n/2 #ilist[0] = 1 #print n, list #for i in range(3,int((n/30.))+1,2): i = 3 while i <= n: if IsPrime(i) == False: i+=2 continue #if i == n: break if n % i == 0: n = n / i if not i in ilist: numfacts += 1 ilist.append(i) #print n, ilist i+=2 if numfacts==4: #print "#########" #print "n,Numfacts",n, numfacts return True else: return False
def Solve2(): target = 5 # cheat: use the finite sieve since I already know the answer. sieve = PrimeSieve(10**4) # prime the data so that we exclude 2 and 5. 2 and 5 will never be part of a set that meets the condition. # this invalidates a target value of 1, but that case is trivial (it's 2). as we enter the loop, it will look like # we ran the loop to generate 2,3,5,7 and then dropped 2 and 5 from primes and removed sets containing them from # valid sets primes = [next(sieve) for x in range(4)] primes.remove(2) primes.remove(5) validSets = [{3}] primeIndex = len(primes) - 1 while True: nextPrime = primes[primeIndex] while nextPrime >= max(primes): primes.append(next(sieve)) nextValidSets = validSets[:] append = nextValidSets.append append({nextPrime}) pairsWith = set() add = pairsWith.add for p in primes: if IsPrime(int(str(p) + str(nextPrime))): if (int(str(nextPrime) + str(p))) in primes: add(p) for currentSet in validSets: if currentSet - pairsWith == set(): candidateSet = set(currentSet) candidateSet.add(nextPrime) if len(candidateSet) >= target: return sum(candidateSet) append(set(candidateSet)) validSets = nextValidSets primeIndex += 1
def Solve(): target = 8 replace = target - 5 length = replace + 1 while True: fixedDigitCount = length - replace # the last digit must be fixed; otherwise too many of the substitutions will be even fixedDigitIndexes = permutations(range(length - 1), fixedDigitCount - 1) fixedDigitIndexes = [list(x) + [length - 1] for x in fixedDigitIndexes] # the last digit must be one of [1,3,7,9]; otherwise all substitutions will be not prime fixedDigitValues = permutations(range(10), fixedDigitCount - 1) fixedDigitValues = sum([[list(x) + [y] for y in [1, 3, 7, 9]] for x in fixedDigitValues], []) # print(fixedDigitIndexes) # print(fixedDigitValues) # return 0 for fixedDigitIndexSet in fixedDigitIndexes: for fixedDigitValueSet in fixedDigitValues: testDigits = ['x'] * length for i in range(len(fixedDigitIndexSet)): testDigits[fixedDigitIndexSet[i]] = str( fixedDigitValueSet[i]) count = 0 for x in range(9, -1, -1): if x == 0 != fixedDigitIndexSet[0]: break testValue = int(''.join(testDigits).replace('x', str(x))) if IsPrime(testValue): count += 1 if count == target: return testValue length += 1
# # Euler 49 # # from itertools import * from Functions import IsPrime for i in range(1000, 3000): if IsPrime(i) and i != 1487 and i != 1847: ilist = list(set(list(permutations(str(i))))) #ilist = list(permutations(str(i))) #ilist = int("".join(ilist)) plist = [] for k in xrange(len(ilist)): x = int("".join(ilist[k])) if x > 999 and IsPrime(x): plist.append(x) #print x if len(plist) > 2 or not (1487 in plist): plist.sort() #print "Prime List",i,")",plist #print type(plist[0]), len(plist) dlist = [] for u in plist: