Midterm Exam Practice 2
You can find the git repository for this midterm at https://github.com/stat-assignments/2025-850-practice-midterm.
You should be able to clone it to your machine, but you should not be able to push your results back to the repository.
Prime Factorization
In this problem, you will work towards building code that will decompose a number into its prime factors.
We’ll start by installing a package that helps work with prime numbers… primes in R, SymPy in Python.
Skill: Install Packages
R
Write code to install the primes package in R. Run the code.
Python
Write code to install the SymPy package in Python. If you choose to install the package via the terminal, place your code in the unlabeled chunk; otherwise, place your code in the python chunk.
Thinking Criticially
You must complete this question regardless of the language used above.
Add
#| eval: falseto the first line of the code chunk (below the r or python chunk header) to stop that code from running every time the document is executed.List at least one additional way to keep the code in the quarto document but prevent it from running.
Skill: Loading packages
Load the packages you just installed in R and Python using the chunks below.
R
Python
Hint: SymPy should be typed as sympy when loading the package. You will only need the primerange function in SymPy.
Skill: Using prewritten functions
R
Use the generate_primes(min, max) function to generate all the primes between 1 and 47, inclusive. Store these numbers in a variable called myprimes.
Python
Use the primerange(a, b) function in SymPy to get all primes between 1 and 47, inclusive. Store these numbers in a variable called myprimes.
You may have to convert the output of primerange to a list by wrapping the function call in list(). It may be convenient to then convert your list into a numpy array using np.array(list(...)). If you choose to do this, you will also need to import the numpy library.
Comparison
Do the function parameters you used in R and python differ? Why?
Skill: Indexing and Subsets
Using your primes variables (in both R and python), determine which primes are a factor of a second variable, x. You may want to test your code on values like x=18, x=25, x = 34, x=43, Your code should output only the prime factors of x.
Remember that modular division gives you the remainder when dividing a number by another number. x %% 3 (R) or x % 3 (Python) will give you the remainder when x is divided by 3.
If x is an integer and the divisor is an integer, the result of modular division will also be an integer.
R
Python
Skill: Writing Functions
In the previous section, you determined which primes were factors of a given number. Of course, it is possible to have a number which has multiple occurrences of the same prime factor.
In R and python, write a function, prime_n(x, prime), which will determine how many times a single, pre-specified prime number prime can be evenly divided into a number x. You may want to calculate an upper bound for this possibility to help you in your search. Your function should take parameters x and prime and return the integer number of times prime occurs in the prime factorization of x.
The operation \(\log_a(x) = b\) is defined the number \(b\) such that \(a^b = x\) for fixed \(a\) and \(x\). You can use this information to determine the maximum number of times a factor could be repeated when calculating the prime factorization of a number.
You will need to load the math library in Python to access the math.log(a, Base) function. In R, the log(x, base) function is built in.
It may also be useful to know that the floor() function (R) and math.floor() function (Python) will round down to the integer below a value.
R
A number of you handled this using a while loop – which is a great way to do it, and doesn’t require the log hint I gave you.
Python
Skill: Data Frames, Loops
Putting the pieces together, use your function and your list of prime factors to determine the prime factorization of a given number. Store your factorization as a data frame with two columns: factor and power, where factor contains the factor and power contains the number of times that factor appears in the prime factorization.
For instance, your result for x=18 should be a data frame that looks like this:
| factor | power |
|---|---|
| 2 | 1 |
| 3 | 2 |
Hint:
- R:
rbind(df, row)will addrowto the bottom ofdfifdfis a data frame - Python:
pandas.concat([df, row])will addrowto the bottom ofdfifdfis a data frame.
Planning
Using the provided scratch paper (please put your name at the top), sketch a basic program flow map that shows how the code you’ve already written fits together to solve this problem. Identify any bits of logic you need to write to solve the problem.
My solution is sketched out on sheet ___
R
Python
Skill: String Operations
Take the data frame you created in the previous problem and write a format_factorization(df) function that will output the results of that data frame as a string, so that the data frame containing the prime factorization of 18 that is shown above would return “2^1 x 3^2”.
Hint:
- Python: in a DataFrame, you can convert the whole column to a string using
df.colname.astype("str")(replace df, colname with appropriate data frame name and column name)
R
Python
Skill: Control Statements
Take the code you wrote in the previous part and use it to create a function prime_factorize that will return the prime factorization of a number. If the number provided is a prime, your function should return “
Planning
What modifications will you need to make to handle any number? e.g. what if the number is greater than 47?
What modifications will you need to make to handle prime numbers?
How can you use previously written code and functions to accomplish this task?
What additional code do you need to write?
If you sketched anything for this problem out on scratch paper, please give me a page number to look at.
My solution is sketched out on sheet ___
R
prime_factorize(34253)Error in prime_factorize(34253): could not find function "prime_factorize"
prime_factorize(prod(2:10))Error in prime_factorize(prod(2:10)): could not find function "prime_factorize"
Python
prime_factorize(34253) # should output "34253 is a prime number"NameError: name 'prime_factorize' is not defined
prime_factorize(3628800) # should output "2^8 x 3^4 x 5^2 x 7^1"NameError: name 'prime_factorize' is not defined
Skill: User-proofing your function
It is never safe to assume that your user knows what they are doing. Can you make your function from the previous part more robust by testing the user input to ensure that it conforms to your expectations?
Planning
What assumptions does your previous answer make about parameters?
What do you need to test to ensure those assumptions are met?
R
prime_factorize("string")Error in prime_factorize("string"): could not find function "prime_factorize"
prime_factorize(2:5)Error in prime_factorize(2:5): could not find function "prime_factorize"
Python
prime_factorize("string")NameError: name 'prime_factorize' is not defined
prime_factorize([2, 3, 4, 5])NameError: name 'prime_factorize' is not defined
A key for this exam can be found here.