6 # The contents of this file are subject to the terms of the
7 # Common Development and Distribution License (the "License").
8 # You may not use this file except in compliance with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
25 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
29 # primenumbers1 - a simple prime number generator
32 # Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
33 export PATH
=/usr
/xpg
6/bin
:/usr
/xpg
4/bin
:/bin
:/usr
/bin
35 # Make sure all math stuff runs in the "C" locale to avoid problems
36 # with alternative # radix point representations (e.g. ',' instead of
37 # '.' in de_DE.*-locales). This needs to be set _before_ any
38 # floating-point constants are defined in this script).
39 if [[ "${LC_ALL}" != "" ]] ; then
41 LC_MONETARY
="${LC_ALL}" \
42 LC_MESSAGES
="${LC_ALL}" \
43 LC_COLLATE
="${LC_ALL}" \
50 # check whether arg1 is a prime number via comparing it against the "pn" array
57 (( max_pn
=sqrt
(num
)+1.
))
59 for (( i
=0 ; i
< num_pn
&& pn
[i
] < max_pn
; i
++)) ; do
60 (( num
% pn
[i
] == 0 )) && return 1;
69 integer max_prime
=$1 # maximum prime number
70 typeset outputformat
=$2
73 integer
-a pn
# integer array for the prime numbers
74 integer num_pn
=1 # number of prime numbers
75 integer n
# current number which should be tested
79 (( max_prime
> 1 )) ||
{ print
-u2 -f "%s: requires a positive integer as first input.\n" "$0" ; exit 1 ; }
81 # calculate prime numbers
82 printf $
"# %s: Calculating prime numbes from 1 to %i\n" "${ date '+%T' ; }" max_prime
1>&2
84 for (( n
=3 ; n
< max_prime
; n
+=2 )) ; do
91 printf $
"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2
93 for (( n
=0 ; n
< num_pn
; n
++ )) ; do
95 case ${outputformat} in
97 printf $
"%i$( (( n % 8 == 0 )) && print -r '\n' || print -r ',\t')" pn
[n
]
103 printf $
"prime %i:\t%i\n" n pn
[n
]
108 if [[ ${outputformat} == "block" ]] && (( n
% 8 != 1 )); then
112 printf $
"# %s: Done.\n" "${ date '+%T' ; }" 1>&2