blob: ad96011d93a6ef3ecffa460b1a80182b48f4ae6c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/*
* The Great Computer Language Shootout
* http://shootout.alioth.debian.org/
*
* Written by Dima Dorfman, 2004
* Compile: gcc -std=c99 -O2 -o nsieve_bits_gcc nsieve_bits.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int bits;
#define NBITS (8 * sizeof(bits))
static unsigned long
nsieve(unsigned long m)
{
unsigned long count, i, j;
bits * a;
a = malloc((m / NBITS) * sizeof(bits));
memset(a, (1 << 8) - 1, (m / NBITS) * sizeof(bits));
count = 0;
for (i = 2; i < m; ++i)
if (a[i / NBITS] & (1 << i % NBITS)) {
for (j = i + i; j < m; j += i)
a[j / NBITS] &= ~(1 << j % NBITS);
++count;
}
return (count);
}
static void
test(unsigned long n)
{
unsigned long count, m;
m = (1 << n) * 10000;
count = nsieve(m);
printf("Primes up to %8ju %8ju\n", m, count);
}
int
main(int ac, char **av)
{
unsigned long n;
char *cp;
n = ac < 2 ? 9 : strtoul(av[1], &cp, 10);
test(n);
if (n >= 1)
test(n - 1);
if (n >= 2)
test(n - 2);
exit(0);
}
/****
build & benchmark results
BUILD COMMANDS FOR: nsievebits.gcc
Fri Sep 15 06:30:15 PDT 2006
/usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 nsievebits.c -o nsievebits.gcc_run
=================================================================
COMMAND LINE (%A is single numeric argument):
nsievebits.gcc_run %A
PROGRAM OUTPUT
==============
Primes up to 5120000 356244
Primes up to 2560000 187134
Primes up to 1280000 98610
*****/
|