def IsPractical(n):

    if n % 2 != 0: return False

    #D = divisors(n)
    #if sum(D) == n*2: return True

    lg = log(n) / log(2)
    if lg == int(lg): return True

    F = RetFact(n)
    S = sorted(list(set(F)))

    for k in range(1, len(S)):
        a = S[k]
        f = S[:k]
        v = 1
        for fn in f:
            v *= fn**F.count(fn)
        w = sum(divisors(v)) + 1
        if a > w:
            print a, w, n, S
            return False

    print "Yes", n
    return True
def IsPractical(n):

  if n%2 != 0: return False
  
  #D = divisors(n)
  #if sum(D) == n*2: return True
  
  lg = log(n)/log(2)
  if lg == int(lg): return True
  
  F = RetFact(n)
  S = sorted(list(set(F)))
  
  for k in range(1,len(S)):
    a = S[k]
    f = S[:k]
    v = 1
    for fn in f:
      v*= fn**F.count(fn)
    w = sum(divisors(v)) + 1
    if a > w:
      print a, w,n,S  
      return False
  
  print "Yes",n
  return True
Example #3
0
def cntpowers(x):

    z = RetFact(x)
    S = set(z)
    summ = 0
    for SS in S:
        summ += z.count(SS)
    return summ
def cntpowers(x):

  z = RetFact(x)
  S = set(z)
  summ = 0
  for SS in S:
    summ += z.count(SS)
  return summ
def p141():
    L = []
    s, limita, limitm = 0, 10**4, 10**12
    for a in xrange(2, limita):
        for b in xrange(1, a):
            if (a**3) * b + (b**2) >= limitm:
                break
            if gcd(a, b) > 1:
                continue
            c = 1
            while True:
                m2 = (c**2) * (a**3) * b + c * (b**2)
                if m2 > limitm:
                    break
                else:
                    if m2**.5 == int(m2**.5):
                        s += m2
                        L.append(m2)

                c += 1
    print 'Solution: {}'.format(s)

    L = sorted(L)
    for m2 in L:
        print m2, RetFact(m2)
Example #6
0
def phi(n):
    v = n
    f = list(set(RetFact(n)))
    for z in f:
        v *= (z - 1)
        v /= z
    return v
def phi(n):

    f = list(set(RetFact(n)))
    num, div = 1, 1
    for x in f:
        num *= x - 1
        div *= x
    return n * num / div
Example #8
0
def IsPhiStrong4(sa, phi):

    flist = RetFact(phi)

    for f1 in flist:
        if phi % (f1 * f1) > 0: return False

    #print "flist",sa,phi,flist
    return True
Example #9
0
def IsSAStrong(sa, factlist):
    powlist = list(set(RetFact(sa)))
    #print "!!",sa,powlist
    for pp in powlist:
        psquare = pp**2
        if not (sa % psquare == 0):
            #print "False!"
            return False
    return True
def divisors(n):
    factors = RetFact(n)
    factors.append(1)
    fact = []
    l = len(factors)
    for z in xrange(2, l + 1):
        y = combinations(factors, z)

        for yy in y:
            yyy = list(yy)
            #print yyy
            s1 = 1
            for yyyy in yyy:
                s1 *= yyyy
            fact.append(s1)
    fact.append(1)
    fact = sorted(list(set(fact)))
    #print fact
    return fact
Example #11
0
def A0(n):

    for k in xrange(2, 17):

        x = repunit(k)
        if miller_rabin(x): continue
        f = RetFact(x)
        if n in f:
            return k
    return 0
def divisors(n):
    factors = RetFact(n)
    factors.append(1)
    fact=[]
    l = len(factors)
    for z in xrange(2,l+1):
      y = combinations(factors,z)
      
      for yy in y:
         yyy = list(yy)
         #print yyy
         s1 = 1
         for yyyy in yyy:
           s1*=yyyy
         fact.append(s1)
    fact.append(1)
    fact = sorted(list(set(fact)))
    #print fact
    return fact
def Phi(n):

    F = set(RetFact(n))
    phi = n
    num = 1
    den = 1
    for f in F:
        num *= (f - 1)
        den *= f

    return n * num / den
def lcm(lst):
  s = set()
  for l in lst:
    F = RetFact(l)
    for f in F:
      s.add(f)
  lcm=1
  print s
  for i in s:
    lcm *=i
  return lcm 
Example #15
0
def Phi(Achnum, factlist):

    # powlist = "["+factlist.rstrip(",")+"]"
    # powlist = list(set(map(int,powlist.strip('[]').split(','))))

    Phi = Achnum
    powlist = list(set(RetFact(Phi)))
    for i in powlist:
        Phi = Phi * (int(i) - 1)
        Phi = Phi / int(i)

    return Phi
Example #16
0
def tf(f):
    t = list(f)

    for f1 in f:
        zz = RetFact(f1 - 1)
        #print "zz",zz
        for zzz in zz:
            #print "zzz",zzz
            if zzz not in f:
                t.append(zzz)
    t = sorted(list(set(t)))
    return t
Example #17
0
def PhiPrimes(n):

    totfacts = []

    n1 = n - 1

    totfacts = RetFact(n1)
    allfactsfound = False

    while allfactsfound == False:
        newfactfound = False
        for m in totfacts:
            o = RetFact(m - 1)
            for p in o:

                if p < 2: continue
                if not p in totfacts:
                    totfacts.append(p)
                    newfactfound = True
        if newfactfound == False: allfactsfound = True

    return list(set(totfacts))
Example #18
0
def PhiPrimes(n):

  totfacts=[]

  n1 = n-1

  totfacts = RetFact(n1)
  allfactsfound = False

  while allfactsfound == False:
    newfactfound = False
    for m in totfacts:
       o = RetFact(m-1)
       for p in o:
       
         if p<2:continue
         if not p in totfacts:
           totfacts.append(p)
           newfactfound = True
    if newfactfound == False: allfactsfound = True

  return list(set(totfacts))  
Example #19
0
def divisorGen(n):
    factors = list(RetFact(n))
    nfactors = len(factors)
    f = [0] * nfactors
    while True:
        yield reduce(lambda x, y: x*y, [factors[x]**f[x] for x in range(nfactors)], 1)
        i = 0
        while True:
            f[i] += 1
            if f[i] <= factors[i]:
                break
            f[i] = 0
            i += 1
            if i >= nfactors:
                return
Example #20
0
def Phi(Achnum, factlist):

    #powlist = "["+factlist.rstrip(",")+"]"
    #powlist = list(set(map(int,powlist.strip('[]').split(','))))
    powlist = sorted(list(set(RetFact(Achnum))))
    #if Achnum ==108:print "%%%%",powlist
    Phi = Achnum
    x1 = 1
    x2 = 1

    for i in powlist:
        x1 = x1 * (int(i) - 1)
        x2 = x2 * int(i)
    Phi = (Phi * x1) / x2

    return Phi
Example #21
0
def IsPhiStrong3(sa, phi, factlist):

    #powlist = "["+factlist.rstrip(",")+"]"
    #powlist = list(set(map(int,powlist.strip('[]').split(','))))

    #powlist = sorted(list(set(factlist)))

    powlist = list(set(RetFact(phi)))

    phi2 = phi
    #print "sa,phi:",sa,phi2,powlist

    for i in powlist:

        #if phi==200:print "#",phi,i,phi2 % (i * i)
        if not (phi2 % (i * i) == 0): return False
        #if phi==200:print phi2%(i-1),phi2 % ((i-1) * (i-1))
        if phi2 % (i - 1) == 0:
            z = i - 1
            if z % 4 == 0: z = z / 2
            if phi2 % (z * z) > 0: return False

    return True
  result = 1
  for fs in facts:
    result *= fs
  return result

#psnum = {}

d={}

stor=[]


for i in range(2,1001):
  if IsPrime(i):continue
  v = divisors(i)
  f0 = RetFact(i)
  f=f0
  v.pop(-1)
  v.pop(0)
  for x in v:
    if x not in f:
      f += [x]
  f = sorted(f)
  #print f
  

    
  k=0
  #print "*",len(f),f

  
Example #23
0
Sum = sum(x)
maxlist=[]

#print tmp
#tmp=list(x)
for l in product(*W):
  #l=sorted(l,reverse = False)
  tmp=list(x)
  #if GCD(l)>1:continue
  #s = sum(l)
  #print l, s
  tsum = 0
  for j in l:
    #print j
    if j in tmp:continue
    f = sorted(list(set(RetFact(j))))
    #print "F",f
    if len(f)==1:
      old = notcoprime(f[0],tmp)
      #print "Old",old
      if j > old:
        tsum+=j-old;tmp.append(j);tmp.remove(old)
      continue
      
    #print "j",j,f
    f1 = f[0];f2 = f[1]

    if j/f1!=f2:
      f1 = j/f2
    elif j/f2!=f1:
      f2 = j/f1
      if y/z==(1.0 * y)/z:
        return False
    else:
      if z/y==(1.0 * z)/y:
        return False
  return True

d = divisors(120120)

x=[]
s=set(x) 
s2=set(x)

for x in d:

  z = RetFact(x)
  S = set(z)
  summ = 0
  for SS in S:
    summ += z.count(SS)
  if summ == 4:
    print x


# for x in d:
  # z = RetFact(x)
  # two = z.count(2)
  # zz = x/(2**two)
  # zzz = 3**(two+1)*zz
  # if zzz>(120120/4):
    # print x,two, zz,z
#
#
#
#
#
from Functions import RetFact

d = 12000.

f = RetFact(d)

ctr = 0
for i in xrange(4001, 6000):
    #if  not (d%i == 0): ctr+=1
    a = RetFact(i)
    if bool(set(a) & set(f)) == False:
        print i, ":", a
        ctr += 1
print ctr

u = int(1999. / 3.)
v = int(1999. / 2.)
w = int(1999. / 5.)
x = int(1999. / 6.)
y = int(1999. / 10.)
z = int(1999. / 15.)
z1 = int(1999. / 30.)

tot = u + v + w - x - y - z  #-z1

print tot, 1999 - tot
Example #26
0
    summ = 0
    for SS in S:
        summ += z.count(SS)
    return summ


N = 4 * 10**5
p = primes(N)
X = 5
summm = 0
Z = 60
for i in xrange(Z, Z + 1):
    if i in p:
        summm += 1
        continue
    d = divisors(i)
    D = cntpowers(i)
    D2 = D / 2

    print i, d, D, D2, RetFact(i)
    for x in d:
        q = cntpowers(x)
        if q == D2:
            summm += 1
            print "*", x, RetFact(x)
        #print "*",x,RetFact(x)
#if D==X: print

print summm
print "time elapsed", time() - st
Example #27
0
            factlist = str(i[0]) + "," + str(j[0]) + "," + str(k[0])

            phi = Phi(sa, factlist)

            if perfect(phi): continue

            #if sa < lim and IsPhiStrong3(sa,phi,factlist):
            if sa < lim and IsPhiStrong4(sa, phi):

                #print i,j,k ,sa, factlist, phi

                cnt += 1
                SAset.append(sa)

x = list(set(SAset))

x.sort()

for b in x:
    u = RetFact(b)
    uu = pfset(u)
    print b, uu  #RetFact(b)

#print "Number of numbers is", x

print
print
#print type(x), len(x), type(x[1])
print "number of primes is", len(x)
Example #28
0
def phi(n):

  f = list(set(RetFact(n)))
  num,div = 1,1
  for x in f:
    num *= x-1
    div *= x
  return n*num/div


N=1000000000L
ctr=0

for i in xrange(N-1000000,N):

   sq = phi(i)*i
   cbr = int(round(sq**(1./3.),0))

   if  cbr**3 ==sq:
     print "Hit!",i,i*i,sq,cbr,RetFact(i)
     ctr+=i

print "total for ",N,"is",ctr






from Functions import gcd, RetFact, divisors
from math import factorial
from Functions import primes

# p = primes(10**8)

# print len(p)

# exit()

for i in xrange(7, 8):

    n = factorial(i)

    div = divisors(n)
    t = 0
    sqrtn = int(n**.5)
    print div
    for j in div:
        #if j> sqrtn:break
        if gcd(j, n / j) == 1:
            f = RetFact(j)
            #if set(f)==set([2,3,5,7]):
            print i, n, len(f), f, j
            t += j**2  # + (n/j)**2
#  print "tot:",i,n,RetFact(t), t%1000000009
#  print "tot:",i,RetFact(i),n, t, t%1000000009,RetFact(t%1000000009)
#  print "tot:",i,n,RetFact(n), t,RetFact(t), t%1000000009
    print
#
#  Euler 204
#

from Functions import RetFact, IsPrime, primes
print "counting primses..."
#pns = primes(10**9)

print "primes calculated..."

summ = 99
for i in xrange(100, 10**9 + 1):
    #if (i in pns):continue

    zz = RetFact(i)
    if max(zz) > 100: continue

    #print i,zz
    summ += 1

print
print "Hamming Number type 100 <10^9 is", summ + 1
#
#
#
#  x + y = xy/n
#
#  1/x = 1/n - 1/y
#  
#
#
from Functions import RetFact, IsPrime
n = 1260 #500000000
maxx = 0
for n in xrange(10**8, 10**9):  #15000):
  if IsPrime(n):continue
  x = RetFact(n**2)
  s = set(x)
  facts = []
  for f in s:
    facts.append(x.count(f)+1)
  m=1
  for f in facts:
    m*=f
  m+=1
  m/=2

  if m>maxx:
    maxx=m
    print "!!!!!",n,":",m
  else:
    print n,":",m
  if maxx>4000000:print "Over 4 Mil!",n,m
#
#  Euler 204
#

from Functions import RetFact, IsPrime, primes

pns = primes(10**9)

summ = 0
for i in xrange(2, 10**9):
    if i > 100 and not (i in pns): continue

    zz = sorted(RetFact(i), reverse=True)
    if zz[0] < 101:
        print i, zz[0]
        summ += 1

print
print "Hamming Number type 100 <10^9 is", summ + 1
sctr=0
for qq in xrange(1,len(qp)):
  q = qp[qq]
  ii,jj,vv=0,0,0
  imax,jmax=0,0
  ctr=0
  for i in xrange(2,56):   #57
    for j in xrange(3,36):
      if gcd(i,j)>1:continue
      n = [p]*i + [q]*j
      v = reduce(mul,n)
      if v> 10**18: break
      A= isAchilles(n)
      if not A:continue
      phiA = Phi(v,n)
      fphi=RetFact(phiA) #F[phiA]
      #fphi=F[phiA]
      B = isAchilles(fphi)
      if not B:continue
      ctr+=1
      if v>vv:ii,jj,vv=i,j,v
      if i>imax:imax=i
      if j>jmax:jmax=j
      print i,j,":" , v, fphi
  if ctr>0:
    print
    print p,q,ctr, ":", RetFact(q-1)
    print ii,jj,vv
    print imax,jmax
    print "###############"
    sctr+=ctr
Example #34
0
                Prev = Old1 + zz

                #print New>Prev
                if New > Prev:
                    makechange = True
                    if zprev + New > nprev + zz:
                        nprev = New
                        zprev = zz
                        Oprev = Old1
        print vsing, zz, nprev
        if vsing >= int(MX * .9):
            print "grrr"
            if vsing not in x: print "GRRRRRRR"
            makechange = False
        if makechange == True:
            print z, Oprev
            x.remove(Oprev)
            x.remove(zprev)
            x.append(nprev)

    #x.append(1)
    #print sorted(x)
    #print MX,":",sorted(x),":",Sum(x)
    print MX, ":", x, ":", Sum(x)

print "Process time is", time() - st

for i in x:
    if not IsPrime(i):  #print i  # i%2==0:
        print i, RetFact(i)
Example #35
0
#
#
#  Euler 124
#
# []
#

ls = []

from Functions import RetFact, RofPrimes

primes = RofPrimes(2, 100001)

for n in xrange(1, 100001):

    if n in primes:
        ls.append((n, n))
        continue

    x = list(set(RetFact(n)))
    tot = 1
    for y in x:
        tot *= y
    ls.append((tot, n))

ls = sorted(ls)

print ls[10000], ls[9999]

print "done"
lim = 100000  # 10000

# F = FactorSieve(100**3)

a, b, c = 0, 0, 0

cnt = 0

for a in xrange(2, lim, 3):

    x = 8 * a * a * a + 15 * a * a + 6 * a - 1
    if x % 27 == 0:

        x /= 27  #   x = b*b*c
        y = RetFact(x)
        y.append(1)
        y = set(y)
        # print "!",a,y
        for z in y:
            b = z
            c = int(x / (b ** 2))
            # print "!!",x,":",a,b,c
            if x == b * b * c:
                if a + b + c <= 1000:  # 110000000:
                    cnt += 1
                    # print a,b,c

print "Number of Cardano Triplets <=", lim, "is", cnt
print "Process time is", time() - st
Example #37
0
    for x in xrange(2, len(d)):
        f1 = combinations(d, x)
        for f in f1:

            prod = Prod(f)
            if prod == i:
                summ = sum(f)
                k = len(f) + prod - summ
                #print i,prod, summ, len(f), k
                if k not in psnum:
                    psnum[k] = i
                elif i < psnum[k]:
                    print "%"
                    psnum[k] = i

    d = RetFact(i)
    if len(set(d)) == 1:
        prod = Prod(d)
        summ = sum(d)
        k = len(d) + prod - summ
        #print i,prod, summ, len(d), k
        if k not in psnum:
            psnum[k] = i
        elif i < psnum[k]:
            #print "&"
            psnum[k] = i

print
#print psnum
dupes = []
tot = 0