标签:linux shell python 素数 prime
Linux Bash/Shell:
[root@mem 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<i;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 [root@mem 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 [root@mem python]# cat while_break.py #!/usr/bin/python i = 2 while i < 100: j = 2 while j <= (i/j): if not(i%j): break j += 1 else: print i, ‘is prime‘ i += 1
本文出自 “lisp的运维之路” 博客,谢绝转载!
标签:linux shell python 素数 prime
原文地址:http://lspgyy.blog.51cto.com/5264172/1535176