summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/prime.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/prime.c')
-rw-r--r--misc/examples/prime.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index d0887353..c3a0663c 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -5,20 +5,22 @@
#include <stc/algo/filter.h>
#include <stc/algo/crange.h>
+typedef long long llong;
-cbits sieveOfEratosthenes(int64_t n)
+
+cbits sieveOfEratosthenes(llong n)
{
cbits bits = cbits_with_size(n/2 + 1, true);
- int64_t q = (int64_t)sqrt((double) n) + 1;
- for (int64_t i = 3; i < q; i += 2) {
- int64_t j = i;
+ llong q = (llong)sqrt((double) n) + 1;
+ for (llong i = 3; i < q; i += 2) {
+ llong j = i;
for (; j < n; j += 2) {
if (cbits_test(&bits, j>>1)) {
i = j;
break;
}
}
- for (int64_t j = i*i; j < n; j += i*2)
+ for (llong j = i*i; j < n; j += i*2)
cbits_reset(&bits, j>>1);
}
return bits;
@@ -26,15 +28,15 @@ cbits sieveOfEratosthenes(int64_t n)
int main(void)
{
- int64_t n = 1000000000;
- printf("Computing prime numbers up to %" c_ZI "\n", n);
+ llong n = 100000000;
+ printf("Computing prime numbers up to %lld\n", n);
clock_t t1 = clock();
cbits primes = sieveOfEratosthenes(n + 1);
- int64_t np = cbits_count(&primes);
+ llong np = cbits_count(&primes);
clock_t t2 = clock();
- printf("Number of primes: %" c_ZI ", time: %f\n\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
+ printf("Number of primes: %lld, time: %f\n\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
puts("Show all the primes in the range [2, 1000):");
printf("2");
c_forrange (i, 3, 1000, 2)