Table of Contents

Cvičení 3

03a

Zavedení funkce

newton.py:

def next_item(x, moc=2):
    return x - x/moc + c/(moc*(x**(moc-1)))

c=float(input())
if c>=0:
    x_n=1
    x_n_1 = next_item(x_n,moc=4)
    while abs(x_n_1-x_n)>1e-10:
        x_n = x_n_1
        x_n_1 = next_item(x_n, moc=4)
        print(x_n_1, x_n_1**2, c)
    print(x_n_1, x_n_1**4, c)
else:
    print("Odmocnina ze zaporneho cisle neni realna")
funkce.py:
def f(x):
    r=0
    if x<0:
        r = x**2
    elif x<1:
        r = 0
    else:
        r = (x-1)**2
    return r

print(f(-0.5), f(0.5), f(1.5))
a=f(-0.5)+f(0.5)+f(1.5)
sachovnice.py
print('\n-----------------')
for i in range(8):
    print('|',end='')
    for j in range(8):
        if (i+j)%2==0:
            print('*',end='|')
        else:
            print(' ',end='|')
    print('\n-----------------')

03b

Dělitelnost a prvočísla

prvocilo.py:

def prvocislo(a):
    r=True
    print("Overuji cislo",a)
    for d in range(2,round(a**0.5)+1):
        if a%d==0:
            r = False
            break
    return r

for i in range(2,50):
    if prvocislo(i):
            print(i, end=' ')
sexy-prvocisla.py:
def prvocislo(a):
    r=True
    print("Overuji cislo",a)
    for d in range(2,round(a**0.5)+1):
        if a%d==0:
            r = False
            break
    return r

for i in range(2,30):
    if prvocislo(i) and prvocislo(i+6):
            print(i,i+6, sep='<->')
super-dokonala-cisla.py:
def soucet_delitelu(a):
    acc=0
    for d in range(1,a+1):
        if a%d==0:
            acc+=d
    return acc

for i in range(1,10001):
    if soucet_delitelu(soucet_delitelu(i))==2*i:
        print("Super dokonale cislo",i)

03c

Největší společný dělitel

gcd-odcitani.py:

def gcd(a,b):
    if a<b:
        a,b=b,a
    while (a>b):
        a = a-b
        if a<b:
            a,b=b,a
    return a

print(gcd(184,253))
gcd-modulo.py:
def gcd(a,b):
    if a<b:
        a,b=b,a
    zb = a%b
    while zb!=0:
        a = b
        b = zb
        zb = a%b
    return b

print(gcd(253,18400))
gcd-porovnani.py:
def gcd(a,b):
    if a<b:
        a,b=b,a
    while (a>b):
        print("odcitani a",a,"b",b)
        a = a-b
        if a<b:
            a,b=b,a
    return a

def gcd_modulo(a,b):
    if a<b:
        a,b=b,a
    zb = a%b
    while zb!=0:
        print("modulo a",a,"b",b)
        a = b
        b = zb
        zb = a%b
    return b


print(gcd(253,18400))
print(gcd_modulo(253,18400))

courses/b3b33alp/cviceni/kratka_videa/c03.txt · Last modified: 2021/09/14 12:19 by stepan