find $\sqrt{\alpha}$ where $\alpha$ is a real positive number
a = 5
a
x0 = -a**2-1
x1 = a**2+1
def average(x0,x1):
return (x0+x1)/2
average(2,7)
low=x0
high = x1
xchecker = (a+100)**0.5
while (xchecker**2-a)**2>0.0000000001:
xnew = average(low,high)
if xnew**2==a:
print("exact number: ",xnew)
break
elif xnew**2>a:
high = xnew
else:
low = xnew
xchecker=xnew
xchecker
def find_sqrt(a,low,high):
xchecker = (a+10)**0.5
while (xchecker**2-a)**2>0.0000000001:
xnew = average(low,high)
if xnew**2==a:
print("exact number: ",xnew)
break
elif xnew**2>a:
high = xnew
else:
low = xnew
xchecker=xnew
return xnew
find_sqrt(30,0,20)
find_sqrt(100,-20,20)
for count in range(0,20):
print(count)
if count>10:
break
m = 500
m=121
num=m-1
while num>1:
if m%num==0:
print('not prime:',num)
break
else:
num-=1
111/37
def Check_prime(x):
num=x-1
ans= "prime"
while num>1:
if x%num==0:
ans="not prime"
break
else:
num-=1
return ans
Check_prime(1611)
def Check_prime(x):
num=x-1
ans= True
while num>1:
if x%num==0:
ans=False
break
else:
num-=1
return ans
Check_prime(13)
m = 500
for count in range(2,m):
if Check_prime(count):
print('PRIME: ',count)
Check_prime(13)
%%time
m = 20000
Prime_list=[]
for count in range(2,m):
if Check_prime(count):
Prime_list.append(count)
print(len(Prime_list))
%%time
Number = 50000
Prime_list=[2]
count=2
while count <Number:
Prime=True
count+=1
for x in range(0,len(Prime_list)):
if count%Prime_list[x]==0:
Prime=False
break
if Prime:
Prime_list.append(count)
print(len(Prime_list))
Primes=[]
UpperInt = 20
Allnums = set(range(UpperInt,1,-1))
Allnums
p=Allnums.pop()
Primes.append(p)
Allnums
Primes
set(range(p*2,UpperInt+1,p))
Allnums.difference_update(set(range(p*2,UpperInt+1,p)))
Allnums
bob=range(3,20,3)
print([x for x in bob])
#Sieve method
UpperInt = int(input('please input your positive integer:'))
Allnums = set(range(UpperInt,1,-1))
%time
Primes=[]
while Allnums:
p=Allnums.pop()
Primes.append(p)
Allnums.difference_update(set(range(p*2,UpperInt+1,p)))
#print(Primes)
print(len(Primes))
%%time
UpperInt=5000000
Allnums = set(range(UpperInt,1,-1))
Primes=[]
while Allnums:
p=Allnums.pop()
Primes.append(p)
Allnums.difference_update(set(range(p*2,UpperInt+1,p)))
#print(Primes)
print(len(Primes))