介是相亲数的介绍
http://zh.wikipedia.org/wiki/%E7%9B%B8%E4%BA%B2%E6%95%B0
介是代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/sysinfo.h>
static inline size_t sum_of_proper_divisors(size_t n)
{
size_t sum, i, ixi;
for(i = 2, sum = 1; (ixi = i*i) <= n; i++) {
if(ixi != n) {
if(n % i == 0) {
sum += i + n/i;
}
} else {
sum += i;
}
}
return sum;
}
static size_t thread_num = 1;
static size_t start = 2;
static size_t end = -1;
static void * thread_main(void * id)
{
size_t i = start + (size_t)id;
for(;;) {
size_t s = sum_of_proper_divisors(i);
if(s > i) {
if(sum_of_proper_divisors(s) == i) {
printf("%zu %zu\n", i, s);
}
}
size_t next = i + thread_num;
if(next <= end && next > i) {
i = next;
} else {
break;
}
}
return NULL;
}
static void usage(const char * appname)
{
fprintf(stderr,
"Usage: %s [options]\n"
"Options:\n"
" -t Threads number ( 1 <= t <= 128) \n"
" -s Start number ( s >= 2 )\n"
" -e End number ( e > s )\n"
" -h Help\n",
appname);
}
int main(int argc, char * argv[])
{
int ch;
opterr = 0;
while((ch = getopt(argc, argv, "t:s:e:h")) != -1) {
switch(ch) {
case ‘t‘:
thread_num = atoi(optarg);
break;
case ‘s‘:
start = atoll(optarg);
break;
case ‘e‘:
end = atoll(optarg);
break;
case ‘h‘:
usage(argv[0]);
return 0;
default:
usage(argv[0]);
return 1;
}
}
if(!(1 <= thread_num && thread_num <= 128 && start >= 2 && end > start)) {
usage(argv[0]);
return 2;
}
fprintf(stderr, "=== thread_num:%zu start:%zu end:%zu ===\n", thread_num, start, end);
struct sysinfo info1, info2;
sysinfo(&info1);
pthread_t pid[128];
size_t i;
for(i = 1; i < thread_num; i++) {
pthread_create(pid+i, NULL, thread_main, (void*)i);
}
thread_main(0);
for(i = 1; i < thread_num; i++) {
pthread_join(pid[i], NULL);
}
sysinfo(&info2);
fprintf(stderr, "=== %lu seconds ===\n", info2.uptime - info1.uptime);
return 0;
}
编译方法(是的,是-pthread 不是 -lpthread,两者有区别)
gcc -Wall -pthread q.c
运行
./a.out -t8 -e5000000 === thread_num:8 start:2 end:5000000 === 1184 1210 6232 6368 10744 10856 17296 18416 66928 66992 67095 71145 69615 87633 122265 139815 220 284 2620 2924 5020 5564 12285 14595 79750 88730 142310 168730 122368 123152 141664 153176 171856 176336 63020 76084 176272 180848 185368 203432 196724 202444 100485 124155 319550 430402 356408 399592 1077890 1099390 1154450 1189150 437456 455344 503056 514736 280540 365084 308620 389924 522405 525915 600392 669688 609928 686072 469028 486178 624184 691256 635624 712216 643336 652664 802725 863835 1175265 1438983 726104 796696 947835 1125765 667964 783556 1511930 1598470 1156870 1292570 879712 901424 898216 980984 1280565 1340235 1358595 1486845 998104 1043096 1328470 1483850 1466150 1747930 1185376 1286744 1798875 1870245 1669910 2062570 1392368 1464592 1468324 1749212 2236570 2429030 2082464 2090656 2728726 3077354 2652728 2941672 2803580 3716164 3606850 3892670 2723792 2874064 2739704 2928136 2802416 2947216 4482765 5120595 3276856 3721544 4246130 4488910 4259750 4445050 4532710 6135962 3786904 4300136 3805264 4006736 4238984 4314616 4604776 5162744 === 27 seconds ===
原文:http://my.oschina.net/2bit/blog/391405