{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true, "pycharm": { "name": "#%% md\n" } }, "source": [ "## a)" ] }, { "cell_type": "code", "execution_count": 5, "outputs": [ { "name": "stdout", "text": [ "4782\n" ], "output_type": "stream" } ], "source": [ "import numpy as np\n", "\n", "φ = (1 + np.sqrt(5))/2\n", "\n", "def digits(k): # number of digits of the kth Fibonacci number\n", " if k < 2:\n", " return 1\n", " return np.ceil(k* np.log10(φ) - np.log10(5)/2) + 1\n", "\n", "def smallestFib(n): # smallest Fibonacci number with n digits\n", " if n < 2:\n", " return 1\n", " return np.ceil((n + np.log10(5) / 2 - 1) / np.log10(φ))\n", "\n", "print(int(smallestFib(1000)))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n", "is_executing": false } } }, { "cell_type": "markdown", "source": [ "## b)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 6, "outputs": [ { "name": "stdout", "text": [ "443839\n" ], "output_type": "stream" } ], "source": [ "sumNumbers = 0\n", "\n", "def powerSum(n): # sum of the fifths powers of the digits\n", " pow = 0\n", " while n > 0:\n", " pow += (n%10)**5\n", " n = n // 10\n", " return pow\n", "\n", "for k in range(2,354294):\n", " if powerSum(k) == k:\n", " sumNumbers += k\n", " \n", "print(sumNumbers)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n", "is_executing": false } } }, { "cell_type": "markdown", "source": [ "## c)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n", "is_executing": false } } }, { "cell_type": "code", "execution_count": 8, "outputs": [ { "name": "stdout", "text": [ "55\n" ], "output_type": "stream" } ], "source": [ "N = 1000000\n", "sieve = np.array(range(N))\n", "for k in sieve[2:int(np.sqrt(N))+1]:\n", " if k != 0: sieve[2*k::k] = 0\n", "\n", "primes = list(map(str,sieve[sieve != 0][1:]))\n", "\n", "def rotations(p):\n", " return [p[k:] + p[:k] for k in range(len(p))]\n", " \n", "circular = ['2']\n", "notcircular = []\n", "\n", "for p in primes: \n", " if p in circular or p in notcircular:\n", " continue \n", " if any(a in p for a in ['0','2','4','5','6','8']):\n", " continue \n", " rots = rotations(p)\n", " if all(a in primes for a in rots):\n", " circular += rots\n", " else:\n", " notcircular += rots\n", "print(len(circular)) \n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n", "is_executing": false } } }, { "cell_type": "code", "execution_count": 14, "outputs": [ { "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mintersect1d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: index 0 is out of bounds for axis 0 with size 0" ], "ename": "IndexError", "evalue": "index 0 is out of bounds for axis 0 with size 0", "output_type": "error" } ], "source": [ "for n in range(12):\n", " for z in range(n):\n", " a,b = list(str(n)), list(str(z))\n", " c = np.intersect1d(a,b)\n", " print(a[a != c[0]])" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n", "is_executing": false } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 0 }