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