xref: /illumos-gate/usr/src/cmd/sgs/common/findprime.c (revision 69b1fd3f24d0ee2e682883606201c61f52085805)
1*69b1fd3fSRichard Lowe /*
2*69b1fd3fSRichard Lowe  * CDDL HEADER START
3*69b1fd3fSRichard Lowe  *
4*69b1fd3fSRichard Lowe  * The contents of this file are subject to the terms of the
5*69b1fd3fSRichard Lowe  * Common Development and Distribution License, Version 1.0 only
6*69b1fd3fSRichard Lowe  * (the "License").  You may not use this file except in compliance
7*69b1fd3fSRichard Lowe  * with the License.
8*69b1fd3fSRichard Lowe  *
9*69b1fd3fSRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*69b1fd3fSRichard Lowe  * or http://www.opensolaris.org/os/licensing.
11*69b1fd3fSRichard Lowe  * See the License for the specific language governing permissions
12*69b1fd3fSRichard Lowe  * and limitations under the License.
13*69b1fd3fSRichard Lowe  *
14*69b1fd3fSRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
15*69b1fd3fSRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*69b1fd3fSRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
17*69b1fd3fSRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
18*69b1fd3fSRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
19*69b1fd3fSRichard Lowe  *
20*69b1fd3fSRichard Lowe  * CDDL HEADER END
21*69b1fd3fSRichard Lowe  */
22*69b1fd3fSRichard Lowe /*
23*69b1fd3fSRichard Lowe  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*69b1fd3fSRichard Lowe  * Use is subject to license terms.
25*69b1fd3fSRichard Lowe  */
26*69b1fd3fSRichard Lowe 
27*69b1fd3fSRichard Lowe #include <sys/types.h>
28*69b1fd3fSRichard Lowe 
29*69b1fd3fSRichard Lowe /*
30*69b1fd3fSRichard Lowe  * function that will find a prime'ish number.  Usefull for
31*69b1fd3fSRichard Lowe  * hashbuckets and related things.
32*69b1fd3fSRichard Lowe  */
33*69b1fd3fSRichard Lowe uint_t
findprime(uint_t count)34*69b1fd3fSRichard Lowe findprime(uint_t count)
35*69b1fd3fSRichard Lowe {
36*69b1fd3fSRichard Lowe 	uint_t	h, f;
37*69b1fd3fSRichard Lowe 
38*69b1fd3fSRichard Lowe 	if (count <= 3)
39*69b1fd3fSRichard Lowe 		return (3);
40*69b1fd3fSRichard Lowe 
41*69b1fd3fSRichard Lowe 
42*69b1fd3fSRichard Lowe 	/*
43*69b1fd3fSRichard Lowe 	 * Check to see if divisible by two, if so
44*69b1fd3fSRichard Lowe 	 * increment.
45*69b1fd3fSRichard Lowe 	 */
46*69b1fd3fSRichard Lowe 	if ((count & 0x1) == 0)
47*69b1fd3fSRichard Lowe 		count++;
48*69b1fd3fSRichard Lowe 
49*69b1fd3fSRichard Lowe 	for (h = count, f = 2; f * f <= h; f++)
50*69b1fd3fSRichard Lowe 		if ((h % f) == 0)
51*69b1fd3fSRichard Lowe 			h += f = 1;
52*69b1fd3fSRichard Lowe 	return (h);
53*69b1fd3fSRichard Lowe }
54