We all know that programming is fun but sometimes it can also be boring. In this blog let’s see how can we use the “Simple” but uncrackable math problem called Collatz Conjecture. “The conjecture is named after Lothar Collatz, who introduced the idea in 1937″ (Wikipedia, Sept. 13, 2021, para. 2).
The Collatz conjecture is a conjecture in mathematics that concerns sequences defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.
Wikipedia
So, the question is what exactly are we going to do here? Well as I’ve said, we want to have fun with this simple but uncrackable problem with R using R Studio. The goal here is to find the conjecture of a number using the Collatz Conjecture formula that if n=odd then multiplied by 3 plus 1 and if n=even divided by two until the number reaches 1. Who knows, you might encounter this as a coding test in your next job.
So if you are following this blog, I presumed that you have or know how to install the tools above. The steps that we are going to do are the following:
- Declare global variable as holder to count how many step(s) before reaching the number 1
- Create a function to initialize Collatz Conjecture calculations
- Create a funtion to check if the integer reaches number one
- Create a function to multiple the number by 3 plus one if the given value is odd
- Create a function for division if the given value is even
Declare global variable
This variable will be incremented every step will pass during calculations
step <- 0 #global variable
Collatz Conjecture calculations
In this function, you can see that the value is being checked if it’s Odd or Even, then do the required calculation based on the conditions. Since this will be called frequently, you can also see that this will increment the global variable to count the steps.
CollatzConjecture <- function(value) {
assign("step", step + 1, envir = .GlobalEnv) # Steps/counts handler
isOdd <- (value %% 2 == 1)
if (isOdd) {
print(paste("Odd value is:", value,sep = " ",collapse = NULL))
return(CheckIfOne(ThreeNPlusOne(value)))
}
else
{
print(paste("Even value is: ", value,sep = " ", collapse = NULL))
return(CheckIfOne(DivideByTwo(value)))
}
}
Function that check if reaches the number 1
This is the function needed to be executed to check if the given value is 1. If the value is not one then do other recursive Collatz Conjecture calculations.
In addition, we do not want to proceed any further if n=1, otherwise it will be on an infinite loop from 4 to 1.
CheckIfOne <- function(value) {
if (value != 1) {
return(CollatzConjecture(value))
}else{
return(paste("Reached #:", value,"Count Of Steps:", step,sep = " ",collapse = ""))
}
}
Function for 3n + 1 if n is Odd
ThreeNPlusOne <- function(value) {
result<-((value * 3) + 1)
print(paste("3 *", value, "+ 1=", result, sep = " ",collapse = NULL))
return(result)
}
Function for division if n is even
DivideByTwo <- function(value) {
result<-(value/2)
print(paste(value, "/ 2 =", result, sep = " ", collapse = NULL))
return(result)
}
Sample usage
CollatzConjecture(71)
Sample result, Note that not all are posted as there are 102 steps.
[1] "Odd value is: 53"
[1] "3 * 53 + 1= 160"
[1] "Even value is: 160"
[1] "160 / 2 = 80"
[1] "Even value is: 80"
[1] "80 / 2 = 40"
[1] "Even value is: 40"
[1] "40 / 2 = 20"
[1] "Even value is: 20"
[1] "20 / 2 = 10"
[1] "Even value is: 10"
[1] "10 / 2 = 5"
[1] "Odd value is: 5"
[1] "3 * 5 + 1= 16"
[1] "Even value is: 16"
[1] "16 / 2 = 8"
[1] "Even value is: 8"
[1] "8 / 2 = 4"
[1] "Even value is: 4"
[1] "4 / 2 = 2"
[1] "Even value is: 2"
[1] "2 / 2 = 1"
[1] "Reached #: 1 Count Of Steps: 102"
And that’s it, as you can see with simple and neat coding, you can get the Collatz Conjecture outcomes of any positive integers. Now, it is time for you to do it, improve or put some graphing. Hope you enjoy it, keep learning, and happy coding.
You can validate the output of our work here https://www.dcode.fr/collatz-conjecture