我試圖從Python(What is the best algorithm for checking if a number is prime?)「拼寫」到Shell腳本。這是我的想法(可能是很無聊的代碼):Shell腳本文本
#!/bin/bash
prime(){
i=5
w=2
while [ `echo "$i*$i" | bc -l ` -le $n ]
do
if [ n % i -eq 0 ]
then echo "$n is not prime"
else
i = i + w
w = 6 - w
echo "$n is prime"
fi
done
}
echo "Test for knowing if a number is prime or not."
sleep 2
echo "Enter the number"
read n
if [ $n -eq 1 ]
then echo "Number 1 is not prime"
elif [ $n -eq 2 ]
then echo "Number two is prime"
elif [ $n -eq 3 ]
then echo "Number three is prime"
else
prime
fi
的問題是,當我把其他任何數字(4,5,6,...)程序不回回來,如果$ n是素數或不。如果有人能幫助我,我將非常感激。
問題是什麼? – csmckelvey 2014-12-06 19:20:26
我忘了,很抱歉。我編輯了這個問題。感謝您的觀察。 – Carlos 2014-12-06 19:25:06
如果你想繼續保持同樣的態度,那麼你有一個漫長的夜晚... – 2014-12-06 19:37:35