Skip to content

feat: add Collatz conjecture - #305

Open
CapedCrusader77 wants to merge 1 commit into
TheAlgorithms:masterfrom
CapedCrusader77:add-collatz
Open

CapedCrusader77 wants to merge 1 commit into
TheAlgorithms:masterfrom
CapedCrusader77:add-collatz

Conversation

@CapedCrusader77

Copy link
Copy Markdown

Adds mathematics/collatz_conjecture.r: sequence generation and step count, with input validation and example tests.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 08:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Double-precision rounding corrupts valid Collatz sequences and step counts, while non-finite inputs bypass validation and repeated vector copying violates the documented linear-time complexity.

3 open issues

Review effort: Balanced

Comment on lines +37 to +54
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
# collatz_steps(27) # 111

check_collatz_input <- function(n) {
if (!is.numeric(n) || length(n) != 1 || is.na(n) || n < 1 || n != floor(n)) {
Comment on lines +32 to +41
sequence <- n
while (n != 1) {
if (n %% 2 == 0) {
n <- n / 2
} else {
n <- 3 * n + 1
}
sequence <- c(sequence, n)
}
sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants