Welcome, guest | Sign In | My Account | Store | Cart

Various functions related to Carmichael numbers (some in progress)

Python, 291 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291``` ```from __future__ import division from time import clock from gmpy import * from pysymbolicext import * import psyco psyco.full() __all__=["carmichael","carmichael1","carmichael2","carmichael3","iscarmichael","clambda"] #Carmichael numbers are odd composites C with at least 3 prime #factors p(n) such that for each p(n) of C, p(n)-1 divides C-1 def carmichael1(a,b,limit=1000): """ carmichael(a,b) For a,b (odd primes) returns array of c such that a*b*c is a Carmichael number """ if a==b or a==2 or b==2: return [] if not is_prime(a) or not is_prime(b): return [] ab=a*b l=lcm(a-1,b-1) m1=ab%l m2=invert(m1,l) abl=ab*l c=m2 while c<=max(a,b): c+=l xm1=ab*c-1 solutions=[] while c<=limit: if (xm1)%(c-1)==0 and is_prime(c): solutions.append(c) c+=l xm1+=abl return solutions def carmichael2(a,b,limit=1000): """ carmichael(a,b) For a,b (odd primes) returns array of c such that a*b*c is a Carmichael number """ if a==b or a==2 or b==2: return [] if not is_prime(a) or not is_prime(b): return [] am1=a-1 bm1=b-1 am1bm1=am1*bm1 g,s,t=gcdext(am1bm1,-(am1+b)) c=((t*(am1+bm1)//g))%bm1+1 while c<=max(a,b): c+=bm1 xm1=(a)*(b)*(c)-1 inc=a*b*bm1 solutions=[] while c<=limit: if (xm1)%(c-1)==0 and is_prime(c): solutions.append(c) c+=bm1 xm1+=inc return solutions #A different approach,but seems slower on many inputs def carmichael3(*args): """ carmichael(*args) (last arg is search limit) For p1...pn (odd distinct primes) returns array of c such that p1*p2...pn*c is a Carmichael number. """ limit=args[-1] args=args[:-1] if 2 in args: return [] for arg in args: if not is_prime(arg): return [] product=reduce(lambda a,b : a*b, args) l=reduce(lambda a,b : lcm(a,b), (x-1 for x in args)) m1=product%l m2=invert(m1,l) productl=product*l c=m2 while c<=max(args): c+=l xm1=product*c-1 solutions=[] while c<=limit: if (xm1)%(c-1)==0 and is_prime(c): solutions.append(c) c+=l xm1+=productl return solutions def carmichael(*args): """ carmichael(*args) (last arg is search limit) For p1...pn (odd distinct primes) returns array of c such that p1*p2...pn*c is a Carmichael number. Redirection function - calls carmichael 1 or 2 depending on number of arguments. """ if len(args)==3: return carmichael1(*args) else: return carmichael3(*args) def iscarmichael(*args): """ iscarmichael(*args). args are prime factors. Naively tests if the product of the given factors is a Carmichael number """ for arg in args: if not is_prime(arg): return False xm1=reduce(lambda a,b : a*b, args)-1 for arg in args: if xm1%(arg-1)!=0: return False return True def cgenerate(limit=1000000,a=6,b=12,c=18): """ cgenerate(a,b,c,limit) Return a list of all Carmichael numbers of the form (ak+1)*(bk+1)*(ck+1) up to limit """ print "Using search limit",limit result=[] k=1 while True: a1=a*k+1 b1=b*k+1 c1=c*k+1 n=a1*b1*c1 if n>limit: break if is_prime(a1) and is_prime(b1) and is_prime(c1): result.append((a1,b1,c1,n)) #result.append(n) k+=1 return result def clambda(n): """ clambda(n) Returns Carmichael's lambda function for positive integer n. Relies on factoring n """ smallvalues=[1,1,2,2,4,2,6,2,6,4,10,2,12,6,4,4,16,6,18,4,6,10,22,2,20,12,18,\ 6,28,4,30,8,10,16,12,6,36,18,12,4,40,6,42,10,12,22,46,4,42,20,16,12,52,18,\ 20,6,18,28,58,4,60,30,6,16,12,10,66,16,22,12,70,6,72,36,20,18,30,12,78,4,54,\ 40,82,6,16,42,28,10,88,12,12,22,30,46,36,8,96,42,30,20] if n<=100: return smallvalues[n-1] factors=factor(n) l1=[] for p,e in factors: if p==2 and e>2: l1.append(2**(e-2)) else: l1.append((p-1)*p**(e-1)) return reduce(lambda a,b : lcm(a,b), l1) numbers=cgenerate(100000000000000000000,2,4,14) for x in numbers: print x[3], if iscarmichael(x[0],x[1],x[2]): print "is a Carmichael number" else: print #----------------------------------TEST CODE----------------------------------# if __name__=="__main__": def naive(a,b,limit): """ naive(a,b,limit). Naive implementation for benchmarking comparison. """ if a==b or a==2 or b==2: return [] if not is_prime(a) or not is_prime(b): return [] c=b+2 xm1=a*b*c-1 am1=a-1 bm1=b-1 inc=2*a*b solutions=[] while c

NOTE - cdic not included here

I am interested in any feedback re: style, efficiency and correctness...

 Created by Philip Smith on Sat, 12 Sep 2009 (MIT)