===== Cvičení 2 ===== ==== 02a ==== [[ https://youtu.be/FHiWnjyLZgw | While cyklus ]] while1.py: print("for") for i in range(10,40,5): print(i) print("while") i=10 while i<40: print(i) i+=5 while2.py: print("for") for i in range(20,-20,-5): print(i) print("while") i=20 while True: print(i) i-=5 if i<=-20: break while-continue.py for i in range(15): if i%2==0: continue if i%3==0: continue print(i) print("while") i=0 while i<15: if i%2==0: i+=1 continue if i%3==0: i+=1 continue print(i) i+=1 problem.py: i=int(input()) while i>1: print(i) if i%2==0: i//=2 # i = i//2 else: i=i*3+1 ==== 02b ==== [[ https://youtu.be/43m8Z5g1LbM | Počítání s reálnými čísly ]] presnost.py: suma=0 step=0.1 for i in range(1,11): suma+=step if suma==i/10: print("Plati", suma, "rovno", i/10) else: print("Neplati", suma, "nerovno", i/10) rovnost.py: x=2.1 epsilon=1e-10 print(abs(x+0.2-2.3)) if abs(x+0.2-2.3)>epsilon: y=10./(x-2.1) print(y) else: print("Nelze delit nulou") odmocnina.py: y=2 x=0 step=1 k=0 for i in range(100): limit=10 while (x+step)**2<=y and (i==0 or limit>=0): x+=step print("Krok",k,x,x**2,y) k+=1 limit-=1 if i>0 and limit<0: break step/=10 print(x) print("Vysledek", x, x**2, y) ==== 02c ==== [[ https://youtu.be/M2so_7F-Et4 | Metoda půlení intervalu ]] puleni.py: c=float(input()) if c>=0: x1=0 if c<=1: x2=1 else: x2=c #zacneme pulit interval k=0 while (x2-x1)>1e-10: x_s = (x1+x2)/2 f_x_s = x_s**2-c f_x1 = x1**2-c if ((f_x1<0) == (f_x_s<0)): x1 = x_s else: x2 = x_s print("Krok",k,x1,x2, x_s**2, c) k+=1 x_r = (x1+x2)/2 print(x_r, x_r**2, c) else: print("Odmocnina ze zaporneho cisle neni realna") newton.py: c=float(input()) if c>=0: x_n=1 x_n_1 = x_n/2+c/(2*x_n) while abs(x_n_1-x_n)>1e-10: x_n = x_n_1 x_n_1 = x_n/2+c/(2*x_n) print(x_n_1, x_n_1**2, c) print(x_n_1, x_n_1**2, c) else: print("Odmocnina ze zaporneho cisle neni realna")