primenumbers1.sh revision 3e14f97f673e8a630f076077de35afdd43dc1587
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER START
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# The contents of this file are subject to the terms of the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Common Development and Distribution License (the "License").
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You may not use this file except in compliance with the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# See the License for the specific language governing permissions
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# and limitations under the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# When distributing Covered Code, include this CDDL HEADER in each
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# If applicable, add the following below this CDDL HEADER, with the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# fields enclosed by brackets "[]" replaced with your own identifying
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# information: Portions Copyright [yyyy] [name of copyright owner]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER END
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# primenumbers1 - a simple prime number generator
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Make sure all math stuff runs in the "C" locale to avoid problems
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# with alternative # radix point representations (e.g. ',' instead of
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# '.' in de_DE.*-locales). This needs to be set _before_ any
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# floating-point constants are defined in this script).
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# check whether arg1 is a prime number via comparing it against the "pn" array
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( i=0 ; i < num_pn && pn[i] < max_pn ; i++)) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# get arguments
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chininteger -a pn # integer array for the prime numbers
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chininteger n # current number which should be tested
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin(( max_prime > 1 )) || { print -u2 -f "%s: requires a positive integer as first input.\n" "$0" ; exit 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# calculate prime numbers
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinprintf $"# %s: Calculating prime numbes from 1 to %i\n" "${ date '+%T' ; }" max_prime 1>&2
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# print results
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinprintf $"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # print prime number
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin printf $"%i$( (( n % 8 == 0 )) && print -r '\n' || print -r ',\t')" pn[n]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinif [[ ${outputformat} == "block" ]] && (( n % 8 != 1 )); then