Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

STATS 102A

HW1

Due July 5, 2023

General Notes

. You will submit a minimum of three files, the core files must conform to the following

naming conventions (including capitalization and underscores).  123456789 is a placeholder,   please replace these nine digits with your nine-digit Bruin ID. The files you must submit are:

1.  123456789  stats102a hw1.R: An R script file containing all of the functions you wrote for the homework. The first line of your .Rmd file, after loading libraries, should  be sourcing this script file.

2.  123456789  stats102a hw1.Rmd: A markdown file generates the output file of your submission.

3.  123456789  stats102a hw1.html/pdf : An output file, either a PDF or an HTML file depending on the output you choose to generate.

4.  included image files: You may name these files what you choose, but you must include all of the image files you generated for your markdown file, otherwise your file will not knit. One way to attach an image file is to use ![title](example1.png).

If you fail to submit any of the required core files you will receive ZERO points for the assignment. If you submit any files which do not conform to the specified naming

convention, you will receive half credit for the assignment.

Your .Rmd file must be knittable.  If your .Rmd file does not knit in graders’ computer, you will receive at most half credit for the assignment.

The two most common reason files fail to knit are because of workspace/directory structure  issues and because of missing include files. To remedy the first, ensure all of the file paths in your document are relative paths pointing at the current working directory. To remedy the  second, simply make sure you upload any and all files you source or include in your .Rmd

file.

. Your coding should adhere to the tidyverse style guide: https://style.tidyverse.org/.

.  All flowcharts should be done on separate sheets of paper, but be included, inline as images, in your final markdown document.

. Any functions/classes you write should have the corresponding comments as the following format.

my function = function(x, y, ...){

#A short description of the function

#Args:

#x: Variable type and dimension

#y: Variable type and dimension

#Return:

#Variable type and dimension

Your codes begin here

}

NOTE: Everything you need to do this assignment is here, in your class notes, or was covered in discussion or lecture.

.  DO NOT look for solutions online.

.  DO NOT collaborate with anyone inside (or outside) of this class.

. Work INDEPENDENTLY on this assignment.

.  EVERYTHING you submit MUST be 100% your, original, work product. Any student suspected of plagiarizing, in whole or in part, any portion of this assignment, will be

immediately referred to the Dean of Student’s office without warning.

1:  Greatest Common Divisor (GCD) and Least Common Multiple (LCM)

Please write a function, gcd(), which takes two integers x, y and calculates their greatest common divisor (GCD) using the Euclidean algorithm.

The Euclidean Algorithm Example:

Let a = 180 and b = 25

1.  calculate 180/25, and get the result 7 with remainder 5, so 180 = 7 × 25 + 5.

2. calculate 25/5, and get the result 5 with remainder 0, so 25 = 5 × 5 + 0.

3. the greatest common divisor of 180 and 25 is 5.

Make use of gcd() to write a function, lcm(), which takes a vector and find the least common multiple. The length of the vector will be at least two but no more than 100.

2:  Prime Factorization

Please write a function get factors() that takes a number x and returns a list object. The list object should contain a vector of unique prime factors of x and the corresponding exponents.

Additionally, write one “helper function” is prime() that returns a logical vector depending on whether or not the elements in x are prime. A good way to test this function should be:

x  <-  sample(x  =  1e4,  size  =  1)

y  <-  get_factors(x)

this_works  <-  prod(y$primes^y$exponents)  ==  x  &  all(is_prime(y$primes))

It is a necessary, but not sufficient, condition that this works == TRUE for your function to work as intended.  (It will largely depend on if your is prime() function is correct.)

Homework Requirements:

a. Write algorithms (or flowchart) for gcd(), lcm(), and get factors() . Please make sure your

algorithms(or flowchart) sufficiently complete, clear, and concise enough to enable a person to

accurately implement the algorithm in any programming language they are adept with using.

b. Write the functions which accurately implements the algorithms as described or requested.

c.  Each function should respond in a reasonable time.

d. Include the error-handling to ensure your functions work properly.

e.  Showcase the functions. You may use suggested test cases or make up some examples but no more than 5 cases per function.

f. Keep all of the functions in a R script and make sure no R functions are written in the Rmd file.

g.  The Rmd file includes only algorithms (or flowchart), and examples. All the R functions should be saved in the other .R file.

Note:

The formatting takes 40% of your homework grade. The efficiency, accuracy, robustness, and error handling of your function account for the rest of 60%.

Sample test cases:

Here are some suggested test cases but not limited to while grading.

1.  GCD

. gcd(72, 8)→ 8

. gcd(-1, 531)→ 1

.  gcd(47011, 73618) 1

. lcm(c(-12, 21))→ 84

.  lcm(c(4789, 6123, 199))→ 5835286353

2.  Prime Factorization

. is prime(c(9, 7))  [False True]

. get factors(1920)  list(primes = [2 3 5], exponents = [7 1 1])

. get factors(1.92)  appropriate error handling