123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true,
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "_Aufgabe 1_\n",
- "\n",
- "Schreiben Sie eine Funktion *prim_sum_up_to*, die die Summe aller Primzahlen von 2 bis *n* berechnet, wobei *n* als\n",
- "Parameter übergeben wird."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "outputs": [],
- "source": [
- "def prim_sum_up_to(end):\n",
- " prims = []"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "pycharm": {
- "name": "#%%\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "outputs": [],
- "source": [
- "def prim_sum_up_to(n):\n",
- " result = 0\n",
- " for m in range(2, n+1):\n",
- " if is_prim(m):\n",
- " result += m\n",
- " return result\n",
- "\n",
- "def is_prim(n):\n",
- " divisor = 2\n",
- " while divisor * divisor <= n:\n",
- " if n % divisor == 0:\n",
- " return False\n",
- " divisor += 1\n",
- " return True\n",
- "\n",
- "print(prim_sum_up_to(100))\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "_Aufgabe 2_\n",
- "\n",
- "Sobald Sie die Funktion mit größeren *n* aufrufen, steigt die Laufzeit des Programms an. Um zu verhindern,\n",
- "dass der Anwender fälschlicherweise annimmt, dass das Programm abgestürzt ist, soll jede Sekunde ein\n",
- "Stern ausgegeben werden, bis das Ergebnis vorliegt.\n",
- "\n",
- "Verwenden Sie hierfür das in der Standardbibliothek enthaltene Modul <code>datetime</code> und\n",
- "importieren Sie daraus die Objekte <code>datetime</code> und <code>timedelta</code>.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "pycharm": {
- "name": "#%%\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "outputs": [],
- "source": [
- "from datetime import datetime, timedelta\n",
- "\n",
- "last_run = datetime.now()\n",
- "intervall = timedelta(seconds=1)\n",
- "\n",
- "def do_only_every_second():\n",
- " global last_run\n",
- " if last_run + intervall > datetime.now():\n",
- " return\n",
- " print(\"*\", end='')\n",
- " last_run = datetime.now()\n",
- "\n",
- "\n",
- "def is_prim(n):\n",
- " divisor = 2\n",
- " while divisor * divisor <= n:\n",
- " if n % divisor == 0:\n",
- " return False\n",
- " divisor += 1\n",
- " do_only_every_second()\n",
- " return True\n",
- "\n",
- "print(f'\\n{prim_sum_up_to(1000000)}')\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "Vielleicht können Sie das Prinzip in einem zukünftigen Praktikum nutzen, wenn nicht bei jedem Schleifendurchlauf\n",
- "etwas passieren soll...."
- ]
- }
- ],
- "metadata": {
- "celltoolbar": "Slideshow",
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.9.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|