From e6e7ec07155e20c407f5a2017a22d4e596f8ede7 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 18 Oct 2025 00:40:43 +0530 Subject: [PATCH 1/2] Create isPalindrome.r --- mathematics/isPalindrome.r | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 mathematics/isPalindrome.r diff --git a/mathematics/isPalindrome.r b/mathematics/isPalindrome.r new file mode 100644 index 00000000..1b4e6b8c --- /dev/null +++ b/mathematics/isPalindrome.r @@ -0,0 +1,30 @@ +# Palindrome Number Checking in R +isPalindrome <- function(number) { + # Negative numbers are not considered palindromes + if (number < 0L) { + return(FALSE) + } + + original_number <- number + reversed_number <- 0L + + # Loop while the number is greater than 0 + while (number > 0L) { + # Get the last digit + remainder <- number %% 10L + + # Build the reversed number + reversed_number <- (reversed_number * 10L) + remainder + + # Remove the last digit using integer division + number <- number %/% 10L + } + + # Return TRUE if the original and reversed numbers are the same + return(original_number == reversed_number) +} + +isPalindrome(121) +isPalindrome(123) +isPalindrome(7) +isPalindrome(-101) From 805ac04d58f232445d9f68870e4ff837aa5d0eec Mon Sep 17 00:00:00 2001 From: ITZ-NIHALPATEL Date: Sat, 25 Oct 2025 21:13:58 +0530 Subject: [PATCH 2/2] Add to DIRECTORY.md --- DIRECTORY.md | 1 + mathematics/isPalindrome.r | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 376ebebd..a7a1e1b5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -85,6 +85,7 @@ * [Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/fibonacci.r) * [First N Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/first_n_fibonacci.r) * [Greatest Common Divisor](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/greatest_common_divisor.r) + * [Palindrome Check](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/isPalindrome.r) * [Josephus Problem](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/josephus_problem.r) * [Least Common Multiple](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/least_common_multiple.r) * [Modular Exponentiation](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/modular_exponentiation.r) diff --git a/mathematics/isPalindrome.r b/mathematics/isPalindrome.r index 1b4e6b8c..22de7050 100644 --- a/mathematics/isPalindrome.r +++ b/mathematics/isPalindrome.r @@ -1,4 +1,13 @@ -# Palindrome Number Checking in R +#' Check if a number is a palindrome +#' +#' @description Checks if an integer is a palindrome (reads the same forwards +#' and backward) without using strings. Negative numbers are not palindromes. +#' @param number The integer to check. +#' @return TRUE if the number is a palindrome, FALSE otherwise. +#' @examples +#' isPalindrome(121) +#' isPalindrome(123) + isPalindrome <- function(number) { # Negative numbers are not considered palindromes if (number < 0L) {