#bash #shell #scripting
#bash #оболочка #сценарии
Вопрос:
Предполагается, что этот скрипт позволяет пользователю вводить максимальное значение, которое они хотят найти, все числа, которые являются произведением двух последовательных чисел друг за другом, меньше или равны максимальному значению. В итоге это в основном работает, но, похоже, превышает максимальное значение на одно число вместо того, чтобы останавливаться на максимальном значении или перед ним, и я не уверен, почему он ведет себя именно так. Любая помощь приветствуется. Ниже приведен код и вывод двух разных максимальных значений, которые я пробовал.
Код
#!/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
Ответ №1:
Суть проблемы в том, что 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