#!/bin/bash filename="primes_find.sh" # find prime numbers (Base 10), limited to search in a set of $n digits long # # reads source number length as arg1 (eg "primes_find.sh 2") or prompts for value # # Pete Nesbitt Sept 2024 ###################################### # variables # # populate number of digits with arg1, or have user input DIGITS=$1 MAX_DIGITS=4 # base on your CPU power. # eg. my 8 core Intel i7: 3 digits: about 5 secs, 4 digits about a minute, 5 is crazy! #set an empty var for list of all primes to display at end primes_found="" primes_found_num="" # something to capture number list of checked numbers checked_list="" ################################### # functions # get_range() { #set the range of digits to test # $DIGITS for dig=5, 1 followed 4 0's # if 1 then just set if [ ${DIGITS} -eq 1 ]; then RANGE_START=1 RANGE_END=9 else # start is like 1000 RANGE_START="`seq -s' ' 1 ${DIGITS} | sed -e s/[2-${DIGITS}]/0/g -e 's/ //g'`" # end is like 9999 RANGE_END="`seq -s' ' 1 ${DIGITS} | sed -e s/[2-${DIGITS}]/0/g -e 's/[0-1]/9/g' -e 's/ //g'`" fi } check_input() { # provide input box if arg one not provided if [ -z "${DIGITS}" ]; then # input provided # DIGITS=$1 #else # prompt user for input printf " => Enter a Number from 1 to ${MAX_DIGITS} (ex: 2 represents two digit numbers): " read DIGITS fi } #### start of work #### ###################### ## see if arg1 provided check_input # will prompt if no arg1 if [ -n ${DIGITS} ] && [ ${DIGITS} -ge 1 ] && [ ${DIGITS} -le ${MAX_DIGITS} ] ; then #not empty, so input string provided # call function to find RANGE get_range else # print error then exit. echo -e "\n\t**** Error ****\n\tusage: ${filename} 'Number'\n\tNumber is a value from 1 to ${MAX_DIGITS}\n example: 2 represents two digit numbers, values 10 through 99" exit 1 fi ############# clear # add -x to retain scroll-back screen buffer (command history is not impacted) echo -e "\n Checking for Prime Numbers from ${RANGE_START} to ${RANGE_END}..." # check for prime numbers # feed string to prime checker for s_num in `seq -s' ' ${RANGE_START} ${RANGE_END}` do # factor outputs (for 91 shows: 91: 7 13 ), we want right side only made_of=`factor ${s_num}| sed 's|.*:||'` # right side # if only one value on right side it is a prime #numbers on right (0, 1 or more) f_num=`echo ${made_of} | sed 's|.*:||' | wc -w` if [ ${f_num} -eq 1 ]; then # append to a var for final summary primes_found="${primes_found}\n\t${s_num}" #primes_found="${primes_found} ${s_num}" # increment total count of primes found, for final display tally let primes_found_num++ fi done #### # print results echo if [ -n "${primes_found}" ]; then # primes found (var not empty) if [ ${primes_found_num} -eq 1 ];then echo " This one Prime Number was found:" else echo -e " Found these ${primes_found_num} Prime Numbers:\n" fi #echo -e "\t ${primes_found}" echo -e "\n${primes_found}" | column -c 40 # --fillrows #(this fills horizontally first) echo else # if no primes found: echo " There were NO Prime Numbers found" echo fi exit #eof