langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
37
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-1.sh
Normal file
37
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-1.sh
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/bin/bash
|
||||
# seq is the array genereated by hailstone
|
||||
# index is used for seq
|
||||
declare -a seq
|
||||
declare -i index
|
||||
|
||||
# Create a routine to generate the hailstone sequence for a number
|
||||
hailstone () {
|
||||
unset seq index
|
||||
seq[$((index++))]=$((n=$1))
|
||||
while [ $n -ne 1 ]; do
|
||||
[ $((n % 2)) -eq 1 ] && ((n=n*3+1)) || ((n=n/2))
|
||||
seq[$((index++))]=$n
|
||||
done
|
||||
}
|
||||
|
||||
# Use the routine to show that the hailstone sequence for the number 27
|
||||
# has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
|
||||
i=27
|
||||
hailstone $i
|
||||
echo "$i: ${#seq[@]}"
|
||||
echo "${seq[@]:0:4} ... ${seq[@]:(-4):4}"
|
||||
|
||||
# Show the number less than 100,000 which has the longest hailstone
|
||||
# sequence together with that sequences length.
|
||||
# (But don't show the actual sequence)!
|
||||
max=0
|
||||
maxlen=0
|
||||
for ((i=1;i<100000;i++)); do
|
||||
hailstone $i
|
||||
if [ $((len=${#seq[@]})) -gt $maxlen ]; then
|
||||
max=$i
|
||||
maxlen=$len
|
||||
fi
|
||||
done
|
||||
|
||||
echo "${max} has a hailstone sequence length of ${maxlen}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue