Monday, December 20, 2010

Shell Script to Find Armstrong Numbers Between a Given Range


#!/bin/bash
# SCRIPT: armstrong_bw_range.sh
# USAGE: armstrong_bw_range.sh
# PURPOSE: Finding Armstrong numbers between given range.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#Armstrong numbers are the sum of their own digits to the power of
#the number of digits. As that is a slightly brief wording, let me
#give an example:
#153 = 1³ + 5³ + 3³
#Each digit is raised to the power three because 153 has three
#digits. They are totalled and we get the original number again!
#Notice that Armstrong numbers are base dependent,but we'll mainly be
#dealing with base 10 examples.The Armstrong numbers up to 5 digits
#are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084
#
#####################################################################
# Script Starts Here #
#####################################################################

echo -n "Enter the Lower Limit : "
read Start
echo -n "Enter the Upper Limit : "
read Ending

echo "Armstrong Numbers between $Start and $Ending are: "

while [ $Start -le $Ending ]
do
Number=$Start
Length=${#Number}
Sum=0
OldNumber=$Number

while [ $Number -ne 0 ]
do
Rem=$((Number%10))
Number=$((Number/10))
Power=$(echo "$Rem ^ $Length" | bc )
Sum=$((Sum+Power))
done

if [ $Sum -eq $OldNumber ]
then
echo -n "$OldNumber "
fi

let Start++
done
echo


OUTPUT:
# sh armstrong_bw_range.sh
Enter the Lower Limit : 1
Enter the Upper Limit : 500
Armstrong Numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

# sh armstrong_bw_range.sh
Enter the Lower Limit : 1000
Enter the Upper Limit : 10000
Armstrong Numbers between 1000 and 10000 are:
1634 8208 9474

16 comments:

  1. gud page!
    n itz xtremely useful for beginners like me..!

    ReplyDelete
  2. Armstrong numbers are---->
    1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051 88593477 146511208 472335975 534494836 912985153 etc.

    ReplyDelete
  3. 1 to 9 armstrong numbers.........????
    How.....???????/

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. A number N is an Armstrong number of order n (n being the number of digits)
      if
      abcd. . . = a^n + b^n + c^n + d^n .+ ...= N
      so in case of 1 to 9 number of digit is 1 so 1^1 = 1 ..... but 10 has two digit => 1^2 + 0^2 != 10 ,,,,,

      Delete
  4. I was growing up. At first, kids don't venture far from the home base tree. When they get older, faster, more confident of their surroundings, they venture further. nikon coolshot 20

    ReplyDelete
  5. Brilliant stuff! This was hilarious and so well written. I really loved it :)

    ReplyDelete
  6. The information you share is very useful to me, thank you so much for the article

    ReplyDelete
  7. Very good, I think I found the knowledge I needed

    ReplyDelete
  8. There are a few shrouded costs in a payday credit! Keep in mind, in the event that you can't pay this loan and you happen to lose your activity, the clock does not stop. auto title loans near me chicago

    ReplyDelete
  9. Hi i am from dodopackaging.Wonderful Blog! I should need to thank for the undertakings you have made in creating this post. I am confiding in a comparable best work from do my coursework uk later on as well. I expected to thank you for this destinations! Thankful for sharing.
    product packaging

    ReplyDelete