Changeset 88 for trunk/src/helpers/math.c
- Timestamp:
- Jul 23, 2001, 11:46:18 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/math.c
r87 r88 41 41 */ 42 42 43 43 44 /* 44 45 *@@ mathGCD: … … 47 48 * 48 49 * For example, mathGCD(10, 12) would return 2. 49 */ 50 51 int mathGCD(int m, int n) 52 { 53 int d = m; 54 int r = n; 55 56 while (r != 0) 57 { 58 d = r; 59 r = m % r; 60 } 61 62 return d; 63 } 50 * 51 * Modern Euclidian algorithm (The Art of Computer 52 * Programming, 4.5.2). 53 */ 54 55 int mathGCD(int a, int b) 56 { 57 int r; 58 59 while (b) 60 { 61 r = a % b; 62 a = b; 63 b = r; 64 } 65 66 return (a); 67 68 } 69 70 /* binary GCD alg. Based on following facts. 71 If N and M are even, then gcd(N,M) = 2 * gcd (N/2, M/2) 72 If N even and M odd, then gcd(N,M) = gcd(N/2, M) 73 If N,M odd, then |N-M|<max(N,M) thus guaranteeing that 74 we can use gcd(N,M) = gcd(N-M,M) to reduce. 75 fact: a&1 is 1 iff a is odd. Mult. and Div. by 2 more efficiently 76 implemented as shift-left 1 and shift-right 1 resp. 77 78 Found at http://remus.rutgers.edu/~rhoads/Code/int_gcd.c 79 */ 80 81 /* long gcd( long a, long b) 82 { 83 int t; 84 85 if (a < 0) a = -a; 86 if (!b) return a; 87 if (b < 0) b = -b; 88 if (!a) return b; 89 t = 0; 90 while (! ((a|b) & 1)) {a >>= 1; b >>= 1; ++t;} 91 while (! (a&1)) a >>= 1; 92 while (! (b&1)) b >>= 1; 93 while (a != b) 94 { 95 if (a > b) 96 { 97 a -= b; 98 do {a >>= 1;} while (! (a&1)); 99 } 100 else { 101 b -= a; 102 do {b >>= 1;} while (! (b&1)); 103 } 104 } 105 return(a<<t); */ 64 106 65 107 /* … … 316 358 } 317 359 318 typedef struct _PRIMEDATA 360 /* 361 *@@ mathGCDMulti: 362 * finds the greatest common divisor for a 363 * whole array of integers. 364 * 365 * For example, if you pass in three integers 366 * 1000, 1500, and 1800, this would return 100. 367 * If you only pass in 1000 and 1500, you'd 368 * get 500. 369 * 370 * Use the fact that: 371 * 372 * gcd(u1, u2, ..., un) = gcd(u1, gcd(u2, ..., un)) 373 * 374 * Greatest common divisor of n integers (The 375 * Art of Computer Programming, 4.5.2). 376 */ 377 378 int mathGCDMulti(int *paNs, // in: array of integers 379 int cNs) // in: array item count (NOT array size) 380 { 381 int d = paNs[cNs-1]; 382 int k = cNs-1; 383 384 while ( (d != 1) 385 && (k > 0) 386 ) 387 { 388 d = mathGCD(paNs[k-1], d); 389 k--; 390 } 391 392 return (d); 393 } 394 395 /* typedef struct _PRIMEDATA 319 396 { 320 397 LINKLIST llPrimes; … … 331 408 // for a later prime 332 409 int iLastInt; 333 } PRIMEENTRY, *PPRIMEENTRY; 410 } PRIMEENTRY, *PPRIMEENTRY; */ 334 411 335 412 /* … … 338 415 */ 339 416 340 int XWPENTRY GCDMultiCallbackCreate(int n, // in: prime417 /* int XWPENTRY GCDMultiCallbackCreate(int n, // in: prime 341 418 int p, // in: power 342 419 void *pUser) // in: user param, here root of tree … … 374 451 375 452 return (0); 376 } 453 } */ 377 454 378 455 /* … … 381 458 */ 382 459 383 int XWPENTRY GCDMultiCallbackLowest(int n, // in: prime460 /* int XWPENTRY GCDMultiCallbackLowest(int n, // in: prime 384 461 int p, // in: power 385 462 void *pUser) // in: user param, here root of tree … … 411 488 412 489 return (0); 413 } 414 415 /* 416 *@@ mathGCDMulti: 417 * finds the greatest common divisor for a 418 * whole array of integers. 419 * 420 * For example, if you pass in three integers 421 * 1000, 1500, and 1800, this would return 100. 422 * If you only pass in 1000 and 1500, you'd 423 * get 500. 424 * 425 * Potentially expensive since this calls 426 * mathFactorPrime for each integer in the 427 * array first. 428 */ 429 430 int mathGCDMulti(int *paNs, // in: array of integers 490 } */ 491 492 /* int mathGCDMulti(int *paNs, // in: array of integers 431 493 int cNs) // in: array item count (NOT array size) 432 494 { … … 549 611 550 612 return dGCD; 551 } 613 } */ 552 614 553 615 // testcase
Note:
See TracChangeset
for help on using the changeset viewer.