main.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. from auditorium import Show
  2. import math
  3. from auditorium.show import Context
  4. from markdown.core import markdown
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. show = Show('My Show')
  8. @show.slide
  9. def presentation(ctx):
  10. """
  11. Welcome to Mathematical Foundation of Algorithms
  12. Marcelo Fornet
  13. * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
  14. * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
  15. """
  16. @show.slide
  17. def score_system(ctx):
  18. """
  19. # Score
  20. - Maximum score: 180 points
  21. - Maximum points to accumulate: 200
  22. - Final Grade: $\\frac{min(P, 180)}{180} \cdot 100$
  23. """
  24. @show.slide
  25. def number_representation(ctx: Context):
  26. """
  27. # Decimal system
  28. """
  29. with ctx.fragment(ctx):
  30. ctx.markdown("How do we represent number in base 10?")
  31. with ctx.fragment(ctx):
  32. ctx.markdown(
  33. "$$2021 = 2 \cdot 10^3 + 0 \cdot 10^2 + 2 \cdot 10^1 + 1 \cdot 10^0$$")
  34. def int_to_base(number, base):
  35. assert base <= 36
  36. digits = []
  37. while number > 0:
  38. dig = number % base
  39. if base > 10:
  40. dig = str(dig) if dig < 10 else chr(97 + dig - 10)
  41. digits.append(dig)
  42. number //= base
  43. if len(digits) == 0:
  44. digits.append(0)
  45. return list(reversed(digits))
  46. @number_representation.slide
  47. def dynamic_base_10(ctx: Context):
  48. """
  49. # Decimal system
  50. """
  51. number = ctx.text_input("2021")
  52. try:
  53. number = int(number)
  54. assert number >= 0
  55. digits = int_to_base(number, 10)
  56. n = len(digits)
  57. # Uncomment if fix latex issue
  58. # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
  59. # in enumerate(reversed(digits)) if d > 0)
  60. # output = f'$${output}$$'
  61. output = ' + '.join(f"{d} * {10**(n-p-1)}" for (p, d)
  62. in enumerate(digits) if d > 0)
  63. if output == '':
  64. output = '0'
  65. except Exception as e:
  66. output = f"{number} is not a valid number"
  67. ctx.markdown(output)
  68. @number_representation.slide
  69. def number_representation_poll_1(ctx: Context):
  70. """
  71. # Decimal system
  72. """
  73. ctx.markdown("Can we represent all integer numbers using decimal system?")
  74. with ctx.fragment(ctx):
  75. with ctx.columns(2) as cl:
  76. with ctx.fragment('highlight-green'):
  77. ctx.markdown('Yes')
  78. cl.tab()
  79. ctx.markdown('No')
  80. @number_representation.slide
  81. def number_representation_poll_2(ctx: Context):
  82. """
  83. # Decimal system
  84. """
  85. ctx.markdown("Can we represent every number in a unique way?")
  86. with ctx.fragment(ctx):
  87. with ctx.columns(2) as cl:
  88. with ctx.fragment('highlight-green'):
  89. ctx.markdown('Yes')
  90. cl.tab()
  91. ctx.markdown('No')
  92. @ show.slide
  93. def number_to_list_of_digits(ctx):
  94. """
  95. # Practice exercise
  96. 1. Convert a python integer to its decimal representation.
  97. https://ideone.com/Ekmuzk
  98. """
  99. @ show.slide
  100. def list_of_digits_to_number(ctx):
  101. """
  102. # Practice exercise
  103. 2. Convert a number in decimal representation to python integer.
  104. https://ideone.com/CkX1rd
  105. """
  106. @ show.slide
  107. def what_is_base2(ctx):
  108. """
  109. # Base 2
  110. """
  111. number = ctx.text_input("5")
  112. try:
  113. number = int(number)
  114. assert number >= 0
  115. digits = int_to_base(number, 2)
  116. n = len(digits)
  117. # Uncomment if fix latex issue
  118. # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
  119. # in enumerate(reversed(digits)) if d > 0)
  120. # output = f'$${output}$$'
  121. output_2 = ' + '.join(f"{2**(n-p-1)}" for (p, d)
  122. in enumerate(digits) if d > 0)
  123. if output_2 == '':
  124. output_2 = '0'
  125. output_1 = bin(number)
  126. except Exception as e:
  127. output_1 = f"{number} is not a valid number"
  128. output_2 = ""
  129. ctx.markdown(output_1)
  130. ctx.markdown(output_2)
  131. @show.slide
  132. def what_is_base_b(ctx):
  133. """
  134. # Base b
  135. $$v = \sum_i d_i \cdot b^i$$
  136. """
  137. @what_is_base_b.slide
  138. def what_is_base_b_2(ctx):
  139. """
  140. # Base b
  141. """
  142. number = 3**10
  143. base = ctx.text_input("10")
  144. try:
  145. base = int(base)
  146. assert 2 <= base <= 36
  147. digits = int_to_base(number, base)
  148. output = ''.join(str(x) for x in digits)
  149. except Exception as e:
  150. print(e)
  151. output = str(e)
  152. ctx.markdown(str(number))
  153. ctx.markdown(output)
  154. @ show.slide
  155. def fibonacci_base(ctx: Context):
  156. """
  157. # Fibonacci base
  158. """
  159. @fibonacci_base.slide
  160. def fibonacci_sequence(ctx: Context):
  161. """
  162. # Fibonacci sequence
  163. """
  164. with ctx.fragment(ctx):
  165. ctx.markdown("$$F_1 = 1$$")
  166. ctx.markdown("$$F_2 = 2$$")
  167. ctx.markdown("$$F_n = F_{n-1} + F_{n-2}$$")
  168. L = [1, 2]
  169. while len(L) < 10:
  170. L.append(L[-1] + L[-2])
  171. with ctx.fragment(ctx):
  172. ctx.markdown(', '.join(map(str, L)))
  173. @ fibonacci_base.slide
  174. def fibonacci_base_description(ctx: Context):
  175. """
  176. # Fibonacci base
  177. $$v = \sum_{1 <= i} d_i \cdot F_i $$
  178. $$d_i \in (0, 1)$$
  179. """
  180. @ fibonacci_base.slide
  181. def fibonacci_base_example(ctx: Context):
  182. """
  183. # Fibonacci base
  184. """
  185. value = str(ctx.text_input("1100101"))
  186. n = len(value)
  187. L = [1, 2]
  188. while len(L) < n:
  189. L.append(L[-1] + L[-2])
  190. output_number = 0
  191. output_sum = ""
  192. R = []
  193. for i, d in enumerate(value):
  194. if d == '1':
  195. output_number += L[n - i - 1]
  196. R.append(L[n - i - 1])
  197. output_sum = ' + '.join(map(str, R))
  198. ctx.markdown(str(output_number))
  199. ctx.markdown(output_sum)
  200. @fibonacci_base.slide
  201. def fibonacci_base_poll_1(ctx: Context):
  202. """
  203. # Fibonacci base
  204. """
  205. ctx.markdown(
  206. "Can we represent all integer numbers using fibonacci base?")
  207. with ctx.fragment(ctx):
  208. with ctx.columns(2) as cl:
  209. with ctx.fragment('highlight-green'):
  210. ctx.markdown('Yes')
  211. cl.tab()
  212. ctx.markdown('No')
  213. @fibonacci_base.slide
  214. def fibonacci_base_poll_2(ctx: Context):
  215. """
  216. # Fibonacci base
  217. """
  218. ctx.markdown("Can we represent every number in a unique way?")
  219. with ctx.fragment(ctx):
  220. with ctx.columns(2) as cl:
  221. ctx.markdown('Yes')
  222. cl.tab()
  223. with ctx.fragment('highlight-red'):
  224. ctx.markdown('No')
  225. # Make a break
  226. @ show.slide
  227. def computer_representation(ctx: Context):
  228. """
  229. ### Representation in the computer
  230. """
  231. with ctx.fragment(ctx):
  232. ctx.markdown("Binary")
  233. with ctx.fragment(ctx):
  234. ctx.markdown("Bytes")
  235. with ctx.fragment(ctx):
  236. ctx.markdown("C++ Demo")
  237. @ show.slide
  238. def base_16_64_application(ctx):
  239. """
  240. ### Application of higher base
  241. * Hexadecimal
  242. * Base64
  243. """
  244. with ctx.fragment(ctx):
  245. ctx.markdown("Demo")
  246. @ show.slide
  247. def decimal_number_in_base_10(ctx):
  248. """
  249. Decimal number
  250. $$\sum_{-\infty \lt i \lt \infty} d_i \cdot 10^{i}$$
  251. """
  252. @ show.slide
  253. def decimal_number_in_computer(ctx):
  254. """
  255. Single precision floating point format
  256. """
  257. with ctx.fragment(ctx):
  258. ctx.markdown("Wikipedia")
  259. with ctx.fragment(ctx):
  260. ctx.markdown("C++ Demo")
  261. # Some precision issues with catastrophic cancelation (substraction)
  262. # Compare numbers directly with each other
  263. # a / b == c (not always the same)
  264. # Case where the two numbers have same representation
  265. # but a == b (is false) This is with nan
  266. # and different representation but a == b (is true) this is 0 and -0
  267. @ show.slide
  268. def homework(ctx):
  269. """
  270. Homework
  271. """
  272. @homework.slide
  273. def task_1(ctx):
  274. """
  275. `*`
  276. Code that convert from any base to any other base
  277. Hint: Try converting to python number first
  278. """
  279. @homework.slide
  280. def task_2(ctx):
  281. """
  282. `*`
  283. Code that convert from base 2 to base 16 without converting to python number
  284. """
  285. @homework.slide
  286. def task_3(ctx):
  287. """
  288. `***`
  289. On fibonacci base, if we forbid consecutive 1, prove that every number is represented uniquely
  290. [Share LaTeX example]
  291. """
  292. # > Proof that fibonacci base without non-consecutive 1 is unique
  293. # > Hint 1: Try using induction
  294. # > Hint 2: Assume all numbers less than F_n
  295. @homework.slide
  296. def task_4(ctx):
  297. """
  298. `**`
  299. Make a program that converts from a number to fibonacci base
  300. """
  301. @homework.slide
  302. def task_5(ctx):
  303. """
  304. `***`
  305. There are $n$ books. Each of them can be painted in one of $k$ different colors.
  306. 1. How many different ways can the books be painted.
  307. 2. Make a program that prints all different possible ways.
  308. """
  309. @ show.slide
  310. def bye(ctx):
  311. """
  312. Mathematical Foundation of Algorithms
  313. Marcelo Fornet
  314. * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
  315. * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
  316. """