123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- from auditorium import Show
- import math
- from auditorium.show import Context
- from markdown.core import markdown
- show = Show('My Show')
- @show.slide
- def presentation(ctx):
- """
- Welcome to Mathematical Foundation of Algorithms
- Marcelo Fornet
- * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
- * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
- """
- @show.slide
- def score_system(ctx):
- """
- # Score
- - Maximum score: 180 points
- - Maximum points to accumulate: 200
- - Final Grade: $\\frac{min(P, 180)}{180} \cdot 100$
- """
- @show.slide
- def number_representation(ctx: Context):
- """
- # Decimal system
- """
- with ctx.fragment(ctx):
- ctx.markdown("How do we represent number in base 10?")
- with ctx.fragment(ctx):
- ctx.markdown(
- "$$2021 = 2 \cdot 10^3 + 0 \cdot 10^2 + 2 \cdot 10^1 + 1 \cdot 10^0$$")
- def int_to_base(number, base):
- assert base <= 36
- digits = []
- while number > 0:
- dig = number % base
- if base > 10:
- dig = str(dig) if dig < 10 else chr(97 + dig - 10)
- digits.append(dig)
- number //= base
- if len(digits) == 0:
- digits.append(0)
- return list(reversed(digits))
- @number_representation.slide
- def dynamic_base_10(ctx: Context):
- """
- # Decimal system
- """
- number = ctx.text_input("2021")
- try:
- number = int(number)
- assert number >= 0
- digits = int_to_base(number, 10)
- n = len(digits)
- # Uncomment if fix latex issue
- # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
- # in enumerate(reversed(digits)) if d > 0)
- # output = f'$${output}$$'
- output = ' + '.join(f"{d} * {10**(n-p-1)}" for (p, d)
- in enumerate(digits) if d > 0)
- if output == '':
- output = '0'
- except Exception as e:
- output = f"{number} is not a valid number"
- ctx.markdown(output)
- @number_representation.slide
- def number_representation_poll_1(ctx: Context):
- """
- # Decimal system
- """
- ctx.markdown("Can we represent all integer numbers using decimal system?")
- with ctx.fragment(ctx):
- with ctx.columns(2) as cl:
- with ctx.fragment('highlight-green'):
- ctx.markdown('Yes')
- cl.tab()
- ctx.markdown('No')
- @number_representation.slide
- def number_representation_poll_2(ctx: Context):
- """
- # Decimal system
- """
- ctx.markdown("Can we represent every number in a unique way?")
- with ctx.fragment(ctx):
- with ctx.columns(2) as cl:
- with ctx.fragment('highlight-green'):
- ctx.markdown('Yes')
- cl.tab()
- ctx.markdown('No')
- @ show.slide
- def number_to_list_of_digits(ctx):
- """
- # Practice exercise
- 1. Convert a python integer to its decimal representation.
- https://ideone.com/Ekmuzk
- """
- @ show.slide
- def list_of_digits_to_number(ctx):
- """
- # Practice exercise
- 2. Convert a number in decimal representation to python integer.
- https://ideone.com/CkX1rd
- """
- @ show.slide
- def what_is_base2(ctx):
- """
- # Base 2
- """
- number = ctx.text_input("5")
- try:
- number = int(number)
- assert number >= 0
- digits = int_to_base(number, 2)
- n = len(digits)
- # Uncomment if fix latex issue
- # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
- # in enumerate(reversed(digits)) if d > 0)
- # output = f'$${output}$$'
- output_2 = ' + '.join(f"{2**(n-p-1)}" for (p, d)
- in enumerate(digits) if d > 0)
- if output_2 == '':
- output_2 = '0'
- output_1 = bin(number)
- except Exception as e:
- output_1 = f"{number} is not a valid number"
- output_2 = ""
- ctx.markdown(output_1)
- ctx.markdown(output_2)
- @show.slide
- def what_is_base_b(ctx):
- """
- # Base b
- $$v = \sum_i d_i \cdot b^i$$
- """
- @what_is_base_b.slide
- def what_is_base_b_2(ctx):
- """
- # Base b
- """
- number = 3**10
- base = ctx.text_input("10")
- try:
- base = int(base)
- assert 2 <= base <= 36
- digits = int_to_base(number, base)
- output = ''.join(str(x) for x in digits)
- except Exception as e:
- print(e)
- output = str(e)
- ctx.markdown(str(number))
- ctx.markdown(output)
- @ show.slide
- def fibonacci_base(ctx: Context):
- """
- # Fibonacci base
- """
- @fibonacci_base.slide
- def fibonacci_sequence(ctx: Context):
- """
- # Fibonacci sequence
- """
- with ctx.fragment(ctx):
- ctx.markdown("$$F_1 = 1$$")
- ctx.markdown("$$F_2 = 2$$")
- ctx.markdown("$$F_n = F_{n-1} + F_{n-2}$$")
- L = [1, 2]
- while len(L) < 10:
- L.append(L[-1] + L[-2])
- with ctx.fragment(ctx):
- ctx.markdown(', '.join(map(str, L)))
- @ fibonacci_base.slide
- def fibonacci_base_description(ctx: Context):
- """
- # Fibonacci base
- $$v = \sum_{1 <= i} d_i \cdot F_i $$
- $$d_i \in (0, 1)$$
- """
- @ fibonacci_base.slide
- def fibonacci_base_example(ctx: Context):
- """
- # Fibonacci base
- """
- value = str(ctx.text_input("1100101"))
- n = len(value)
- L = [1, 2]
- while len(L) < n:
- L.append(L[-1] + L[-2])
- output_number = 0
- output_sum = ""
- R = []
- for i, d in enumerate(value):
- if d == '1':
- output_number += L[n - i - 1]
- R.append(L[n - i - 1])
- output_sum = ' + '.join(map(str, R))
- ctx.markdown(str(output_number))
- ctx.markdown(output_sum)
- @fibonacci_base.slide
- def fibonacci_base_poll_1(ctx: Context):
- """
- # Fibonacci base
- """
- ctx.markdown(
- "Can we represent all integer numbers using fibonacci base?")
- with ctx.fragment(ctx):
- with ctx.columns(2) as cl:
- with ctx.fragment('highlight-green'):
- ctx.markdown('Yes')
- cl.tab()
- ctx.markdown('No')
- @fibonacci_base.slide
- def fibonacci_base_poll_2(ctx: Context):
- """
- # Fibonacci base
- """
- ctx.markdown("Can we represent every number in a unique way?")
- with ctx.fragment(ctx):
- with ctx.columns(2) as cl:
- ctx.markdown('Yes')
- cl.tab()
- with ctx.fragment('highlight-red'):
- ctx.markdown('No')
- # Make a break
- @ show.slide
- def computer_representation(ctx: Context):
- """
- ### Representation in the computer
- """
- with ctx.fragment(ctx):
- ctx.markdown("Binary")
- with ctx.fragment(ctx):
- ctx.markdown("Bytes")
- with ctx.fragment(ctx):
- ctx.markdown("C++ Demo")
- @ show.slide
- def base_16_64_application(ctx):
- """
- ### Application of higher base
- * Hexadecimal
- * Base64
- """
- with ctx.fragment(ctx):
- ctx.markdown("Demo")
- @ show.slide
- def decimal_number_in_base_10(ctx):
- """
- Decimal number
- $$\sum_{-\infty \lt i \lt \infty} d_i \cdot 10^{i}$$
- """
- @ show.slide
- def decimal_number_in_computer(ctx):
- """
- Single precision floating point format
- """
- with ctx.fragment(ctx):
- ctx.markdown("Wikipedia")
- with ctx.fragment(ctx):
- ctx.markdown("C++ Demo")
- # Some precision issues with catastrophic cancelation (substraction)
- # Compare numbers directly with each other
- # a / b == c (not always the same)
- # Case where the two numbers have same representation
- # but a == b (is false) This is with nan
- # and different representation but a == b (is true) this is 0 and -0
- @ show.slide
- def homework(ctx):
- """
- Homework
- """
- @homework.slide
- def task_1(ctx):
- """
- `*`
- Code that convert from any base to any other base
- Hint: Try converting to python number first
- """
- @homework.slide
- def task_2(ctx):
- """
- `*`
- Code that convert from base 2 to base 16 without converting to python number
- """
- @homework.slide
- def task_3(ctx):
- """
- `***`
- On fibonacci base, if we forbid consecutive 1, prove that every number is represented uniquely
- [Share LaTeX example]
- """
- # > Proof that fibonacci base without non-consecutive 1 is unique
- # > Hint 1: Try using induction
- # > Hint 2: Assume all numbers less than F_n
- @homework.slide
- def task_4(ctx):
- """
- `**`
- Make a program that converts from a number to fibonacci base
- """
- @homework.slide
- def task_5(ctx):
- """
- `***`
- There are $n$ books. Each of them can be painted in one of $k$ different colors.
- 1. How many different ways can the books be painted.
- 2. Make a program that prints all different possible ways.
- """
- @ show.slide
- def bye(ctx):
- """
- Mathematical Foundation of Algorithms
- Marcelo Fornet
- * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
- * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
- """
|