所以我更改了我的脚本,以在用户最大值以下找到两个连续数字的乘积,现在发生了这种情况

So I have changed my script to find the product of two numbers in succession to each other below users maxvalue now this happens

所以这个脚本应该允许用户输入一个最大值,他们希望找到所有小于或等于最大值的两个连续数字的乘积的数字。最终发生的是它大部分都有效,但似乎比最大值高出一个数字,而不是在最大值处或之前停止,我不确定为什么会这样。任何帮助表示赞赏。下面是我尝试过的两个不同最大值的代码和输出。

代码

#!/bin/bash
#Task 3.
#This part allows the user to set the range of numbers they wish to find the products of two nonnegative number in succession to each other
count=0
echo "Input a maximum value."

read -r maxval
count=1
val1=0
val2=1
term=$((val1*val2))

echo "Ok all the products of two numbers in succession to each smaller than or equal to $maxval are as follows."

while [ "$term" -le "$maxval" ]
do
        term=$(($val1*$val2))
        echo "$term"
        ((val1++))
        ((val2++))
        ((count++))

done

echo "You have now reached the end of your range of products with integers in succession to each other smaller than or equal to $maxval."
echo "The total amount of products found in your range of products with integers in succesion to each other was $count"

输出 1

Input a maximum value.
13
Ok all the products of two numbers in succession to each smaller than or equal to 13 are as follows.
0
2
6
12
20
You have now reached the end of your range of products with integers in succession to each other smaller than or equal to 13.
The total amount of products found in your range of products with integers in succesion to each other was 6

输出 2

Input a maximum value.
154
Ok all the products of two numbers in succession to each smaller than or equal to 154 are as follows.
0
2
6
12
20
30
42
56
72
90
110
132
156
You have now reached the end of your range of products with integers in succession to each other smaller than or equal to 154.
The total amount of products found in your range of products with integers in succesion to each other was 14

问题的要点是 term 正在计算(并打印) 测试之后;你想切换它,以便你在 测试之前计算。

一个想法:

while [ "$term" -le "$maxval" ]       # if we pass the test ...
do
        echo "$term"                  # dislplay and ...
        ((val1++))
        ((val2++))
        ((count++))                   # optimistically +1 assuming next test is successful and ...
        term=$(($val1*$val2))         # calculate for the next test
done

((count--))                           # last test was unsuccessful so -1

测试运行:

$ ./count.bash
Input a maximum value.
13
Ok all the products of two numbers in succession to each smaller than or equal to 13 are as follows.
0
2
6
12
You have now reached the end of your range of products with integers in succession to each other smaller than or equal to 13.
The total amount of products found in your range of products with integers in succesion to each other was 4

$ ./count.bash
Input a maximum value.
154
Ok all the products of two numbers in succession to each smaller than or equal to 154 are as follows.
0
2
6
12
20
30
42
56
72
90
110
132
You have now reached the end of your range of products with integers in succession to each other smaller than or equal to 154.
The total amount of products found in your range of products with integers in succesion to each other was 12