return res所在行不正确,return一般只能处理数字和小于128的数字。
假设您的意思是return $res,脚本将运行。
我设法让程序使用与您的代码类似的代码:
#!/bin/bash
catalan() {
local n=$1
#echo "called with $n" >&2
if (( n <= 1 )); then
res=1
else
res=0
for ((i=0; i<n; i++))
do
var1=$(( n-i-1 ))
call1=$(catalan $i)
call2=$(catalan $var1)
res=$(( res+call1*call2 ));
#echo ":$i:$var1: result Call1:$call1: and Call2:$call2: $res" >&2
done
fi
#echo "result is ${res}" >&2
echo "$res"
}
n=$1
until (( pcat > n ))
do catalan "$((pcat++))"
done
echo "all was done"
还有第二个问题,Call1 和 Call2 的值需要相乘,而不是相加。将res+call1+call2 更改为:
res=$(( res+call1*call2 ))
但是生成的代码非常慢。仅计算第十 (10) 个加泰罗尼亚数字,代码就需要 16 秒。
一个将值保存在单个数组中的全新程序:catarray。
这样:
#!/bin/bash
# some initial values to jump start the code:
catarray=( 1 1 2 5 )
#############################################################################
catalan(){
#echo "making call for $1" >&2
local n=$1
# ${#catarray[@]} is the count of values in catarray (last index + 1).
# if the number n to be found is not yet in the array of values of
# catarray then we need to calculate it. Else, we just print the value.
if (( n >= ${#catarray[@]} )); then
#echo "$n is bigger than ${#catarray[@]}" >&2
# this is a new number, lets loop up till we
# fill the array just up to this value
for (( i=${#catarray[@]};i<=n;i++)); do
#echo "fill index $i in array" >&2
# calculate the sum of all terms for catalan of $n.
for(( j=0;j<i;j++ )); do
(( catarray[i] += catarray[j] * catarray[i-j-1] ))
#echo "done math in $i for $j with ${catarray[j]} *
#echo "* ${catarray[i-j-1]} = ${catarray[i]}"
done
done
fi
# After making the math or else we just print the known value.
#printf 'result of catalan number is %s\n' "${catarray[n]}"
}
#############################################################################
catalan "$1"
printf '%s, ' "${catarray[@]}"; echo
Wich 将在 4 毫秒内执行第十 (10) 个加泰罗尼亚语数字。
包含很多回声以“了解”程序的工作原理。您可以取消引用它们。
虽然有一个限制,bash 中的数字应该适合 64 位(对于 64 位计算机)或小于 (2^63-1)。这使得最大的加泰罗尼亚数字可能成为第 35 个。
$ catalan 35
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900,
2674440, 9694845, 35357670, 129644790, 477638700, 1767263190,
6564120420, 24466267020, 91482563640, 343059613650, 1289904147324,
4861946401452, 18367353072152, 69533550916004, 263747951750360,
1002242216651368, 3814986502092304, 14544636039226909,
55534064877048198, 212336130412243110, 812944042149730764,
3116285494907301262
但只需约 20 毫秒即可完成。