This is an R programming language The Collatz function C(x) takes any integer x
ID: 3874324 • Letter: T
Question
This is an R programming language
The Collatz function C(x) takes any integer x as an input, and then follows these steps If x is odd then C(x) = 3x If x is even then C(x)x/2 1 Task 1: Make a function which takes a number x as an argument and returns 3x +1 Task 2: Make a function which takes a number x as an argument and returns x/2. Task 3: Make a function that implements the Collatz function. Task 4: Make a function that iterates the Collatz function a user specified n of times, and returns the trajectory. Task 5: Make a function that iterates the Collatz function and stops when the trajectory reaches 1. It should then print out the trajectory. Task 6: Make a function which takes a number x as an argument and returns a function it takes to reach the number 1. list that contains the orbit of x and the number of iterations of the CollatzExplanation / Answer
iscol.even <- function(c)
{
if(c%%2==0)
{
print("True")
}
else
{
print("False")
}
}
collatz <- function(col)
{
if(iscol.even(col)){
col/2
}
else
{
3*col + 1
}
}
col <- 3
col.total <- NULL
while(col!=1){
col <- collatz(col)
col.total <- c(col.total, col)
}
col.total
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.