def pfactors_sub1000(n):
    for p in primes(1025):
        while n%p==0:
            yield p
            n=n//p
            if n==1: return
            if p>1000:
                yield n
                return
        return carmichael_1fact(pfd[0][0],pfd[0][1])
    else:
        lambda_list = []
        for fact in pfd:
            lambda_list.append(carmichael_1fact(fact[0],fact[1]))
        
        mult = lambda_list[0]
        for i in range (1,len(lambda_list)):
            mult = lcm(mult,lambda_list[i])
        
        return mult

total=8
Lsub_list = []
for n in range(2,total+1):
    primes_list = list(primes(n+1))
    #print primes_list
    
    lambda_list = []
    for i in range(len(primes_list)):
        #add lambda(p**a) for n%lambda(p**a) = 0 
        a=1
        lambda_list.append([carmichael_fact(primes_list[i]**a)])
        while n%lambda_list[i][a-1]==0:
            a+=1
            lambda_list[i].append(carmichael_fact(primes_list[i]**a))
    #print lambda_list
    
    Lsub_list_next = 1
    for i in range(len(primes_list)):
        lam_len = len(lambda_list[i])
    
    #len_num is actually 1 less than the length
    len_num = int(log10(n)//1)
    this_num = n
    for i in xrange(len_num):
        this_num = 10*(this_num%(10**len_num)) + (this_num//(10**len_num))
        circ_list.append(this_num)
    
    return circ_list
    
#total = 1000000
total = 1000000

#get all primes first
primes_list = []
for i in primes(total):
    if allDigitsOdd(i):
        primes_list.append(i)


count_circ = 0
for i in primes_list:
    b_all_circ_prime = 1
    for j in circleList(i):
        if not j in primes_list:
            b_all_circ_prime = 0
            break
    
    if  b_all_circ_prime:
        count_circ +=1
'''
Created on Nov 12, 2015

@author: Trader
'''
from PE003_Largest_Prime_Factor import primes, divisorGen
from PE003_Largest_Prime_Factor import factorGen
from skimage.viewer.plugins.color_histogram import pct_total_area
from numpy import prod

all_p_list = list(primes(10000))

n_list = range(30,500)
n_list = [2*3*5*7*11*13*17,2*3*5*7*11*13*17*19,2*3*5*7*11*13*17*19*23, 2*3*5*7*11*13*17*19*23*29,2*3*5*7*11*13*17*19*23*29*31]

'''
n_list_mult = [2,3,5,7,11,13]
n_list_mult = [1]
for i in n_list_mult:
    n_list.append(i*2*3*5*7*11*13)
'''
 
#for i in xrange(len(p_list)):
for n in n_list:
    #looking for a fraction less than 15499/94744
    #once we find the fraction, look for the true lowest denom in the range
    all_p_list = list(primes(n))
    
    #get divisors for prod
    div_list = [a for (a,b) in factorGen(n)]
    #print div_list
            combo.append(this_num/base_n_list[j])
            this_num = this_num % base_n_list[j]
        yield combo

b_get_pfd_ceil_pow = 1
b_create_combos = 1
b_list_combos = 0
b_run_main = 0

#get R for 2*3*5*7
n = 2*3*5*7


pfd = prime_fact_decomp(n)
pfd_list = [a for (a,b) in pfd]
p_list = [x for x in primes(n) if x not in pfd_list]
p_list.sort(reverse=True)
print len(p_list)

if b_get_pfd_ceil_pow:
    pfd_ceil_pow = []
    #get min a st p**a > n
    for p in p_list:
        pfd_ceil_pow.append(int(floor(log(n,p))))
#print len(pfd_ceil_pow)

if b_create_combos:

    all_combo = []
    len_n_list = [x+1 for x in pfd_ceil_pow]
    
'''
Created on Nov 24, 2015

@author: Trader
'''
from PE003_Largest_Prime_Factor import primes,factorGen

b_try1 = 0
b_try2 = 1

target = float(15499)/float(94744)
print float(15499)/float(94744)
p_list = list(primes(100))
#print p_list
print ''
print 2*3, 1-float(4-1)/float(2*3-1)
print 2*3*5, (4*5-1) + (2), float(2*3*5 - (4*5-1 + 2)-1)/float(2*3*5-1)
print float(8)/float(29)
print ''

if b_try1:
    this_num = 2*3
    this_divs = 4
    this_p_list =[2,3] 
    
    for i in p_list[2:100]:
        this_p_count = this_num - this_divs
        this_divs = (this_divs * i) + this_p_count 
        this_num = this_num*i
        this_R = float(this_num - this_divs) / float(this_num-1)
        print i,this_p_count,this_divs,this_num,this_R
def pfactors(n):
    for p in primes(n):
        while n%p==0:
            yield p
            n=n//p
            if n==1: return
def pfactors(n):
    for p in primes(n):
        while n%p==0:
            yield p
            n=n//p
            if n==1: return

'''
p_list = list(primes(1000))
print len(p_list)

for i in p_list:
    for i in range(100):
'''     
p_mast_list = primes(1000)
p_list = []
i_list = []
for i in range(2,10000):
    this_list = list(pfactors_sub1000(10**i-1))
    print i, this_list
    for j in this_list:
        print (j,i)
        if j<1000 and j not in p_list:
            #print (j,i)
            p_list.append(j) 
            i_list.append(i)
 
print len(p_list)

print p_list
    else:
        lambda_list = []
        for fact in pfd:
            lambda_list.append(carmichael_1fact(fact[0],fact[1]))
        
        mult = lambda_list[0]
        for i in range (1,len(lambda_list)):
            mult = lcm(mult,lambda_list[i])
        
        return mult

total=100

t0 = time.time()
#get all primes first
primes_list = list(primes(total+1))
#print primes_list
t1 = time.time()
print 'have ' + str(total+1) + ' primes: ' + str(t1-t0)

#have one lambda list, shared by all calculations for Lsub
#initialize the list with lambda(p**a) for a=1
#where we need lambda(p**a) for a>1, we will add these values as needed
lambda_list = []
for i in primes_list:
    lambda_list.append([carmichael_1fact(i,1)])
t2 = time.time()
print 'have ' + str(total+1) + ' lambda(p**1) values: ' + str(t2-t1)

Lsub_list = []
for n in range(2,total+1):