介是相亲数的介绍
http://zh.wikipedia.org/wiki/%E7%9B%B8%E4%BA%B2%E6%95%B0
介是代码
1#include <pthread.h> 2#include <stdio.h> 3#include <stdlib.h> 4#include <sys/time.h> 5#include <unistd.h> 6 7#define MAX_THREAD 128 8 9static size_t thread_num = 1; 10static size_t start = 2; 11static size_t end = -1; 12static size_t found[MAX_THREAD] = {0}; 13 14static inline size_t sum_of_proper_divisors(size_t n) 15{ 16 size_t sum, i, ixi; 17 for(i = 2, sum = 1; (ixi = i*i) <= n; i++) { 18 if(ixi != n) { 19 if(n % i == 0) { 20 sum += i + n/i; 21 } 22 } else { 23 sum += i; 24 } 25 } 26 return sum; 27} 28 29static void * thread_main(void * arg) 30{ 31 size_t id = (size_t)arg; 32 size_t i = start + id; 33 struct timeval tv; 34 gettimeofday(&tv, NULL); 35 double t1 = tv.tv_sec + tv.tv_usec / 1e6; 36 37 for(;;) { 38 size_t s = sum_of_proper_divisors(i); 39 if(s > i) { 40 if(sum_of_proper_divisors(s) == i) { 41 printf("%zu %zu\n", i, s); 42 found[id]++; 43 } 44 } 45 46 size_t next = i + thread_num; 47 if(next <= end && next > i) { 48 i = next; 49 } else { 50 break; 51 } 52 } 53 54 gettimeofday(&tv, NULL); 55 double t2 = tv.tv_sec + tv.tv_usec / 1e6; 56 57 fprintf(stderr, "=== thread:%zd found:%zd time:%f (sec) ===\n", id, found[id], t2-t1); 58 return NULL; 59} 60 61static void usage(const char * appname) 62{ 63 fprintf(stderr, 64 "Usage: %s [options]\n" 65 "Options:\n" 66 " -t Threads number ( 1 <= t <= %u )\n" 67 " -s Start number ( s >= 2 )\n" 68 " -e End number ( e > s )\n" 69 " -h Help\n", 70 appname, MAX_THREAD); 71} 72 73int main(int argc, char * argv[]) 74{ 75 int ch; 76 opterr = 0; 77 while((ch = getopt(argc, argv, "t:s:e:h")) != -1) { 78 switch(ch) { 79 case 't': 80 thread_num = atoll(optarg); 81 break; 82 case 's': 83 start = atoll(optarg); 84 break; 85 case 'e': 86 end = atoll(optarg); 87 break; 88 case 'h': 89 usage(argv[0]); 90 return 0; 91 default: 92 usage(argv[0]); 93 return 1; 94 } 95 } 96 97 if(!(1 <= thread_num && thread_num <= MAX_THREAD && start >= 2 && end > start)) { 98 usage(argv[0]); 99 return 2; 100 } 101 102 fprintf(stderr, "=== thread_num:%zu start:%zu end:%zu ===\n", thread_num, start, end); 103 104 pthread_t pid[MAX_THREAD]; 105 size_t i; 106 107 for(i = 1; i < thread_num; i++) { 108 pthread_create(pid+i, NULL, thread_main, (void*)i); 109 } 110 thread_main(0); 111 112 for(i = 1; i < thread_num; i++) { 113 pthread_join(pid[i], NULL); 114 } 115 return 0; 116}
介是编译方法(是的,是-pthread 不是 -lpthread,两者有区别)
gcc -Wall -pthread xqs.c -o xqs
介是使用方法举例
1./xqs -t 2 -e 800000 | sort -n 2=== thread_num:2 start:2 end:800000 === 3=== thread:1 found:6 time:1.287065 (sec) === 4=== thread:0 found:31 time:2.117775 (sec) === 5220 284 61184 1210 72620 2924 85020 5564 96232 6368 1010744 10856 1112285 14595 1217296 18416 1363020 76084 1466928 66992 1567095 71145 1669615 87633 1779750 88730 18100485 124155 19122265 139815 20122368 123152 21141664 153176 22142310 168730 23171856 176336 24176272 180848 25185368 203432 26196724 202444 27280540 365084 28308620 389924 29319550 430402 30356408 399592 31437456 455344 32469028 486178 33503056 514736 34522405 525915 35600392 669688 36609928 686072 37624184 691256 38635624 712216 39643336 652664 40667964 783556 41726104 796696