def primeAndTwiceSq(n): for sumPart in sieveOfEratosthenes(n): twiceSquare = n- sumPart if twiceSquare%2==0: if sqrt(twiceSquare/2)==int(sqrt(twiceSquare/2)): return [sumPart,2,twiceSquare/2] return[n,-1,-1]
## SOLVED # How many numbers below fifty million can be expressed as the # sum of a prime square, prime cube, and prime fourth power? # http://projecteuler.net/problem=87 from time import clock from samsPrimeTools import sieveOfEratosthenes # start timer st=clock() # sieve for primes up to sqrt(50m) primes=sieveOfEratosthenes(7072) # remove zeroes for z in range(primes.count(0)): primes.remove(0) # set object will not contain # duplicated entries S=set() # limit is fourth root of 50m # this won't BE 85 since the # zeroes have been removed for f in primes[:85]: if f<85:
# a permutation of 79180. # Find the value of n, 1 < n < 107, for which phi(n) is a # permutation of n and the ratio n/phi(n) produces a minimum. from samsPrimeTools import sieveOfEratosthenes def ispermutation(a,b): if sorted(str(a))==sorted(str(b)): return True else: return False print 'Sieving' primes=sieveOfEratosthenes(4000) for i in range(primes.count(0)): primes.remove(0) print 'Finding totients' i=0 minratio=2 for p1 in primes: i+=1 for p2 in primes[i:]: n=p1*p2 if n > 10**7:break phi=(p1-1)*(p2-1) if n==87109:print n,phi
# irrational. The decimal expansion of such square # roots is infinite without any repeating pattern at # all. # # The square root of two is 1.41421356237309504880.. # and the digital sum of the first one hundred # decimal digits is 475. # # For the first one hundred natural numbers, find # the total of the digital sums of the first one # hundred decimal digits for all the irrational # square roots. from samsPrimeTools import sieveOfEratosthenes from decimal import * getcontext().prec = 100 primes=sieveOfEratosthenes(100) t=0 for p in primes: if p!=0: s=str(Decimal(p).sqrt()) s=s[2:] for d in s: t+=int(d) print t
# 33 = 3**2 + 2**3 + 2**4 # 49 = 5**2 + 2**3 + 2**4 # 47 = 2**2 + 3**3 + 2**4 # How many numbers below fifty million can be expressed as the sum # of a prime square, prime cube, and prime fourth power? # ============================================================== from samsPrimeTools import sieveOfEratosthenes #upper limit is the square root of fifty million upper= int(5e7**(0.5))+1 print 'Sieving primes' primes=sieveOfEratosthenes(upper) print 'Removing zeros' for z in range(primes.count(0)): primes.remove(0) print primes print 'Searching' s=set() for a in primes: for b in primes: for c in primes: t=(a**2) + (b**3) + (c**4) if t<50:s.add(t)