3d45c1ef7b8abc38ed429344d898785091325302
[platform/upstream/glib.git] / glib / gprimes.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "glib.h"
20
21 static const guint g_primes[] =
22 {
23   11,
24   19,
25   37,
26   73,
27   109,
28   163,
29   251,
30   367,
31   557,
32   823,
33   1237,
34   1861,
35   2777,
36   4177,
37   6247,
38   9371,
39   14057,
40   21089,
41   31627,
42   47431,
43   71143,
44   106721,
45   160073,
46   240101,
47   360163,
48   540217,
49   810343,
50   1215497,
51   1823231,
52   2734867,
53   4102283,
54   6153409,
55   9230113,
56   13845163,
57 };
58
59 static const guint g_nprimes = sizeof (g_primes) / sizeof (g_primes[0]);
60
61 guint
62 g_spaced_primes_closest (guint num)
63 {
64   gint i;
65
66   for (i = 0; i < g_nprimes; i++)
67     if (g_primes[i] > num)
68       return g_primes[i];
69
70   return g_primes[g_nprimes - 1];
71 }