I'm just learning to code with bash, and I'm running into some problems doing some exercises. I feel like I might be doing something wrong with the syntax, but no matter how I look at it it doesn't look wrong.
The code I've got here is supposed to find the 'lucky numbers' between 1000 and 10000. In this case, a lucky number is a number who's digits sum to a number that in turns sums to 7. The part that does the calculation of the lucky number works on it's own, but once I put it into a for loop, where there is no single input, the output is infinite and wrong. I can only assume the for loop is the problem:
digit=0
sum=0
for (( i=1000; i<=10000; i++ ))
do
while [ $i -gt 0 ]
do
digit=$(( $i % 10 ))
i=$(( $i / 10 ))
sum=$(( $sum + $digit ))
done
while [ $sum -gt 0 ]
do
digit=$(( $sum % 10 ))
sum$(( $sum / 10 ))
sum2=$(( $sum2 + $digit ))
done
if [[ $sum2 == 7 ]];
then
echo $sum2
fi
done
If anyone has any idea what I'm doing wrong, or even a way to make it better, I would really appreciate it!
sum$(( $sum ? 10 )). I'm assuming that's a copy/paste error.1<=10000is always true.iinside the loop, which is generally a bad idea.i<=10000.