位数组实现用筛法(Sieve of Eratosthnes)计算素数
2011-04-16 01:57
330 查看
// // main.c // bitarray // // Created by Cheney Shen on 11-4-16. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #include <stdio.h> #include <limits.h> #include <string.h> #include <time.h> #define BITMASK(b) (1 << ((b) % CHAR_BIT)) #define BITSLOT(b) ((b) / CHAR_BIT) #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b)) #define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b)) #define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT) #define MAX 100000 int main (int argc, const char * argv[]) { char bitarray[MAX]; int i, j; time_t start, end; memset(bitarray, 0, BITNSLOTS(MAX)); time(&start); printf("It's %s\n", ctime(&start)); for (i = 2; i < MAX; i++) { if(!BITTEST(bitarray, i)) { printf("%d\t", i); for (j = i<<1; j < MAX; j += i) BITSET(bitarray, j); } } time(&end); printf("\n"); printf("Now it's is %s", ctime(&end)); return 0; }
相关文章推荐
- Sieve Of Eratosthenes求素数(埃拉托斯特尼筛法)
- <ZZ>The Sieve of Eratosthees ( 爱拉托逊斯筛选法 数论 筛法 )
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
- The Sieve of Eratosthenes (素数筛选法)
- 将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)
- 并行计算----5种方式实现判断是不是素数.Net,Win32,JavaRunnable,OpenMp,JavaThread
- 09 Sieve of Eratosthenes
- 计算素数(质数)java算法(应该是筛法)
- 用数组实现栈 Array implementation of stacks
- 数组例子2:二维数组中的行列互换和按钮控件数组实现计算器界面(暂不支持计算功能,仅界面及简单输入)
- 素数的计算-埃氏筛法(区间素数利器)
- The Sieve of Eratosthees(爱拉托逊斯筛选法)
- java与接口实现素数个数计算
- 用 Eratosthenes筛法构造1-n的素数表.
- 递归实现辗转相除以及筛法查找第n个素数
- Eratosthenes筛法--Haskell实现
- 计算数组中最大值、最小值和平均值的方法(The methods of calculating the maximum, minimum and average of a array )
- Algorithm: Sieve of Eratosthenes
- 埃式筛法计算素数Python3(学了一阵儿了,第一次写.)
- The Sieve of Eratosthens(爱拉托逊斯筛选法)