From d11d8a920dcb0d1080a8b627a9ebac33abe48a02 Mon Sep 17 00:00:00 2001 From: Gokul A Date: Sat, 21 Nov 2026 14:01:16 +0530 Subject: [PATCH 1/3] feat: add Collatz conjecture --- mathematics/collatz_conjecture.r | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 mathematics/collatz_conjecture.r diff --git a/mathematics/collatz_conjecture.r b/mathematics/collatz_conjecture.r new file mode 100644 index 0000000..d84427c --- /dev/null +++ b/mathematics/collatz_conjecture.r @@ -0,0 +1,77 @@ +# Collatz Conjecture +# +# Start with any positive integer n and repeat: +# - if n is even, divide it by 2 +# - if n is odd, replace it with 3 * n + 1 +# The conjecture states that every positive integer eventually reaches 1. +# It is unproven, but it holds for every number tested so far. +# +# Time Complexity: O(s) where s = number of steps to reach 1 +# Space Complexity: O(s) to store the sequence, O(1) to only count steps +# +# Applications: +# - Classic example of an unsolved problem in number theory +# - Practice for loops and sequence generation +# - Testing integer arithmetic and overflow handling +# +# Example usage: +# collatz_sequence(6) # 6 3 10 5 16 8 4 2 1 +# collatz_steps(27) # 111 + +check_collatz_input <- function(n) { + if (!is.numeric(n) || length(n) != 1 || is.na(n) || n < 1 || n != floor(n)) { + stop("Error: n must be a positive integer") + } +} + +#' Generate the Collatz sequence starting at n +#' @param n Positive integer +#' @return Numeric vector from n down to 1 +collatz_sequence <- function(n) { + check_collatz_input(n) + sequence <- n + while (n != 1) { + if (n %% 2 == 0) { + n <- n / 2 + } else { + n <- 3 * n + 1 + } + sequence <- c(sequence, n) + } + sequence +} + +#' Count the steps needed to reach 1 +#' @param n Positive integer +#' @return Number of steps (0 for n = 1) +collatz_steps <- function(n) { + check_collatz_input(n) + steps <- 0 + while (n != 1) { + if (n %% 2 == 0) { + n <- n / 2 + } else { + n <- 3 * n + 1 + } + steps <- steps + 1 + } + steps +} + +# =========================== +# Example Usage & Testing +# =========================== +stopifnot(identical(collatz_sequence(1), 1)) +stopifnot(identical(collatz_sequence(6), c(6, 3, 10, 5, 16, 8, 4, 2, 1))) +stopifnot(collatz_steps(1) == 0) +stopifnot(collatz_steps(6) == 8) +stopifnot(collatz_steps(27) == 111) +stopifnot(length(collatz_sequence(27)) == collatz_steps(27) + 1) + +# Invalid input raises an error +stopifnot(inherits(try(collatz_steps(0), silent = TRUE), "try-error")) +stopifnot(inherits(try(collatz_steps(2.5), silent = TRUE), "try-error")) + +cat("Collatz sequence of 6:", paste(collatz_sequence(6), collapse = " "), "\n") +cat("Steps for 27:", collatz_steps(27), "\n") +cat("All Collatz tests passed\n") From 57869edc249cfcab9f6258aa0178414b907aef03 Mon Sep 17 00:00:00 2001 From: Gokul A Date: Tue, 24 Nov 2026 11:35:02 +0530 Subject: [PATCH 2/3] docs: add Collatz Conjecture to DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7f900da..620fa35 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -88,6 +88,7 @@ * [Armstrong Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/armstrong_number.r) * [Bisection Method](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/bisection_method.r) * [Catalan Numbers](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/catalan_numbers.r) + * [Collatz Conjecture](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/collatz_conjecture.r) * [Euclidean Distance](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/euclidean_distance.r) * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/extended_euclidean_algorithm.r) * [Factorial](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/factorial.r) From 2a1ec6e3ac9d070579442dacbeeefb35aaf40f68 Mon Sep 17 00:00:00 2001 From: Gokul A Date: Tue, 24 Nov 2026 11:37:15 +0530 Subject: [PATCH 3/3] fix: guard Collatz against precision loss, non-finite input and quadratic copying --- mathematics/collatz_conjecture.r | 42 ++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/mathematics/collatz_conjecture.r b/mathematics/collatz_conjecture.r index d84427c..5c9f9a8 100644 --- a/mathematics/collatz_conjecture.r +++ b/mathematics/collatz_conjecture.r @@ -19,9 +19,26 @@ # collatz_steps(27) # 111 check_collatz_input <- function(n) { - if (!is.numeric(n) || length(n) != 1 || is.na(n) || n < 1 || n != floor(n)) { + if (!is.numeric(n) || length(n) != 1 || !is.finite(n) || n < 1 || + n != floor(n)) { stop("Error: n must be a positive integer") } + if (n > 2^53) { + stop("Error: n exceeds the exact-integer range") + } +} + +# Apply one Collatz step. Doubles hold integers exactly only up to 2^53, so +# stop before 3 * n + 1 would pass that limit instead of returning a rounded +# (wrong) value. +collatz_next <- function(n) { + if (n %% 2 == 0) { + return(n / 2) + } + if (n > (2^53 - 1) / 3) { + stop("Error: Collatz transition exceeds the exact-integer range") + } + 3 * n + 1 } #' Generate the Collatz sequence starting at n @@ -29,16 +46,19 @@ check_collatz_input <- function(n) { #' @return Numeric vector from n down to 1 collatz_sequence <- function(n) { check_collatz_input(n) - sequence <- n + # Grow the buffer geometrically and trim once at the end: amortized O(s) + sequence <- numeric(16) + sequence[1] <- n + sequence_length <- 1 while (n != 1) { - if (n %% 2 == 0) { - n <- n / 2 - } else { - n <- 3 * n + 1 + n <- collatz_next(n) + sequence_length <- sequence_length + 1 + if (sequence_length > length(sequence)) { + length(sequence) <- 2 * length(sequence) } - sequence <- c(sequence, n) + sequence[sequence_length] <- n } - sequence + sequence[seq_len(sequence_length)] } #' Count the steps needed to reach 1 @@ -48,11 +68,7 @@ collatz_steps <- function(n) { check_collatz_input(n) steps <- 0 while (n != 1) { - if (n %% 2 == 0) { - n <- n / 2 - } else { - n <- 3 * n + 1 - } + n <- collatz_next(n) steps <- steps + 1 } steps