C小程序多线程算相亲数

Wesley13
• 阅读 462

介是相亲数的介绍

http://zh.wikipedia.org/wiki/%E7%9B%B8%E4%BA%B2%E6%95%B0

介是代码

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#define MAX_THREAD 128

static size_t thread_num = 1;
static size_t start = 2;
static size_t end = -1;
static size_t found[MAX_THREAD] = {0};

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 void * thread_main(void * arg)
{
    size_t id = (size_t)arg;
    size_t i = start + id;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    double t1 = tv.tv_sec + tv.tv_usec / 1e6;

    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);
                found[id]++;
            }
        }

        size_t next = i + thread_num;
        if(next <= end && next > i) {
            i = next;
        } else {
            break;
        }
    }

    gettimeofday(&tv, NULL);
    double t2 = tv.tv_sec + tv.tv_usec / 1e6;

    fprintf(stderr, "=== thread:%zd found:%zd time:%f (sec) ===\n", id, found[id], t2-t1);
    return NULL;
}

static void usage(const char * appname)
{
    fprintf(stderr,
            "Usage: %s [options]\n"
            "Options:\n"
            "  -t        Threads number ( 1 <= t <= %u )\n"
            "  -s        Start number ( s >= 2 )\n"
            "  -e        End number ( e > s )\n"
            "  -h        Help\n",
            appname, MAX_THREAD);
}

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 = atoll(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 <= MAX_THREAD && start >= 2 && end > start)) {
        usage(argv[0]);
        return 2;
    }

    fprintf(stderr, "=== thread_num:%zu start:%zu end:%zu ===\n", thread_num, start, end);

    pthread_t pid[MAX_THREAD];
    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);
    }
    return 0;
}

介是编译方法(是的,是-pthread 不是 -lpthread,两者有区别)

gcc -Wall -pthread xqs.c -o xqs

介是使用方法举例

./xqs -t 2 -e 800000 | sort -n
=== thread_num:2 start:2 end:800000 ===
=== thread:1 found:6 time:1.287065 (sec) ===
=== thread:0 found:31 time:2.117775 (sec) ===
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
63020 76084
66928 66992
67095 71145
69615 87633
79750 88730
100485 124155
122265 139815
122368 123152
141664 153176
142310 168730
171856 176336
176272 180848
185368 203432
196724 202444
280540 365084
308620 389924
319550 430402
356408 399592
437456 455344
469028 486178
503056 514736
522405 525915
600392 669688
609928 686072
624184 691256
635624 712216
643336 652664
667964 783556
726104 796696
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Stella981 Stella981
2年前
C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
报错信息:最近打开下载的Excel,会报如下错误。(xls格式不受影响)!(https://oscimg.oschina.net/oscnet/2b6f0c8d7f97368d095d9f0c96bcb36d410.png)!(https://oscimg.oschina.net/oscnet/fe1a8000d00cec3c
Stella981 Stella981
2年前
Linux查看GPU信息和使用情况
1、Linux查看显卡信息:lspci|grepivga2、使用nvidiaGPU可以:lspci|grepinvidia!(https://oscimg.oschina.net/oscnet/36e7c7382fa9fe49068e7e5f8825bc67a17.png)前边的序号"00:0f.0"是显卡的代
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
MySQL 的慢 SQL 怎么优化?
!(https://oscimg.oschina.net/oscnet/7b00ec583b5e42cc80e8c56c6556c082.jpg)Java技术栈www.javastack.cn关注阅读更多优质文章(https://www.oschina.net/action/GoToLink?urlhttp
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Wesley13 Wesley13
2年前
C 猜猜猜😀文字小游戏
前言随机性  随机数生成(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E9%259A%258F%25E6%259C%25BA%25E6%2595%25B0%25E7%2594%259F%25E6%2
Stella981 Stella981
2年前
Flink SQL Window源码全解析
!(https://oscimg.oschina.net/oscnet/72793fbade36fc18d649681ebaeee4cdf00.jpg)(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmp.weixin.qq.com%2Fs%3F__biz%3DMzU3MzgwNT