Linux编写1到100的素数,打印1-100以内的素数
Linux Bash/Shell:[[emailprotected]shell]#catprime.sh#!/bin/sh#Printprimefrom1to100for((i=1;i<=100;i++))doif[$i-eq1];thencontinuefiflag=0for((j=2;jdoa=$[$i%$j]if[$a-eq0];then...
Linux Bash/Shell:[[email protected] shell]# cat prime.sh
#!/bin/sh
# Print prime from 1 to 100
for ((i=1;i<=100;i++))
do
if [ $i -eq 1 ];then
continue
fi
flag=0
for ((j=2;j
do
a=$[$i % $j]
if [ $a -eq 0 ];then
flag=1
fi
done
if [ $flag -eq 0 ];then
echo $i is prime
fi
done
Python写法(2.6.6):Python写法1
[[email protected] python]# cat prime.py
#!/usr/bin/python
#Print prime 1-100
for i in range(1,100):
if i == 1:
continue
for j in range(2,i):
if i % j == 0:
break
else:
print i, ‘is a prime‘
Python写法2
[[email protected] python]# cat while_break.py
#!/usr/bin/python
i = 2
while i
j = 2
while j <= (i/j):
if not(i%j):
break
j += 1
else:
print i, ‘is prime‘
i += 1
本文出自 “lisp的运维之路” 博客,谢绝转载!
原文:http://lspgyy.blog.51cto.com/5264172/1535176
更多推荐
所有评论(0)