xref: /titanic_44/usr/src/lib/libshell/common/scripts/primenumbers1.sh (revision 47b4653e9ff2a8aebb64f9e357713fd04108674b)
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 2008 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29#
30# primenumbers1 - a simple prime number generator
31#
32
33# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
34export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
35
36# Make sure all math stuff runs in the "C" locale to avoid problems
37# with alternative # radix point representations (e.g. ',' instead of
38# '.' in de_DE.*-locales). This needs to be set _before_ any
39# floating-point constants are defined in this script).
40if [[ "${LC_ALL}" != "" ]] ; then
41    export \
42        LC_MONETARY="${LC_ALL}" \
43        LC_MESSAGES="${LC_ALL}" \
44        LC_COLLATE="${LC_ALL}" \
45        LC_CTYPE="${LC_ALL}"
46        unset LC_ALL
47fi
48export LC_NUMERIC=C
49
50
51# check whether arg1 is a prime number via comparing it against the "pn" array
52function is_prime
53{
54	integer i
55	integer num=$1
56	float   max_pn
57
58	(( max_pn=sqrt(num)+1. ))
59
60	for (( i=0 ; i < num_pn && pn[i] < max_pn ; i++)) ; do
61		(( num % pn[i] == 0 )) && return 1;
62	done
63	return 0
64}
65
66# main
67set -o errexit
68
69# get arguments
70integer max_prime=$1 # maximum prime number
71typeset outputformat=$2
72
73# variables
74integer -a pn		# integer array for the prime numbers
75integer num_pn=1	# number of prime numbers
76integer n		# current number which should be tested
77pn[0]=2			# start value
78
79# prechecks
80(( max_prime > 1 )) || { print -u2 -f "%s: requires a positive integer as first input.\n" "$0" ; exit 1 ; }
81
82# calculate prime numbers
83printf $"# %s: Calculating prime numbes from 1 to %i\n" "${ date '+%T' ; }" max_prime 1>&2
84
85for (( n=3 ; n < max_prime ; n+=2 )) ; do
86	if is_prime $n ; then
87		(( pn[num_pn++]=n ))
88	fi
89done
90
91# print results
92printf $"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2
93
94for (( n=0 ; n < num_pn ; n++ )) ; do
95	# print prime number
96	case ${outputformat} in
97		block)
98			printf $"%i$( (( n % 8 == 0 )) && print -r '\n' || print -r ',\t')" pn[n]
99			;;
100		line)
101			printf $"%i\n" pn[n]
102			;;
103		*)
104			printf $"prime %i:\t%i\n" n pn[n]
105			;;
106	esac
107done
108
109if [[ ${outputformat} == "block" ]] && (( n % 8 != 1 )); then
110	print
111fi
112
113printf $"# %s: Done.\n" "${ date '+%T' ; }" 1>&2
114
115#EOF.
116