BuK/IIm20/Vorlesung 12 Seidl

Aus ProgrammingWiki

Wiki-Quelltext dieser Seite.

<language>Python</language>
=== S. 7 ===
<code id="60cdfa9473510">
def in_naturals():
    index = 1
    while True:
        yield index
        index += 1

# n=exponent; a=basis
def pot_loop2(n, a, loops):
    result = 1
    for i in in_naturals():
        if i == loops:
            break
        
        result = result * a
    return result
</code>
<run id="60cdfa9473610">
print(pot_loop2(10, 2, 11))
</run>

=== S. 12 ===
<code id="60cdfa947356f">
def nat():
    index = 0
    while True:
        yield index
        index += 1

def f(x, y):
    for z in range(0, y + 1):
        if ((z + 1) ** 2) > x:
            return z
</code>
<run id="60cdfa9473665">
print(f(19, 19))
</run>

=== S. 19 ===

<code id="60cdfa94735be">
def muehf(n):
    m = 0
    while ((m ** 2) - n) != 0:
        m += 1
    n = m
    print(n)
</code>
<run id="60cdfa94736c5">
muehf(4)
</run>

=== S. 24 ===
Ergebnis "None" entspricht "⊥"
<code id="60cdfd169b71d">
def muehf(x):
    for y in range(100):
        if x % 2 + y == 0:
            return y
</code>
<run id="60cdfd169bc67">
print(muehf(10))
print(muehf(5))
</run>

=== S. 26 ===
<code id="60cdfd169b7af">
def muehf_alt(x):
    for y in range(100):
        if x % 2 - y == 0:
            return y
</code>
<run id="60cdfd169bcdb">
print(muehf_alt(10))
print(muehf_alt(5))
</run>

Als while-Schleifen Implementierung:
<code id="60cdfd169b8fe">
def muehff(x):
    y = 0
    while monus(x % 2, y) != 0:
        y += 1
    print(y)

def monus(a, b):
    if a - b < 0:
        return 0
    else:
        return a - b
</code>
<run id="60cdfd169bd47">
muehff(10)
muehff(5)
</run>

=== S. 28 ===
<code id="60cdfd169b988">
def muehf_alt_z(x):
    for y in range(100):
        if x ** 2 - y == 0:
            return y
</code>
<run id="60cdfd169bdb0">
print(muehf_alt_z(10))
print(muehf_alt_z(5))
</run>

=== S. 30 ===
<code id="60cdfd169ba23">
def muehf_alt_d(x):
    for y in range(100):
        print(y ** 2 - x)
        if y ** 2 - x == 0:
            return y
</code>
<run id="60cdfd169be21">
print(muehf_alt_d(10))
print(muehf_alt_d(5))
</run>

=== S. 32 ===
<code id="60cdfd169bab0">
def prim_get(lower, upper):
    for num in range(lower, upper + 1):
       # all prime numbers are greater than 1
       if num > 1:
           for i in range(2, num):
               if (num % i) == 0:
                   break
           else:
                return num

def is_prime(num):
    if num > 1:
     
        # Iterate from 2 to n / 2
        for i in range(2, int(num/2)+1):
     
            # If num is divisible by any number between
            # 2 and n / 2, it is not prime
            if (num % i) == 0:
                return False
                break
        else:
            return True
     
    else:
        return True
                
def prim(x):
    for y in range(100):
        if is_prime(y) is True and y > x:
            print(0, y)
        else:
            print(1, y)
        return
</code>
<run id="60cdfd169be96">
prim(10)
prim(5)
</run>

=== S. 34 ===
<code id="60cdfd169bb38">
def g(m):
    for n in range(1, 100):
        if m != 0 and m % n == 0:
            return n/m
</code>
<run id="60cdfd169bf02">
print(g(10))
print(g(5)) 
</run>