File: generate_n_primes2.c

package info (click to toggle)
primesieve 7.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,080 kB
  • sloc: cpp: 7,270; ansic: 455; sh: 199; makefile: 86
file content (91 lines) | stat: -rw-r--r-- 1,847 bytes parent folder | download | duplicates (2)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
///
/// @file   generate_n_primes2.c
/// @brief  Test n prime number generation.
///
/// Copyright (C) 2017 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve.h>

#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

// primes inside [0, 100]
const uint64_t small_primes[25] =
{
   2,  3,  5,  7, 11,
  13, 17, 19, 23, 29,
  31, 37, 41, 43, 47,
  53, 59, 61, 67, 71,
  73, 79, 83, 89, 97
};

// primes inside [10^16, 10^16 + 1000]
const uint64_t large_primes[20] =
{
  10000000000000061ull,
  10000000000000069ull,
  10000000000000079ull,
  10000000000000099ull,
  10000000000000453ull,
  10000000000000481ull,
  10000000000000597ull,
  10000000000000613ull,
  10000000000000639ull,
  10000000000000669ull,
  10000000000000753ull,
  10000000000000793ull,
  10000000000000819ull,
  10000000000000861ull,
  10000000000000897ull,
  10000000000000909ull,
  10000000000000931ull,
  10000000000000949ull,
  10000000000000957ull,
  10000000000000991ull,
};

void check(int OK)
{
  if (OK)
    printf("   OK\n");
  else
  {
    printf("   ERROR\n");
    exit(1);
  }
}

int main()
{
  size_t i;
  size_t size = 25;
  uint64_t* primes = (uint64_t*) primesieve_generate_n_primes(size, 0, UINT64_PRIMES);

  for (i = 0; i < size; i++)
  {
    printf("primes[%zu] = %" PRIu64, i, primes[i]);
    check(primes[i] == small_primes[i]);
  }

  primesieve_free(primes);
  size = 20;
  primes = (uint64_t*) primesieve_generate_n_primes(size, 10000000000000000ull, UINT64_PRIMES);

  for (i = 0; i < size; i++)
  {
    printf("primes[%zu] = %" PRIu64, i, primes[i]);
    check(primes[i] == large_primes[i]);
  }

  primesieve_free(primes);
  printf("\n");
  printf("All tests passed successfully!\n");

  return 0;
}