Skip to content

Commit

Permalink
2023
Browse files Browse the repository at this point in the history
  • Loading branch information
yoninazarathy committed Jun 28, 2024
1 parent e581008 commit 186f575
Show file tree
Hide file tree
Showing 4 changed files with 3,119 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 2023/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: course
course:
julia --project=. src/makeCourse.jl

.PHONY: assessment
assessment:
julia --project=. src/makeAssessments.jl

.PHONY: install
install:
julia --project=. -e 'using Pkg; Pkg.instantiate()'
198 changes: 198 additions & 0 deletions 2023/MakeupQuiz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Makeup Quiz MATH2504 2023"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is an extra (makeup quiz)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function is_prime(n)\n",
" n < 2 && return false\n",
" for i in 2:n\n",
" (n % i == 0) && return false\n",
" end\n",
" return true\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for n in 1:100\n",
" if is_prime(n)\n",
" println(\"$n is prime\")\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1a:** Code above prints no output, why? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1b:** After fixing `is_prime()` to work correctly (determine if input is prime), you write. What is the output of executing `result[7]`?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result = ((n)-> (n,is_prime(n))).(1:10);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1c:** Re-implement your fixed function from 1a with a while loop instead of a for loop."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function my_loud_factorial(n)\n",
" println(\"MF $n\")\n",
" return n <= 1 ? 1 : n*my_loud_factorial(n-1)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2a:** What is the printed output of executing `my_loud_factorial(4)` and what is the return value?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2b:** Assume now that you are given a function called `fast_factorial_under10()`. When you call it, computes the factorial of an integer positive number less than or equal to 10 very efficiently. You wish to modify `my_loud_factorial()` to use `fast_factorial_under10()` for any input `n` less than 10. Write the code for the modified function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3c:** Assume now you wish to add a keyword argument to the original my_loud_factorial (from top of question) operating on some pure function f such that the code computes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\prod_{i=1}^n f(i)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the function signature is `my_loud_factorial(n; by = f)` Note that `f(0)` is assume to be always 1 but that is not necessarily the case for `f(1)`. Implement this extended version of my_loud_factorial."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function bit_pattern(n)\n",
" one::UInt64 = 1\n",
" ret_val::UInt64 = 0\n",
" for i in 0:n-1\n",
" ret_val = ret_val | one << i\n",
" end\n",
" return ret_val\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3a**: What is the output (in hexadecimal) of `bit_pattern(10)`?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3b**: What is the output `bit_pattern(100)`?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3c**: What are the possible characters appearing as output of `bit_pattern(n)` (for any n) in hexadecimal form? E.g. is it all of 0,1,2,...9,A,B,...,F? Or is it just a subset of these characters? Explain why."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.1",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 186f575

Please sign in to comment.