python_exercise_3 Functions

def hello_world():
	print('Hello, world!')

def  hello_name(name):
	print('Hello, '+name+'!')

'''
print 是一个函数,就是将符合规范的括号内容输出到屏幕中,而return是一个关键字,将某些值返回给调用的
地方
'''

def hello_world_return():
	 return 'Hello, world!'

def  hello_name_return(name):
	return 'Hello, '+name+'!'


hello_world()
hello_name("JOE")
print(hello_world_return())
print(hello_name_return("Joe"))



def evaluate(x):
	result = 3*(x ** 2)-x+2
	print(result)
	return result

evaluate(1)



def my_max_1(x,y):
	if(x>y):
		return x
	else:
		return y
def my_max_2(x,y):
	if(x > y):
		return x
	return y


print(my_max_1(10,99))
print(my_max_2(1,999))

#(a)
import math
def is_prime(n):
	for i in range(2,int(math.sqrt(n))):
		if(n%i == 0):
			return False
	return True

print(is_prime(99991))

#(b)
def is_prime_2(n):
	if(n==2 or n== 3):
		return True
	if((n-1)%6 !=0 and (n+1)%6 != 0 ):
		return False

	for i in range(2,int(math.sqrt(n))):
		if(n%i == 0):
			return False
	return True

print(is_prime(99991))

#(c)
def count_prime(n):
	print(str(2)+' '+str(3),end = ' ')
	for x in range(4,n+1):
		if((x-1)%6 !=0 and (x+1)%6 != 0 ):
			continue
		flag = True
		for i in range(2,int(math.sqrt(n))):
			if(x%i == 0):
				flag = False
		if(flag == True):
			print(x,end = ' ')
		else:
			flag = True

count_prime(20)
print()

#(d)
def count_prime_2(n):
	if(n == 1):
		return 2
	if(n == 2):
		return 3
	num_found = 2
	x = 5
	while(True):
		
		if((x-1)%6 !=0 and (x+1)%6 != 0 ):
			x = x+1
			continue

		flag = True
		for i in range(2,int(math.sqrt(n))):
			if(x%i == 0):
				flag = False
		if(flag == True):
			num_found = num_found +1
			if(num_found == n):
				return x
		else:
			flag = True
		x = x+1

print(count_prime_2(5))


#(a)
def root(f, a, b):
	mid_point = (a+b)/2
	if(f(mid_point) == 0): 
		return mid_point
	if(f(mid_point) < 0):
		root(f,mid_point,b)
	if(f(mid_point) > 0):
		root(f,a,mid_point)
#(b)
def root_2(f, a, b):
	mid_point = (a+b)/2
	if(f(mid_point) == 0): 
		return mid_point
	if(f(mid_point) < 0):
		root(f,mid_point,b)
	if(f(mid_point) > 0):
		root(f,a,mid_point)

#(c)
def chech(f,a,b):
	if(f(a)*f(b)>0):
		print('function evals have same sign')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值