fork download
  1. library(gmp)
  2.  
  3. `[` <- function(a, b)
  4. {
  5. if (!is.character(a)) return(.Primitive("[")(a, b))
  6. paste0(substring(a, b, b), collapse = "")
  7. }
  8.  
  9. `+` <- function(a, b)
  10. {
  11. if (!is.character(a)) return(.Primitive("+")(a, b))
  12. paste0(a, b)
  13. }
  14.  
  15. NearestPalindromeNumbers <- function(n)
  16. {
  17. s <- as.character(n)
  18. if (length(grep("^10+$", s))) return(c(n - 1, n + 1))
  19.  
  20. j <- nchar(s) %/% 2
  21. i <- nchar(s) - j
  22. a <- s[0:i]
  23. b <- a[j:0]
  24. x <- as.bigz(a + b)
  25. if (x == n) return(x)
  26.  
  27. a <- as.character(as.bigz(a) + ifelse(x < n, 1, -1))
  28. b <- a[j:0]
  29. y <- as.bigz(a + b)
  30. switch(sign(abs(x - n) - abs(y - n)) + 2, x, range(x, y), y)
  31. }
  32.  
  33. q = list(0, 5, 17, 100, 2024, as.bigz("12345679042654321"), as.bigz("31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"))
  34.  
  35. for (n in q) cat(sprintf("[%s]\n", toString(NearestPalindromeNumbers(n))))
Success #stdin #stdout #stderr 0.32s 44060KB
stdin
Standard input is empty
stdout
[0]
[5]
[22]
[99, 101]
[2002]
[12345678987654321, 12345679097654321]
[31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491319491103818397221984272575881537659472699732644708458115013971162391837116812903591959575630727503349061151491596414831256640288450350331100630952987634635171904529282690290251884718855136066007854273721419420627063933128466234540168430643296658465419091021725613876873328465748216443395665790188244691830394598492264469555011258391072014820547111848218049535271322850559064483907466032823156808412897607112435284308268998026826046187032954494790285015739939617914882059723833462648323979853562951413]
stderr
Attaching package: ‘gmp’

The following objects are masked from ‘package:base’:

    %*%, apply, crossprod, matrix, tcrossprod