1*906afcb8SAndy Fiddaman#!/usr/bin/ksh93 2*906afcb8SAndy Fiddaman 3*906afcb8SAndy Fiddaman# 4*906afcb8SAndy Fiddaman# CDDL HEADER START 5*906afcb8SAndy Fiddaman# 6*906afcb8SAndy Fiddaman# The contents of this file are subject to the terms of the 7*906afcb8SAndy Fiddaman# Common Development and Distribution License (the "License"). 8*906afcb8SAndy Fiddaman# You may not use this file except in compliance with the License. 9*906afcb8SAndy Fiddaman# 10*906afcb8SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11*906afcb8SAndy Fiddaman# or http://www.opensolaris.org/os/licensing. 12*906afcb8SAndy Fiddaman# See the License for the specific language governing permissions 13*906afcb8SAndy Fiddaman# and limitations under the License. 14*906afcb8SAndy Fiddaman# 15*906afcb8SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each 16*906afcb8SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17*906afcb8SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the 18*906afcb8SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying 19*906afcb8SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner] 20*906afcb8SAndy Fiddaman# 21*906afcb8SAndy Fiddaman# CDDL HEADER END 22*906afcb8SAndy Fiddaman# 23*906afcb8SAndy Fiddaman 24*906afcb8SAndy Fiddaman# 25*906afcb8SAndy Fiddaman# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 26*906afcb8SAndy Fiddaman# 27*906afcb8SAndy Fiddaman 28*906afcb8SAndy Fiddaman# 29*906afcb8SAndy Fiddaman# primenumbers1 - a simple prime number generator 30*906afcb8SAndy Fiddaman# 31*906afcb8SAndy Fiddaman 32*906afcb8SAndy Fiddaman# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant 33*906afcb8SAndy Fiddamanexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin 34*906afcb8SAndy Fiddaman 35*906afcb8SAndy Fiddaman# Make sure all math stuff runs in the "C" locale to avoid problems 36*906afcb8SAndy Fiddaman# with alternative # radix point representations (e.g. ',' instead of 37*906afcb8SAndy Fiddaman# '.' in de_DE.*-locales). This needs to be set _before_ any 38*906afcb8SAndy Fiddaman# floating-point constants are defined in this script). 39*906afcb8SAndy Fiddamanif [[ "${LC_ALL}" != "" ]] ; then 40*906afcb8SAndy Fiddaman export \ 41*906afcb8SAndy Fiddaman LC_MONETARY="${LC_ALL}" \ 42*906afcb8SAndy Fiddaman LC_MESSAGES="${LC_ALL}" \ 43*906afcb8SAndy Fiddaman LC_COLLATE="${LC_ALL}" \ 44*906afcb8SAndy Fiddaman LC_CTYPE="${LC_ALL}" 45*906afcb8SAndy Fiddaman unset LC_ALL 46*906afcb8SAndy Fiddamanfi 47*906afcb8SAndy Fiddamanexport LC_NUMERIC=C 48*906afcb8SAndy Fiddaman 49*906afcb8SAndy Fiddaman 50*906afcb8SAndy Fiddaman# check whether arg1 is a prime number via comparing it against the "pn" array 51*906afcb8SAndy Fiddamanfunction is_prime 52*906afcb8SAndy Fiddaman{ 53*906afcb8SAndy Fiddaman integer i 54*906afcb8SAndy Fiddaman integer num=$1 55*906afcb8SAndy Fiddaman float max_pn 56*906afcb8SAndy Fiddaman 57*906afcb8SAndy Fiddaman (( max_pn=sqrt(num)+1. )) 58*906afcb8SAndy Fiddaman 59*906afcb8SAndy Fiddaman for (( i=0 ; i < num_pn && pn[i] < max_pn ; i++)) ; do 60*906afcb8SAndy Fiddaman (( num % pn[i] == 0 )) && return 1; 61*906afcb8SAndy Fiddaman done 62*906afcb8SAndy Fiddaman return 0 63*906afcb8SAndy Fiddaman} 64*906afcb8SAndy Fiddaman 65*906afcb8SAndy Fiddaman# main 66*906afcb8SAndy Fiddamanset -o errexit 67*906afcb8SAndy Fiddaman 68*906afcb8SAndy Fiddaman# get arguments 69*906afcb8SAndy Fiddamaninteger max_prime=$1 # maximum prime number 70*906afcb8SAndy Fiddamantypeset outputformat=$2 71*906afcb8SAndy Fiddaman 72*906afcb8SAndy Fiddaman# variables 73*906afcb8SAndy Fiddamaninteger -a pn # integer array for the prime numbers 74*906afcb8SAndy Fiddamaninteger num_pn=1 # number of prime numbers 75*906afcb8SAndy Fiddamaninteger n # current number which should be tested 76*906afcb8SAndy Fiddamanpn[0]=2 # start value 77*906afcb8SAndy Fiddaman 78*906afcb8SAndy Fiddaman# prechecks 79*906afcb8SAndy Fiddaman(( max_prime > 1 )) || { print -u2 -f "%s: requires a positive integer as first input.\n" "$0" ; exit 1 ; } 80*906afcb8SAndy Fiddaman 81*906afcb8SAndy Fiddaman# calculate prime numbers 82*906afcb8SAndy Fiddamanprintf $"# %s: Calculating prime numbes from 1 to %i\n" "${ date '+%T' ; }" max_prime 1>&2 83*906afcb8SAndy Fiddaman 84*906afcb8SAndy Fiddamanfor (( n=3 ; n < max_prime ; n+=2 )) ; do 85*906afcb8SAndy Fiddaman if is_prime $n ; then 86*906afcb8SAndy Fiddaman (( pn[num_pn++]=n )) 87*906afcb8SAndy Fiddaman fi 88*906afcb8SAndy Fiddamandone 89*906afcb8SAndy Fiddaman 90*906afcb8SAndy Fiddaman# print results 91*906afcb8SAndy Fiddamanprintf $"# %s: Calculation done, printing results:\n" "${ date '+%T' ; }" 1>&2 92*906afcb8SAndy Fiddaman 93*906afcb8SAndy Fiddamanfor (( n=0 ; n < num_pn ; n++ )) ; do 94*906afcb8SAndy Fiddaman # print prime number 95*906afcb8SAndy Fiddaman case ${outputformat} in 96*906afcb8SAndy Fiddaman block) 97*906afcb8SAndy Fiddaman printf $"%i$( (( n % 8 == 0 )) && print -r '\n' || print -r ',\t')" pn[n] 98*906afcb8SAndy Fiddaman ;; 99*906afcb8SAndy Fiddaman line) 100*906afcb8SAndy Fiddaman printf $"%i\n" pn[n] 101*906afcb8SAndy Fiddaman ;; 102*906afcb8SAndy Fiddaman *) 103*906afcb8SAndy Fiddaman printf $"prime %i:\t%i\n" n pn[n] 104*906afcb8SAndy Fiddaman ;; 105*906afcb8SAndy Fiddaman esac 106*906afcb8SAndy Fiddamandone 107*906afcb8SAndy Fiddaman 108*906afcb8SAndy Fiddamanif [[ ${outputformat} == "block" ]] && (( n % 8 != 1 )); then 109*906afcb8SAndy Fiddaman print 110*906afcb8SAndy Fiddamanfi 111*906afcb8SAndy Fiddaman 112*906afcb8SAndy Fiddamanprintf $"# %s: Done.\n" "${ date '+%T' ; }" 1>&2 113*906afcb8SAndy Fiddaman 114*906afcb8SAndy Fiddaman#EOF. 115