Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "6bb6fd3d-f24e-4ce2-a472-1ca4c8f02306", "metadata": {}, "source": [ "### Project Euler Common Functions" ] }, { "cell_type": "code", "execution_count": null, "id": "48addcc5-3908-4fea-841b-9edeadf7faf9", "metadata": {}, "outputs": [], "source": [ "def prod(x_vector):\n", " # x_1 * x_2 * ... * x_n , for n = len(x_vector)\n", " # emulate the behavior of math.prod()\n", " result = 1\n", " for i in x_vector:\n", " result *= i\n", " return result\n", "\n", "def int_log(n,base):\n", " # floor( log_base(n) )\n", " result = 0\n", " while int(n) >= base:\n", " n /= base\n", " result += 1\n", " return result\n", "\n", "def gcd(x, y):\n", " # greatest common divisor\n", " while(y):\n", " x, y = y, x % y\n", " return x\n", "\n", "def lcm(x, y):\n", " # lowest common multiplier\n", " return (x*y)//gcd(x,y) \n", "\n", "def factorial(n):\n", " # n!\n", " return n*factorial(n-1) if n > 1 else 1\n", "\n", "def nCr(n,r):\n", " # nCr = n! / (n-r)! / r!\n", " return factorial(n)//factorial(n-r)//factorial(r)\n", "\n", "def sum_of_square_first_n(n):\n", " # 1^2 + 2^2 + 3^2 + ... + n^2\n", " return (n)*(n+1)*(2*n+1)//6\n", "\n", "def sum_of_first_n(n):\n", " # 1 + 2 + 3 + ... + n\n", " return (1+n)*n//2\n", "\n", "def fibonacci(first, second, n_term = False, max_value = False):\n", " # fib = { 1, 1, 2, 3, 5, 8 ... } truncated at n-th term OR fib_n exceeded max_value\n", " result = [first, second]\n", " if not n_term and not max_value:\n", " print(\"At least one of 'n_term','max_value' argument is needed.\")\n", " return\n", " while True:\n", " if n_term and len(result) >= n_term:\n", " break\n", " if max_value and result[-1] > max_value:\n", " result.pop()\n", " break\n", " result.append(result[-1]+result[-2])\n", " return result \n", "\n", "def is_prime(n):\n", " # check if n is prime\n", " for i in range(2,int(n**0.5)+1):\n", " if (n%i) == 0:\n", " return False\n", " return True\n", "\n", "def prime_numbers(n, max_len = False):\n", " # prime numbers under n OR 1st to (max_len)-th prime\n", " result = []\n", " if max_len:\n", " n = max_len**2//5000 + max_len*20\n", " sieve = [True] * (n+1)\n", " for p in range(2, n+1):\n", " if (sieve[p]):\n", " result.append(p)\n", " if max_len and len(result) >= max_len:\n", " return result\n", " for i in range(p, n+1, p):\n", " sieve[i] = False\n", " return result\n", "\n", "def prime_factors(n):\n", " # all prime factors of n with repetition\n", " i = 2\n", " factors = []\n", " while i * i <= n:\n", " if n % i:\n", " i += 1\n", " else:\n", " n //= i\n", " factors.append(i)\n", " if n > 1:\n", " factors.append(n)\n", " return factors\n", "\n", "def divisors(n):\n", " # divisors of n\n", " divs = []\n", " for i in range(1,int((n)**(1/2))+1):\n", " if n%i == 0:\n", " divs.append(i)\n", " divs.append(n//i)\n", " return list(set(divs))\n", "\n", "def combinations(elements):\n", " # all combinations of elements \n", " # elements = [1,2] --> [[],[1],[2],[1,2]]\n", " if len(elements) == 0: \n", " return [[]]\n", " comb = []\n", " for i in combinations(elements[1:]):\n", " comb += [i, i+[elements[0]]]\n", " return comb\n", "\n", "def split_combinations(n):\n", " # combinations of slicing n into two non-negative integer\n", " # n = 4 --> [[0,4],[1,3],[2,2]]\n", " return [[i,n-i] for i in range(n) if 2*i<=n ] if n > 0 else [[0,0]]\n", "\n", "def rotate_matrix(m):\n", " # rotate matrix by 90 degree\n", " return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]\n", "\n", "def list_count(x_vector):\n", " # count the appearance of the elements in x_vector\n", " # emulate the behavior of collections.Counter()\n", " result = {}\n", " for x in x_vector:\n", " result[x] = result.get(x,0) + 1\n", " return result\n", "\n", "def distinct_digit_generator(candidates,selected=None):\n", " # a generator of numbers formed by using distinct given digits\n", " # candidates = [3,2,1] --> (321,312,231...) \n", " if selected is None:\n", " selected = []\n", " if len(candidates) == 0:\n", " yield int(''.join(str(i) for i in selected))\n", " for i in range(len(candidates)):\n", " temp_c = candidates.copy()\n", " temp_s = selected+[temp_c.pop(i)]\n", " yield from distinct_digit_generator(temp_c,temp_s)\n", " \n", "def recurring_len(n):\n", " # return the number of recurring digits of 1/n\n", " if int('9'*(n-1))%n != 0:\n", " return 0\n", " else:\n", " recurring_nums = str(int('9'*(n-1))//n)\n", " return (n-1)//recurring_nums.count(recurring_nums[:len(str(n))+1])" ] } ], "metadata": { "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
MySQL Users and Privileges
Next Notebook:
Project Euler Q1
Loading