]> git.leopard-lacewing.eu Git - zahlenTA.git/commitdiff
[py] jacobi jacobi_tex
authorPeter Schaefer <schaeferpm@gmail.com>
Wed, 6 Jun 2012 05:58:36 +0000 (07:58 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Wed, 6 Jun 2012 05:58:36 +0000 (07:58 +0200)
UE/lib.py
UE/python/test.pdf
UE/python/test.tex

index 64a8c67d96e379df7f05270382b1af59473f6c5b..546b06dc436c4a66a4b6b81de5f775b50d878600 100644 (file)
--- a/UE/lib.py
+++ b/UE/lib.py
 import math
 
 # Berechnet die Summe aller vielfachen von base bis max
-def summandial(m,base=1):
-    m = int(m/base)*base
-    return m*(m+base)/(2*base)
+def summandial(m, base=1):
+    m = int(m / base) * base
+    return m * (m + base) / (2 * base)
 
 # Gibt die naechst kleine Fib zu max zurueck
-def fib(m,a=1,b=2):
+def fib(m, a=1, b=2):
     while b < m:
-        a,b = b,a+b
+        a, b = b, a + b
     return a
 
 # Gibt die Liste aller Fib zur naechst kleineren Fib von max zurueck
-def fibV(m,a=[1,2]):
+def fibV(m, a=[1, 2]):
     i = 1;
     while a[i] < m:
-        a.append(a[i] + a[i-1])
-        i = i+1
+        a.append(a[i] + a[i - 1])
+        i = i + 1
     return a
 
 # Gibt die Summe aller d-fachen Fib zu max zurueck (d=2)
-def fibS(m,d=2):
-    a,b = 1,2
+def fibS(m, d=2):
+    a, b = 1, 2
     s = 0
     while b < m:
-        a,b = b,a+b
-        if a%d == 0:
-            s = s +a     
+        a, b = b, a + b
+        if a % d == 0:
+            s = s + a     
     return s
 
 # Gibt alle Primzahlen bis n zurueck
 def primes(n):
     if n < 2:
         return 0
-    p = range(1,n+1,2)
+    p = range(1, n + 1, 2)
     q = len(p);
     p[0] = 2;
-    for k in range(3,int(n**.5+1),2):
-        if p[(k-1)/2]:
-            for i in range(((k*k-1)/2),q,k):
+    for k in range(3, int(n ** .5 + 1), 2):
+        if p[(k - 1) / 2]:
+            for i in range(((k * k - 1) / 2), q, k):
                 p[i] = 0
-    return filter(lambda x:x>0,p)
+    return filter(lambda x:x > 0, p)
 
 # Gibt alle Primfaktoren von n zurueck
 def factor(n):
-    if n<4:
+    if n < 4:
         return [n]
-    p = primes(int(n**.5+1))
+    p = primes(int(n ** .5 + 1))
     f = []
-    while n>1:
-        p = filter(lambda x:n%x==0,p)
-        if(p==[]):
+    while n > 1:
+        p = filter(lambda x:n % x == 0, p)
+        if(p == []):
             f.append(n)
             break
         f[len(f):] = p
-        n = n/prod(p)
+        n = n / prod(p)
     f.sort()
     return f
 
 # Gibt alle Primfaktoren von n als vektor mit Vielfachheit zurueck
 def factorD(n):
-    if n<4:
+    if n < 4:
         return {n:1}
-    p = primes(int(n**.5+1))
+    p = primes(int(n ** .5 + 1))
     f = {}
-    while n>1:
-        p = filter(lambda x:n%x==0,p)
-        if(p==[]):
+    while n > 1:
+        p = filter(lambda x:n % x == 0, p)
+        if(p == []):
             f[n] = 1
             break
         for i in range(len(p)):
             if(p[i] in f):
-                f[p[i]]=f[p[i]]+1
+                f[p[i]] = f[p[i]] + 1
             else:
-                f[p[i]]=1
-        n = n/prod(p)
+                f[p[i]] = 1
+        n = n / prod(p)
 #    f.sort()
     return f 
     
 # Multipliziert alle Werte aus Liste seq auf
 def prod(seq):
-    return reduce(lambda x,y:x*y, seq, 1)
+    return reduce(lambda x, y:x * y, seq, 1)
 
 # generiert Pyth tripel
-def pythtrip(u,v):
-    return [u*u-v*v, 2*u*v, u*u+v*v]
+def pythtrip(u, v):
+    return [u * u - v * v, 2 * u * v, u * u + v * v]
 
 #Binomial Koeffizient
-def binomial(n,k):
-    return math.factorial(n)/(math.factorial(k)*math.factorial(n-k))
+def binomial(n, k):
+    return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
 
 def ebinomial(seq):
     vek = [1, 0]
     for i in seq:
-        for j in range(len(vek)-1,0,-1):
-            vek[j] = vek[j]+vek[j-1]*i
-        vek[len(vek):]=[0]
+        for j in range(len(vek) - 1, 0, -1):
+            vek[j] = vek[j] + vek[j - 1] * i
+        vek[len(vek):] = [0]
         
-    return vek[:len(vek)-1]
+    return vek[:len(vek) - 1]
 
 #Anzahl der Teiler
 def divisors(num):
     test = factorD(num)
-    return sum(ebinomial(map(lambda x:test[x],test.keys()))[1:])+1
+    return sum(ebinomial(map(lambda x:test[x], test.keys()))[1:]) + 1
 
 
 #erweiterter Euklid
-def euklid(a,b):
+def euklid(a, b):
     if a < b:
         tmp = a
         a = b
@@ -114,15 +114,15 @@ def euklid(a,b):
     x = [1, 0]
     y = [0, 1]
     q = a / b
-    while a%b:
-        [a ,b] = [b,a-q*b]
-        x = [x[1],x[0]-q*x[1]]
-        y = [y[1],y[0]-q*y[1]]
-        q = a/b
-    return [b,x[1],y[1]]
+    while a % b:
+        [a , b] = [b, a - q * b]
+        x = [x[1], x[0] - q * x[1]]
+        y = [y[1], y[0] - q * y[1]]
+        q = a / b
+    return [b, x[1], y[1]]
 
 #erweiterter Euklid mit TeX Ausgabe
-def euklid_tex(a,b):
+def euklid_tex(a, b):
     if a < b:
         tmp = a
         a = b
@@ -132,120 +132,211 @@ def euklid_tex(a,b):
     q = int(a / b)
     print "\\begin{array}{ccccccc}"
     print "  r_{i-2} & r_{i-1} & q_i & x_{i-2} & x_{i-1} & y_{i-2} & y_{i-1}\\\\\\hline"
-    print " ", a, "&" , b ,"&" , q,"&" ,x[0],"&",x[1] ,"&",y[0] ,"&" ,y[1],"\\\\"
-    while a%b:
-        [a ,b] = [b,a-q*b]
-        x = [x[1],x[0]-q*x[1]]
-        y = [y[1],y[0]-q*y[1]]
-        q = int(a/b)
-        if a%b:
-            print " ", a, "&" , b ,"&" , q,"&" ,x[0],"&",x[1] ,"&",y[0] ,"&" ,y[1],"\\\\"
+    print " ", a, "&" , b , "&" , q, "&" , x[0], "&", x[1] , "&", y[0] , "&" , y[1], "\\\\"
+    while a % b:
+        [a , b] = [b, a - q * b]
+        x = [x[1], x[0] - q * x[1]]
+        y = [y[1], y[0] - q * y[1]]
+        q = int(a / b)
+        if a % b:
+            print " ", a, "&" , b , "&" , q, "&" , x[0], "&", x[1] , "&", y[0] , "&" , y[1], "\\\\"
         else:
             print " \\cline{2-2}\\cline{5-5}\\cline{7-7}"
             print " \multicolumn{1}{l|}{", a, "}& \multicolumn{1}{l|}{" , b ,
-            print "}&&\multicolumn{1}{l|}{" ,x[0],"}&\multicolumn{1}{l|}{",x[1] ,
-            print "}&\multicolumn{1}{l|}{",y[0] ,"}&\multicolumn{1}{l|}{" ,y[1], "}\\\\"
+            print "}&&\multicolumn{1}{l|}{" , x[0], "}&\multicolumn{1}{l|}{", x[1] ,
+            print "}&\multicolumn{1}{l|}{", y[0] , "}&\multicolumn{1}{l|}{" , y[1], "}\\\\"
             print " \\cline{2-2}\\cline{5-5}\\cline{7-7}"
     print "\\end{array}",
-    return [b,x[1],y[1]]
+    return [b, x[1], y[1]]
 
-# groesster gemeinsamer Teiler
-def gcd(a,b):
-    return euklid(a,b)[0]
+# groesster gemeinsamer Teiler 
+def gcd(a, b):
+    return euklid(a, b)[0]
 
 # kleinstes gemeinsames Vielfaches
-def lcm(a,b):
-    return abs(a*b) / gcd(a,b)
+def lcm(a, b):
+    return abs(a * b) / gcd(a, b)
 
 #eulersche phi-Funktion
 def phi(n):
-    return n*reduce(lambda x,p:x*(1-1./p), factorD(n).keys(), 1)
+    return n * reduce(lambda x, p:x * (1 - 1. / p), factorD(n).keys(), 1)
 
-def ordm(a,m):
+def ordm(a, m):
     e = 1
     ae = a
-    while ae%m!=1:
-        ae=ae*a
-        e = e+1
+    while ae % m != 1:
+        ae = ae * a
+        e = e + 1
     return e
 
 # Legendre Symbol
-def legendre(z,n):
-    stack = [[z,n]]
+def legendre(z, n):
+    stack = [[z, n]]
     d = +1
     while len(stack):
-        [z,n] = stack.pop(0)
-        print z,"/",n
-        if(z>n):
-            stack.append([z%n, n])
+        [z, n] = stack.pop(0)
+#        print z,"/",n
+        if(z > n):
+            stack.append([z % n, n])
+            continue
+        
+        if(z == n - 1):
+            if(n % 4 == 3):
+                d = d * (-1)
             continue
 
         fac = factorD(z).keys()
-        if(len(fac)>1):
-            stack[len(stack):] = map(lambda x:[x,n],fac)
+        if(len(fac) > 1):
+            stack[len(stack):] = map(lambda x:[x, n], fac)
             continue
         
-        if(z==2):
-            if(n%8==3 or n%8==5):
-                d = d*(-1)
+        if(z == 2):
+            if(n % 8 == 3 or n % 8 == 5):
+                d = d * (-1)
             continue
-        if(z==1):
+        if(z == 1):
             continue
         
-        stack.append([n,z])
-        if (n%4==3 and z%4==3):
-            d = d* (-1)
+        stack.append([n, z])
+        if (n % 4 == 3 and z % 4 == 3):
+            d = d * (-1)
         
     return d
 
 #Legendre Symbol mit TeX Ausgabe
-def legendre_tex(z,n):
-    stack = [[z,n],[0,0]]
-    l=-1
+def legendre_tex(z, n):
+    stack = [[z, n], [0, 0]]
+    l = -1
     d = +1
     print "\\begin{align}"
-    while len(stack)>1:
-        [z,n] = stack.pop(0)
+    while len(stack) > 1:
+        [z, n] = stack.pop(0)
 
-        if([z,n]==[0,0]):
-            stack.append([0,0])
-            l = l+1
-            if(l==0):
+        if([z, n] == [0, 0]):
+            stack.append([0, 0])
+            l = l + 1
+            if(l == 0):
                 print "\n&=",
-            elif(l>3):
+            elif(l > 3):
                 print "\\\\ \n&=",
                 l = 1
             else:
                 print "\n=",
-            if(d<0):
+            if(d < 0):
                 print "-",
             continue
-        print "\\legend[L]{",z,"}{",n,"}",
+        print "\\legend[L]{", z, "}{", n, "}",
         
-        if(z>n):
-            stack.append([z%n, n])
+        if(z > n):
+            stack.append([z % n, n])
+            continue
+        
+        if(z == n - 1):
+            if(n % 4 == 3):
+                d = d * (-1)
             continue
 
         fac = factorD(z).keys()
-        if(len(fac)>1):
-            stack[len(stack):] = map(lambda x:[x,n],fac)
+        if(len(fac) > 1):
+            stack[len(stack):] = map(lambda x:[x, n], fac)
             continue
         
-        if(z==2):
-            if(n%8==3 or n%8==5):
-                d = d*(-1)
+        if(z == 2):
+            if(n % 8 == 3 or n % 8 == 5):
+                d = d * (-1)
             continue
-        if(z==1):
+        if(z == 1):
             continue
         
-        stack.append([n,z])
-        if (n%4==3 and z%4==3):
-            d = d* (-1)
+        stack.append([n, z])
+        if (n % 4 == 3 and z % 4 == 3):
+            d = d * (-1)
+    
+    print
+    print "=", d        
+    print "\\end{align}"    
+    return d
+
+# Jacobi Symbol
+def jacobi(z, n):
+    d = +1
+    while 1:
+        print z, "/", n
+        if(z > n):
+            z = z % n
+            continue
+        
+        if(z == n - 1):
+            if(n % 4 == 3):
+                d = d * (-1)
+            break
+
+        if(z % 4 == 0):
+            z = z / 4
+            continue
+        
+        if(z == 2):
+            if(n % 8 == 3 or n % 8 == 5):
+                d = d * (-1)
+            break
+        if(z == 1):
+            break
+        
+        [z, n] = [n, z]
+        if (n % 4 == 3 and z % 4 == 3):
+            d = d * (-1)
+        
+    return d
+
+#Jacobi Symbol mit TeX Ausgabe
+def jacobi_tex(z, n):
+    l = -2
+    d = +1
+    print "\\begin{align}"
+    while 1:
+
+        l = l + 1
+        if(l == 0):
+            print "\n&=",
+        elif(l > 3):
+            print "\\\\ \n&=",
+            l = 1
+        elif(l>0):
+            print "\n=",
+        if(d < 0):
+            print "-",
+
+        print "\\legend[J]{", z, "}{", n, "}",
+        
+
+        if(z > n):
+            z = z % n
+            continue
+        
+        if(z == n - 1):
+            if(n % 4 == 3):
+                d = d * (-1)
+            break
+
+        if(z % 4 == 0):
+            z = z / 4
+            continue
+        
+        if(z == 2):
+            if(n % 8 == 3 or n % 8 == 5):
+                d = d * (-1)
+            break
+        if(z == 1):
+            break
+        
+        [z, n] = [n, z]
+        if (n % 4 == 3 and z % 4 == 3):
+            d = d * (-1)
     
     print
-    print "=",d        
+    print "=", d        
     print "\\end{align}"    
     return d
 
 
-legendre_tex(69,97)
\ No newline at end of file
+print jacobi_tex(69, 97)
index 06d4625a0507130df4b3299a66919b600f03db65..16e94d70846afb556956862766f89e54dfe98af4 100644 (file)
Binary files a/UE/python/test.pdf and b/UE/python/test.pdf differ
index eb09f71adbb2315214dacbb35ab5dfd6ff0534e1..7b8f67ace3c6a366a4bb07d230d747895b3e067c 100644 (file)
  
 
 \begin{align}
-\legend{ 69 }{ 97 } 
-&= \legend[L]{ 3 }{ 97 } \legend[L]{ 23 }{ 97 } 
-= \legend[L]{ 97 }{ 3 } \legend[L]{ 97 }{ 23 } 
-= \legend[L]{ 1 }{ 3 } \legend[L]{ 5 }{ 23 } 
-= \legend[L]{ 23 }{ 5 } \\ 
-&= \legend[L]{ 3 }{ 5 } 
-= \legend[L]{ 5 }{ 3 } 
-= \legend[L]{ 2 }{ 3 }
+\legend[J]{ 69 }{ 97 } 
+&= \legend[J]{ 97 }{ 69 } 
+= \legend[J]{ 28 }{ 69 } 
+= \legend[J]{ 7 }{ 69 } 
+= \legend[J]{ 69 }{ 7 } \\ 
+&= \legend[J]{ 6 }{ 7 }
 = -1
 \end{align}