From 836325cb954c6e487444e4e8baccdea6c6960df5 Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Thu, 26 Sep 2019 12:50:55 +0200 Subject: [PATCH] =?UTF-8?q?01:=20P=C5=99=C3=ADklady?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-uvod/ciferny-soucet.py | 12 ++++++++++++ 01-uvod/euklides-modulici.py | 16 ++++++++++++++++ 01-uvod/euklides-odcitaci.py | 13 +++++++++++++ 01-uvod/euklides-trikovy.py | 10 ++++++++++ 01-uvod/prvocisla-test.py | 17 +++++++++++++++++ 01-uvod/prvocisla-vypis.py | 20 ++++++++++++++++++++ 01-uvod/zadavani-cislic.py | 13 +++++++++++++ 7 files changed, 101 insertions(+) create mode 100755 01-uvod/ciferny-soucet.py create mode 100755 01-uvod/euklides-modulici.py create mode 100755 01-uvod/euklides-odcitaci.py create mode 100755 01-uvod/euklides-trikovy.py create mode 100755 01-uvod/prvocisla-test.py create mode 100755 01-uvod/prvocisla-vypis.py create mode 100755 01-uvod/zadavani-cislic.py diff --git a/01-uvod/ciferny-soucet.py b/01-uvod/ciferny-soucet.py new file mode 100755 index 0000000..ebac204 --- /dev/null +++ b/01-uvod/ciferny-soucet.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +# Počítání ciferného součtu + +n = int(input()) +soucet = 0 + +while n > 0: + cislice = n % 10 + soucet += cislice + n = n // 10 + +print(soucet) diff --git a/01-uvod/euklides-modulici.py b/01-uvod/euklides-modulici.py new file mode 100755 index 0000000..eddbf0d --- /dev/null +++ b/01-uvod/euklides-modulici.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# Největší společný dělitel: Euklidův algoritmus s modulem + +x = int(input()) +y = int(input()) + +while x > 0 and y > 0: + if x > y: + x %= y + else: + y %= x + +if x > 0: + print(x) +else: + print(y) diff --git a/01-uvod/euklides-odcitaci.py b/01-uvod/euklides-odcitaci.py new file mode 100755 index 0000000..4bb7cbe --- /dev/null +++ b/01-uvod/euklides-odcitaci.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# Největší společný dělitel: Euklidův algoritmus s odčítáním + +x = int(input()) +y = int(input()) + +while x != y: + if x > y: + x -= y + else: + y -= x + +print(x) diff --git a/01-uvod/euklides-trikovy.py b/01-uvod/euklides-trikovy.py new file mode 100755 index 0000000..17f98f4 --- /dev/null +++ b/01-uvod/euklides-trikovy.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# Největší společný dělitel: Euklidův algoritmus s pár triky navíc + +x = int(input()) +y = int(input()) + +while y > 0: + x, y = y, x%y + +print(x) diff --git a/01-uvod/prvocisla-test.py b/01-uvod/prvocisla-test.py new file mode 100755 index 0000000..77e86aa --- /dev/null +++ b/01-uvod/prvocisla-test.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# Otestuje, zda číslo je prvočíslem + +n = int(input()) + +d = 2 +mam_delitele = False + +while d < n: + if n%d == 0: + print("Číslo", n, "je dělitelné", d) + mam_delitele = True + break + d += 1 + +if not mam_delitele: + print("Číslo", n, "je prvočíslo") diff --git a/01-uvod/prvocisla-vypis.py b/01-uvod/prvocisla-vypis.py new file mode 100755 index 0000000..4ef3f9b --- /dev/null +++ b/01-uvod/prvocisla-vypis.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# Vypíše všechna prvočísla od 1 do n + +n = int(input()) + +x = 2 +while x <= n: + d = 2 + mam_delitele = False + + while d < x: + if x%d == 0: + mam_delitele = True + break + d += 1 + + if not mam_delitele: + print(x) + + x += 1 diff --git a/01-uvod/zadavani-cislic.py b/01-uvod/zadavani-cislic.py new file mode 100755 index 0000000..1b5e3f4 --- /dev/null +++ b/01-uvod/zadavani-cislic.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# Skládáme číslo po číslicích + +print("Zadávej číslice, ukonči -1:") +n = 0 + +while True: + cislice = int(input()) + if cislice < 0: + break # Vyskočíme z cyklu + n = 10*n + cislice + +print(n) -- GitLab