コード例 #1
0
ファイル: euler_49.py プロジェクト: beyoungwoo/C_sample
def E49():
    prime_perm = [ ]
    for i in range(1487, 10000):
        if is_prime(i) == False: continue

        plist = list(itertools.permutations(str(i)))
        for each_num in plist:
            num =  "".join(map(str, each_num))
            if is_prime(num) == True:
                prime_perm.append(num)

        len_p = len(prime_perm)
        for j in range(0, len_p-1):
            for k in range(j+1, len_p):
                if prime_perm[k] == prime_perm[j]: continue

                diff = int(prime_perm[k]) - int(prime_perm[j])
                next_num = int(prime_perm[k]) + diff

                if (str(next_num) in prime_perm) == True :
                    print prime_perm[j], prime_perm[k], next_num


        del prime_perm[:]

    return 0
コード例 #2
0
def eight_prime_family(prime, rd):
  c=0
  for digit in '0123456789':
    n = int(string.replace(prime, rd, digit))
    if (n>100000 and is_prime(n)):
      c=c+1
  return c==8
コード例 #3
0
ファイル: euler_70.py プロジェクト: beyoungwoo/C_sample
def E70():
    res = 0
    min_res = 100
    min_i = 100
    p_set = []
    for i in range (3, MAXNUM, 2):
        if is_prime(i) == True:
            continue

        p_set = factor(i)
        if len(p_set) != 2:
            continue

        if int(p_set[0][1])!=1 or int(p_set[1][1])!=1:
            continue

        # [(421, 1), (571, 1)] <- both are 1
        cnt = i - (int(p_set[0][0])+int(p_set[1][0])-1)
        if is_perm(i, cnt) == False:
            continue

        res = float(i)/float(cnt)
        if min_res >= res:
            min_res = res
            min_i = i
            #print min_res, min_i, cnt, (1, p_set[0][0], p_set[1][0], i)

    print "result", min_res, min_i
    return 0
コード例 #4
0
ファイル: euler_10.py プロジェクト: beyoungwoo/C_sample
def P(n):
    all_sum = 0
    i = 0
    while i <= n:
        if (is_prime(i)):
            all_sum += i
        i += 1

    return all_sum
コード例 #5
0
ファイル: euler_7.py プロジェクト: beyoungwoo/C_sample
def Get_P(n) :
    i = 2
    prime_th = 0
    while (1):
        if (prime_th == n):
            return (i-1)
            break

        if (is_prime(i)):
            prime_th += 1

        i += 1
コード例 #6
0
def project_euler_27():
    """Finds the product of a and b for that produces the most primes for 
    consecutive values of n, starting with n = 0. For n^2 + an + b where
    |a| < 1000 and |b| < 1000."""
    best = (0, -1001, -1001)

    for a in range(-999, 1000):
        for b in range(-999, 1000):
            n = 0
            while is_prime(abs(n*n + a*n + b)): n += 1
            if n > best[0]: best = (n, a, b)
    print(best[1]*best[2])
コード例 #7
0
ファイル: main.py プロジェクト: devinshields/Project-Euler
def pFact(num):
  pfactors = []
  while True:
    j = 2
    while j < num+1:
      if num%j==0 and is_prime(j):
        num = num/j
        pfactors.append(j)
        break
      j+=1
    if num==1:
      break
  return pfactors
コード例 #8
0
def project_euler_26():
    """Calculates the value of d < 1000 for which 1/d contains the longest
    recurring cycle in its decimal fraction part.
    
    Decimal fraction parts only repeat if d is relatively prime to 10,
    so we filter out non-primes from consideration. Next, the number
    of digits is equal to the multiplicative order of 10 mod d."""
    best, index = 0, 0

    for x in [i for i in range(10,1000) if is_prime(i)]:
        y = mult_order_mod(x)
        if y > best:
            index = x
            best = y
    print(index)
コード例 #9
0
ファイル: main.py プロジェクト: devinshields/Project-Euler
def main ():
  """
  
  """
  
  #
  primes  = [i for i in range(10**3,10**4) if is_prime(i) and '0' not in str(i)]
  #print primes
  
  for p in primes:
    perms = sorted(list(set([int(''.join(perm)) for perm in permutations(str(p)) if is_prime(int(''.join(perm)))])))
    if len(perms) < 3:continue

    diffDiff = [b-a for a,b in pairwise([y-x for x,y in pairwise(perms)])]
    if 0 in diffDiff:

      print p,perms
      print p,diffDiff
      print 

  
  
  
  pass
コード例 #10
0
ファイル: euler_46.py プロジェクト: beyoungwoo/C_sample
def FindPrimeAndTwiceSquare(n):
    max_twice = math.sqrt(n)
    print n, max_twice
    for i in range(1, int(max_twice)):
        if (2 * (i **2)) > n:
            break

        prime = n - (2 * (i ** 2))
        print prime, n
        if (is_prime(prime)) == True:
            print i, "=", prime, "+", "2 x ", i, "^2"
            return True
        #else :
        #    print "not....",i, "=", prime, "+", "2 x ", i, "^2"

    return False
コード例 #11
0
ファイル: main.py プロジェクト: devinshields/Project-Euler
def main ():
  """
  
  """

  maxN = 10**6+1
  maxConsecTerms = 0
  maxPrime = 0

  # make a list of primes
  primes = [i for i in range(2,maxN) if is_prime(i)]

  

  for j,p in enumerate(primes):
    print j/780.

    # make a list of the candidate primes
    cand = [c for c in primes[:j]]
  
    # starting at each prime in the candidate list...
    consecTerms=0
    for ind,c in enumerate(cand):
      candSum = 0
      i = ind
      while candSum < p and i < len(cand):
        candSum += cand[i]
        i       += 1
      # aggregate terms until they equal or exceed the target prime
      #print p,c,candSum
      
      consecTerms = i-ind

      if p == candSum:
        if consecTerms > maxConsecTerms:
          maxPrime = p
          maxConsecTerms = consecTerms
          
          #print p,i-ind #,sum(cand[ind:i]),cand[ind:i]
        break
      continue
    print '****',maxPrime,maxConsecTerms
  print 'And the answer is:',maxPrime,'with a total of',maxConsecTerms,'consecutive terms.'
  pass
コード例 #12
0
ファイル: euler_35.py プロジェクト: beyoungwoo/C_sample
def CirPrime(num):
    buf = str(num)
    len_buf = len(buf)

    devide = 1
    for i in range (0, len_buf-1):
        devide *= 10

    new_num = num
    for i in range(0, len_buf):
        #print new_num
        if (is_prime(new_num)) :
            d1 = new_num / devide
            d2 = new_num % devide
            new_num = d2 * 10 + d1
        else :
            return -1

    return 0
コード例 #13
0
ファイル: euler_69.py プロジェクト: beyoungwoo/C_sample
def E69():
    res = 0
    max_res = 0
    max_i = 0
    for i in range (2, MAXNUM):
        if i % 2 == 1:
            continue;
        if is_prime(i) == True:
            continue
        cnt = 1
        for j in range (1, i):
            if gcd(i, j) == 1:
                cnt+=1
        if cnt != 0:
            res = float(i)/float(cnt)
            if max_res < res:
                max_res = res
                max_i = i

    print max_res, max_i
    return 0
コード例 #14
0
ファイル: hello.py プロジェクト: devinshields/Project-Euler
def right_prime(n):
  if len(str(n)) ==1:return is_prime(n)
  return is_prime(n) and right_prime(int(str(n)[1:]))
コード例 #15
0
ファイル: hello.py プロジェクト: devinshields/Project-Euler
def left_prime(n):
  if len(str(n)) ==1:return is_prime(n)
  return is_prime(n) and left_prime(int(str(n)[:-1]))
コード例 #16
0
ファイル: main.py プロジェクト: devinshields/Project-Euler
#!/usr/bin/python

from Euler import is_prime
from time import clock

# start a process timer
clock()

# setup a counter
max = 0

# get a list of primes
z = [n for n in range(10**6+1) if is_prime(n)]
lenght = len(z)

for a in range(0, lenght-1):
  #
  t = 0
  sum = 0
  wyrazy = 0

  #
  for i in range(a, lenght-1):
    sum = sum + z[i]
    if sum > 10**6:
      break
    wyrazy += 1
    if is_prime(sum) and sum != z[i] and wyrazy > max:
      r = sum
      max = wyrazy
print(r)
コード例 #17
0
ファイル: genprimes.py プロジェクト: BlackLight/ProjectEuler
#!/usr/bin/python

from Euler import is_prime
from sys import stdout

print "{ ",

for n in range(2,10**6):
	if is_prime(n) :
		stdout.write("'%s': 1, " % (str(n)))

print "}"

コード例 #18
0
ファイル: p50_2.py プロジェクト: zjj609/ProjectEulerPractices
from Euler import prime_sieve, is_prime

max = 1000000
primes = prime_sieve(max)
prime_sum = [0]

sum = 0
count = 0
while (sum<terms and is_prime(n)):
            (terms, max_prime) = (j-i, n)

print "Project Euler 50 Solution = ", max_prime, " with ", terms, " terms"
コード例 #19
0
ファイル: euler_27.py プロジェクト: beyoungwoo/C_sample
<div class="info" style="text-align:left;">where |<i>n</i>| is the modulus/absolute value of <i>n</i><br />e.g. |11| = 11 and |&minus;4| = 4</div>
</blockquote>
<p>Find the product of the coefficients, <i>a</i> and <i>b</i>, for the quadratic expression that produces the maximum number of primes for consecutive values of <i>n</i>, starting with <i>n</i> = 0.</p>
</div><br />
<br /></div>
"""

from Euler import is_prime

max_cnt = 0
max_a = 0
max_b = 0

for a in range (-1000, 1001):
    for b in range (-1000, 1001):
        n = 0
        while (True):
            result = (n*n) + (a*n) + b
            if (is_prime(result)):
                n += 1
            else:
                if (max_cnt < n):
                    max_cnt = n
                    max_a = a
                    max_b = b
                break


print max_cnt, max_a, max_b
print "result=", (max_a * max_b)
コード例 #20
0
ファイル: hello.py プロジェクト: devinshields/Project-Euler
#!/usr/bin/python -tt
import sys
sys.path.append('/Users/admin/Desktop/python-work/Project_Euler/primes')

from Euler import prime_sieve, is_prime
 
nmax = 0;
primes = prime_sieve(1000)
for a in range(-999,999,2):
  for b in primes:
    n = 1
    while is_prime(n**2 + a*n + b): n += 1
    if n>nmax: nmax, p = n, a*b
 
print "Answer to PE27 = ",p

def main():
  print 'hello'

# Standard boilerplate to call the main() function.
if __name__ == '__main__':
  main()

コード例 #21
0
ファイル: euler_41.py プロジェクト: beyoungwoo/C_sample
                    continue
                BUF[a4] = 1
                for a5 in range(1, 8):
                    if (BUF[a5] == 1):
                        continue
                    BUF[a5] = 1
                    for a6 in range(1, 8):
                        if (BUF[a6] == 1):
                            continue
                        BUF[a6] = 1
                        for a7 in range(1, 8):
                            if (BUF[a7] == 1):
                                continue
                            count += 1
                            buf = "%d%d%d%d%d%d%d" % (a1,a2,a3,a4,a5,a6,a7)
                            if (is_prime(int(buf)) == True):
                                print "prime = ", buf
                            BUF[a7] = 0
                        BUF[a6] = 0
                    BUF[a5] = 0
                BUF[a4] = 0
            BUF[a3] = 0
        BUF[a2] = 0


'''
for a1 in range(1, 10):
    BUF = [0,0,0,0,0,0,0,0,0,0]
    BUF[a1] = 1
    for a2 in range(1, 10):
        if (BUF[a2] == 1):
コード例 #22
0
ファイル: 124.py プロジェクト: BlackLight/ProjectEuler
def genPrimes(maxN):
	primes = []
	for n in range(2, maxN+1):
		if is_prime(n):
			primes.append(n)
	return primes
コード例 #23
0
ファイル: problem-050.py プロジェクト: skensell/project-euler
            chart[(i,j)] = result
            return result
        else:
            result = sum_of_primes(i-1,j) - primes[i-1]
            chart[(i,j)] = result
            return result


(highest, num_of_terms) = (41,6)
starting_prime_index = 0
for i in xrange(0,4): #index of the starting prime
    m = num_of_terms
    for j in xrange(i+m,550): #index of the ending prime
        the_sum = sum_of_primes(i,j)
        if the_sum > 1000000: break #increment here to test
        if is_prime(the_sum):
            highest = the_sum
            num_of_terms = j-i
            starting_prime_index = i


print highest, "is the sum"
print num_of_terms, "terms in the sum"
print "The sum starts at the prime", primes[starting_prime_index]
print time.clock() - t_0, " seconds to complete"


##k=1000000
##while not is_prime(k):
##    k -= 1
##
コード例 #24
0
def project_euler_3():
    '''Prints the largest prime factor of 600851475143.
    See the is_prime method in Euler.py for more details.'''
    print(max(i for i in range(1, int(600851475143**.5) +1) if 600851475143 % i == 0 and is_prime(i)))
コード例 #25
0
ファイル: euler_46.py プロジェクト: beyoungwoo/C_sample
from Euler import is_prime 
import math

def FindPrimeAndTwiceSquare(n):
    max_twice = math.sqrt(n)
    print n, max_twice
    for i in range(1, int(max_twice)):
        if (2 * (i **2)) > n:
            break

        prime = n - (2 * (i ** 2))
        print prime, n
        if (is_prime(prime)) == True:
            print i, "=", prime, "+", "2 x ", i, "^2"
            return True
        #else :
        #    print "not....",i, "=", prime, "+", "2 x ", i, "^2"

    return False

MAX_LIMIT = 1000000
count = 0
for i in range(3, MAX_LIMIT, 2):
    if (is_prime(i) == True):
        continue

    if (FindPrimeAndTwiceSquare(i) == False):
        print "(DONE) result = [",i,"]"
        exit(0)
コード例 #26
0
'''
What is the side length of the square spiral for which the ratio of primes
along both diagonals first falls below 10%?
'''

from Euler import is_prime, itertools

# consider a layer N
# for example, N = 3
# .......         .......
# .NNNNN.    S    .C...C.
# .N...N.    S    ..XXXX.
# .N...N. => S => ..XXXX.
# .N...N.    S    ..XXXX.
# .NNNNN.    S    .C.....
# .......         .......

count = 0
for N in itertools.count(start=2):
    S = 2*N-1
    X = (S-1)*(S-2)
    i = X+1
    count += sum(is_prime(i+j*(S-1)) for j in range(3))
    total = 4*N-3
    if count/total < 0.1:
        break

ans = S
print(ans)
コード例 #27
0
ファイル: problem-027.py プロジェクト: skensell/project-euler
#27. Come up with a formula n^2 + an + b for |a|,|b|<=1000 which
#generates the most number of consecutive primes starting from n=0.

## Here's the output:
##-59231  is the product of a and b
##70  is the maximum consecutive number of primes
##8.589944  seconds to complete


from Euler import is_prime
import time
import cProfile

t_0 = time.clock()

max_consecutive = 0

for a in xrange(-1000,1001):
    for b in xrange(-1000,1001):
        for n in xrange(80):
            if not is_prime(n**2+a*n+b):
                break
            elif n > max_consecutive:
                max_consecutive = n+1
                if max_consecutive==70:
                    print a*b, " is the product of a and b"


print max_consecutive, " is the maximum consecutive number of primes"
print time.clock()-t_0, " seconds to complete"
コード例 #28
0
ファイル: euler_3.py プロジェクト: beyoungwoo/C_sample
"""
<div id="content">
<div style="text-align:center;" class="print"><img src="images/print_page_logo.png" alt="projecteuler.net" style="border:none;" /></div>
<h2>Largest prime factor</h2><div id="problem_info" class="info"><h3>Problem 3</h3><span>Published on Friday, 2nd November 2001, 06:00 pm; Solved by 281868; Difficulty rating: 5%</span></div>
<div class="problem_content" role="problem">
<p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143 ?</p>
<!--
Note: This problem has been changed recently, please check that you are using the right number.
-->
</div><br />
<br /></div>
"""


from Euler import is_prime

#limit = 13195
limit = 600851475143

i = 3
max_num = 0
for i in range (i, 10000):
    if (is_prime(i)):
        if (limit % i == 0):
            temp = limit // i
            max_num = i


print "max =", max_num