{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Programming with Python (lab classes)\n", "#### Vedran Ĺ ego, [vsego.org](http://vsego.org/)\n", "\n", "## Content:\n", "\n", "1. Loops and conditionals" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Problem 1.** Write a program that loads one integer $n$, a digit $d$, and two nonnegative integers $a$ and $b$ such that $a > b$ (verify that the input satisfies these conditions) and prints the largest integer $x$ such that:\n", "\n", "1. $0 \\le x \\le n$,\n", "\n", "2. $x$ is a perfect square of some integer (i.e., there exists $y \\in \\mathbb{Z}$ such that $x = y^2$),\n", "\n", "3. the last digit of $x$ is $d$,\n", "\n", "4. the remainder of the division of $x$ by $a$ is equal to $b$.\n", "\n", "If there is no such number, the program has to print an appropriate message." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hints:**\n", "\n", "1. Verifying the input means checking that $0 \\le d \\le 9$ and that $a > b \\ge 0$. There is no need to check that any of the loaded numbers are integers, as this will be handled by the int funtion that crashes the program if something other than integer is given (which can be handled, but we'll get to that later on).\n", "\n", "2. Checking that a certain number is a perfect square is not very hard, but there can be far too many candidates. For example, if $n = 1024 = 32^2$, there are only $33$ perfect squares in the set $\\{0, 1, \\dots, n\\}$, but we would have to check all $1025$ nonnegative integers smaller than or equal to $n$. \n", " A better approach is to have a loop go through the values of $y$ (from zero to $\\lfloor \\sqrt{n} \\rfloor$), compute $x = y^2$, and then check the conditions 3 and 4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Some running examples**\n", "\n", "Input \\#1:\n", "\n", " n = 73\n", " d = 9\n", " a = 3\n", " b = 1\n", " The desired number: 49\n", "\n", "Input \\#2:\n", "\n", " n = 200\n", " d = 9\n", " a = 3\n", " b = 1\n", " The desired number: 169\n", "\n", "Input \\#3:\n", "\n", " n = 47\n", " d = 9\n", " a = 3\n", " b = 1\n", " No such number exists!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Problem 2.** Using the idea from the second hint for the previous problem, write a program that loads an integer $n$ and prints how many integers $x$ there are such that\n", "\n", "* $x$ is a perfect square,\n", "\n", "* $1 < x \\le n$,\n", "\n", "* $n$ is divisible by $x$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Problem 3.** Write a program that loads a natural number $n$ and prints in how many ways can $n$ be written as a sum of consecutive natural numbers (including the one-element sum of $n$ alone).\n", "\n", "For example, $15$ can be written as $15$, $7+8$, $4+5+6$, or $1+2+3+4+5$, so the program should print 4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint:** You may use the formula for the sum of the first $n$ natural numbers:\n", "\n", "$$1 + 2 + 3 + \\cdots + n = \\frac{n}{2}(n+1).$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 4.** Write a program that loads two integers and computes their greatest common divisors using the [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) (for example, the one from [MATH10101](http://www.maths.manchester.ac.uk/~mdc/MATH10101.htm#LectureNotes)) and prints it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 5.** Write a program that loads two integers and computes their [lowest common multiplier](http://en.wikipedia.org/wiki/Least_common_multiple)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint:** The simplest approach comes from the formula:\n", "$$\\operatorname{lcm}(m, n) \\operatorname{gcd}(m,n) = |mn|.$$\n", "Using this is generally not advisable, as $mn$ can be too big for most of the programming languages (while $\\operatorname{lcm}(m, n)$ is still small enough), but Python doesn't impose a limit on the size of integers, so you can use this here.\n", "\n", "However, do try to avoid computing anything that is bigger than $\\operatorname{lcm}(m,n)$ (a hint within a hint: commutativity).\n", "\n", "There are, of course, other ways of doing this. Some of these shall be addressed later, when we deal with prime numbers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 6.** Write a program that loads a natural number and checks if it is a [palindrome](http://en.wikipedia.org/wiki/Palindrome)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint:** How would you reverse the order of the digits in a number?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 7.** Write a program that loads a natural number and checks if its digits come in\n", "\n", "1. a descending order (for example: $731$, but not $331$ or $713$)?\n", "\n", "2. a non-increasing order (for example: $731$ or $331$, but not $713$)?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint:** We know how to read a number digit by digit. So, if we save the previous digit when getting the next one, we shall have both and we'll be able to compare them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 8.** Write a program that loads an integer and prints its\n", "\n", "1. second digit from the left,\n", "\n", "2. third digit from the right.\n", "\n", "If the number doesn't have two digits, then its second digit from the left doesn't exist and the program should print an informative message instead of that nonexistent digit. On the other hand, if the number has less than tree digits, its third digit from the right is zero." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 9.** Write a program that loads a natural number $n$ and prints Fibonacci numbers $F_0, F_1, \\dots, F_n$, defined by\n", "$$F_0 = 0, \\quad F_1 = 1, \\quad F_{k+2} = F_{k+1} + F_{k}, \\quad k \\in \\mathbb{N}_0.$$" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }