xref: /titanic_41/usr/src/lib/libshell/common/scripts/primenumbers1.sh (revision dc5982c9e1ebe3315aac975f1c4f1ad46c83c2d2)
1#!/usr/bin/ksh93
2
3#
4# CDDL HEADER START
5#
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.
9#
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.
14#
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]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
26#
27
28#
29# primenumbers1 - a simple prime number generator
30#
31
32# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
33export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
34
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).
39if [[ "${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
46fi
47export LC_NUMERIC=C
48
49
50# check whether arg1 is a prime number via comparing it against the "pn" array
51function is_prime
52{
53	integer i
54	integer num=$1
55	float   max_pn
56
57	(( max_pn=sqrt(num)+1. ))
58
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
63}
64
65# main
66set -o errexit
67
68# get arguments
69integer max_prime=$1 # maximum prime number
70typeset outputformat=$2
71
72# variables
73integer -a pn		# integer array for the prime numbers
74integer num_pn=1	# number of prime numbers
75integer n		# current number which should be tested
76pn[0]=2			# start value
77
78# prechecks
79(( max_prime > 1 )) || { print -u2 -f "%s: requires a positive integer as first input.\n" "$0" ; exit 1 ; }
80
81# calculate prime numbers
82printf $"# %s: Calculating prime numbes from 1 to %i\n" "${ date '+%T' ; }" max_prime 1>&2
83
84for (( n=3 ; n < max_prime ; n+=2 )) ; do
85	if is_prime $n ; then
86		(( pn[num_pn++]=n ))
87	fi
88done
89
90# print results
91printf $"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2
92
93for (( 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]
98			;;
99		line)
100			printf $"%i\n" pn[n]
101			;;
102		*)
103			printf $"prime %i:\t%i\n" n pn[n]
104			;;
105	esac
106done
107
108if [[ ${outputformat} == "block" ]] && (( n % 8 != 1 )); then
109	print
110fi
111
112printf $"# %s: Done.\n" "${ date '+%T' ; }" 1>&2
113
114#EOF.
115