source: trunk/Softwares/Common/src/c/func_premier.c

Last change on this file was 102, checked in by rosiere, 16 years ago

Previous commit with new files :P

  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1#include "func_math.h"
2#include "func_premier.h"
3#include "func_io.h"
4
5//-----[ Premier ]---------------------------------------------------------
6
7unsigned int premier(unsigned int x)
8{
9  unsigned int it;
10
11  if (x <= 0)
12    return 0;
13 
14  if (x <= 2)
15    return 1;
16
17  for(it=2; it <= (x/2); it++)
18    if (modulo(x,it) == 0)
19      return 0;
20 
21  return 1;
22}
23
24unsigned int n_premier(unsigned int nieme_premier)
25{
26  unsigned int nb_find  = 0;
27  unsigned int res      = 1;
28  while (nb_find < nieme_premier)
29    {
30      res ++;
31      if (premier(res) == 1)
32        nb_find ++;
33    }
34  return res;
35}
36
37//-------------------------------------------------------------------------
38//-----[ Test ]------------------------------------------------------------
39//-------------------------------------------------------------------------
40
41void test_n_premier (int x)
42{
43  int x_max = 30;
44  int wait [x_max+1];
45
46  wait [ 0] =   1; // 1
47  wait [ 1] =   2; // 2
48  wait [ 2] =   3; // 3
49  wait [ 3] =   5; // 5
50  wait [ 4] =   7; // 7
51  wait [ 5] =  11; // b
52  wait [ 6] =  13; // d
53  wait [ 7] =  17; // 11
54  wait [ 8] =  19; // 13
55  wait [ 9] =  23; // 17
56  wait [10] =  29; // 1d
57  wait [11] =  31; // 1f
58  wait [12] =  37; // 25
59  wait [13] =  41; // 29
60  wait [14] =  43; // 2b
61  wait [15] =  47; // 2f
62  wait [16] =  53; // 35
63  wait [17] =  59; // 3b
64  wait [18] =  61; // 3d
65  wait [19] =  67; // 43
66  wait [20] =  71; // 47
67  wait [21] =  73; // 49
68  wait [22] =  79; // 4f
69  wait [23] =  83; // 53
70  wait [24] =  89; // 59
71  wait [25] =  97; // 61
72  wait [26] = 101; // 65
73  wait [27] = 103; // 67
74  wait [28] = 107; // 6b
75  wait [29] = 109; // 6d
76  wait [30] = 113; // 71
77 
78  for (int i = 0; i <= ((x<x_max)?x:x_max); i ++)
79    if (n_premier (i) != wait[i]) quit(i+1);
80}
Note: See TracBrowser for help on using the repository browser.