#!/usr/bin/sh # # Counts from 1 to max and prints numbers whose digits # don't repeat. Displays the biggest jump and the total # count of numbers. # Author: sharfah max=$1 counter=1 prev=$counter maxdiff=0 total=0 while ( [ $counter -le $max ] ) do echo $counter | grep '\(.\).*\1' > /dev/null if [ $? -ne 0 ] then echo "$counter" total=`expr $total + 1` diff=`expr $counter - $prev` if [ $diff -gt $maxdiff ] then maxdiff=$diff from=$prev to=$counter fi prev=$counter fi counter=`expr $counter + 1` done echo "Biggest jump is $maxdiff:$from->$to" echo "Total count of numbers is $total"Output
Run on an Intel(R) Xeon(R) CPU 2.33GHz 8 CPU, SUSE LINUX Enterprise Server 9, it produced:
sharfah@starship:~> time ./beust.sh 10000 > beust.log 38.68s real 12.71s user 25.80s system sharfah@starship:~> tail beust.log 9867 9870 9871 9872 9873 9874 9875 9876 Biggest jump is 105:1098->1203 Total count of numbers is 5274When run on Solaris 10, it took 1m26.219s. If you think you can make this go faster, let me know how in the comments section!