标签:
1. 求50到100之间的素数
import math for i in range(50,100+1): for j in range(2,int(math.sqrt(i))+1): if i%j==0: break else: print(i)
2. 求一元二次方程的解:ax2+bx+c=0
import math def quad(a,b,c): if not isinstance(a,(int,float)) and isinstance(b,(int,float)) and isinstance(c,(int,float)): raise TypeError(‘bad operand type‘) return ‘ABC\‘re Error‘ else: if a==0 and b!=0: return (-c)/b elif a==0 and b==0: return ‘ABC\‘re Error‘ else: m=b*b-4*a*c if m>=0: x1=(-b+math.sqrt(m))/(2*a) x2=(-b-math.sqrt(m))/(2*a) if x1==x2: return x1 else: return x1,x2 else: return complex((-b)/(2*a),(math.sqrt(-m))/(2*a)),complex((-b)/(2*a),-(math.sqrt(-m))/(2*a)) a=int(input(‘plz input a:‘)) b=int(input(‘plz input b:‘)) c=int(input(‘plz input c:‘)) s=quad(a,b,c) print(s)
标签:
原文地址:http://www.cnblogs.com/wffett/p/4572775.html