8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libshell / common / scripts / primenumbers1.sh
blobb408eb3049ef2db6f7325317695be0dc747012bc
1 #!/usr/bin/ksh93
4 # CDDL HEADER START
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]
21 # CDDL HEADER END
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/xpg6/bin:/usr/xpg4/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
40 export \
41 LC_MONETARY="${LC_ALL}" \
42 LC_MESSAGES="${LC_ALL}" \
43 LC_COLLATE="${LC_ALL}" \
44 LC_CTYPE="${LC_ALL}"
45 unset LC_ALL
47 export LC_NUMERIC=C
50 # check whether arg1 is a prime number via comparing it against the "pn" array
51 function is_prime
53 integer i
54 integer num=$1
55 float max_pn
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;
61 done
62 return 0
65 # main
66 set -o errexit
68 # get arguments
69 integer max_prime=$1 # maximum prime number
70 typeset outputformat=$2
72 # variables
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
76 pn[0]=2 # start value
78 # prechecks
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
85 if is_prime $n ; then
86 (( pn[num_pn++]=n ))
88 done
90 # print results
91 printf $"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2
93 for (( n=0 ; n < num_pn ; n++ )) ; do
94 # print prime number
95 case ${outputformat} in
96 block)
97 printf $"%i$( (( n % 8 == 0 )) && print -r '\n' || print -r ',\t')" pn[n]
99 line)
100 printf $"%i\n" pn[n]
103 printf $"prime %i:\t%i\n" n pn[n]
105 esac
106 done
108 if [[ ${outputformat} == "block" ]] && (( n % 8 != 1 )); then
109 print
112 printf $"# %s: Done.\n" "${ date '+%T' ; }" 1>&2
114 #EOF.