-
Notifications
You must be signed in to change notification settings - Fork 0
/
series
executable file
·56 lines (38 loc) · 847 Bytes
/
series
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
equation="$1"
shift 1
while getopts 'y:d:z:s:' opt; do
case "$opt" in
y ) yeq="${OPTARG%;*}"
y="${OPTARG#*;}";;
d ) detail=$OPTARG;;
z ) zeq="${OPTARG%;*}"
z="${OPTARG#*;}";;
s ) start="$OPTARG";;
esac
done
fact() {
declare -i number=$1 result=1
while [ $number -gt 1 ]; do
result="$((result * number))"
number="$((number - 1))"
#echo "for the number: $1 factorial is: $result"
done
echo $result
}
series=$start
#1 = +, 0 = -
for i in $(seq $detail); do
part="$equation"
part="${part//y/$y}"
part="${part//z/$z}"
for factExp in $(echo "$part" | egrep -o "[0-9]{1,}!"); do
factRes=$(fact ${factExp%!})
part="${part//$factExp/$factRes}"
done
series="$series$part"
#Execute user defined y equation
y="$(echo ${yeq//y/$y} | bc)"
z="$(echo "${zeq//z/$z}" | bc)"
done
echo "$series"