Exemple #1
0
def primeGen(n):
    pr = 2
    cnt = 0
    while (1):
        if (prime(pr) == True):
            cnt += 1
            yield pr
        pr += 1
        if (cnt >= n):
            break
Exemple #2
0
def solve():
  primes = []
  m = (float('inf'), 1)
  for p1 in prime(math.sqrt(N/10), math.sqrt(10*N)):
    for p2 in primes:
      n = p1*p2
      if n >= N:
        break
      totient = (p1-1)*(p2-1) # since p1 and p2 are primes
      if sorted(str(n)) == sorted(str(totient)):
        m = min(m, (1.*n/totient,n))
    primes.append(p1)
  return m
Exemple #3
0
def brute_force():
  primes = [673,109,7,3]
  for p in prime(673,1e6):
    ps = str(p)
    for pp in primes:
      pps = str(pp)
      if not is_prime(int(pps+ps)):
        break
      if not is_prime(int(ps+pps)):
        break
      if pps != '673':
        print p, "good for", pps
    else:
      return p
  return "[result]"
Exemple #4
0
def solve():
  root = Node(0)
  for p in prime(10000):
    root.children.append(Node(p, parent=root))
  print "primes loaded"

  def walk_graph(node):
    for i in range(len(node.children)):
      ci = node.children[i]
      si = str(ci.value)
      for j in range(i,len(node.children)):
        cj = node.children[j]
        sj = str(cj.value)
        if ci.value < cj.value and is_prime(int(si+sj)) and is_prime(int(sj+si)):
          ci.children.append(Node(cj.value, parent=ci))
      if ci.steps() < 5:
        if len(ci.children) and len(ci.children) >= 5-ci.steps():
          walk_graph(ci)
      else:
        print ci.sum(), ci
        return ci.sum()

  return walk_graph(root)
Exemple #5
0
def solve():
    print sum([p for p in prime(limit=2e6)])
Exemple #6
0
#
# There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
#
# How many circular primes are there below one million?

from prime import *
from permut import *

import time

def is_circular_prime(x):
  x = list(str(x))
  if len(x) == 1: 
    return True
  if filter(lambda i: int(i)%2==0, x):
    return False
  for p in circular_permut(x):
    if not is_prime(int(p)):
      return False
  return True

# test
# for p in prime(100):
#   if is_circular_prime(p):
#     print p

t = time.time()
l = [p for p in prime(1e6) if is_circular_prime(p)]
print len(l)
print "time: %f s" % (time.time() - t)
Exemple #7
0
  group, i = [], 1
  amodn = a
  while amodn != 1:
    amodn = (amodn*a) % n
    group.append(amodn)
    i += 1
  group.append(amodn)
  return group

#test
# test = [1, 3, 5, 9, 11, 13]
# for t in test:
#   print t, find_group_of_units(t, 14)

t_start = time.time()

d, group_of_units = 7, find_group_of_units(10, 7)
for p in prime(1000):
  if p > d:
    new_group_of_units = find_group_of_units(10, p)
    if len(new_group_of_units) > len(group_of_units):
      d = p
      group_of_units = new_group_of_units
    #print "d=%d (group of units #%d)" % (p, len(new_group_of_units))

t_end = time.time()

print "\nMAX"
print "d = %d (group of units #%d)" % (d, len(group_of_units))
print "time : %f sec" % (t_end - t_start)
Exemple #8
0
def factor(n):
    for i in range(2, int(n**0.5)):
        if n % i == 0 and prime(i):
            result.append(i)
    return result
Exemple #9
0
#!/usr/bin/env python
#
# The Euler Project: problem 7
#
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
#
# What is the 10 001st prime number?

from prime import *

i = 0
for p in prime(limit=1e6):
  i += 1
  if i == 10001:
    break

print i, p
Exemple #10
0
            if oper_1 == 'n':
                break

    elif oper == 'extgcd':
        while 1:
            a, b = input("Please enter two number -> ").split()
            a, b = int(a, 16), int(b, 16)
            tmp = ojld(a, b)
            oper_1 = input('Go on ? [y/n]')
            if oper_1 == 'n':
                break

    elif oper == 'prime':
        while 1:
            a = int(input('Pleaase enter your number -> '))
            tmp = prime(reduci(a))
            print("Output: ")
            for i in tmp:
                print("(" + bin(i) + ',' + hex(i) + ') ')
            oper_1 = input('Go on ? [y/n]')
            if oper_1 == 'n':
                break

    elif oper == 'help':
        print('Hi,your choice can be like those:')
        print('ari,fast_mod,gcd,extgcd,ojld,prime')
        print('You can go back by input "back"')

    else:
        print('Error input! ')
Exemple #11
0
import prime
n = 19463
while not n == 0:
    n %= 10
    prime(n)
    n //= 10