fork download
  1. function NearestParindromeNumber([BigInt]$n)
  2. {
  3. if ($n -match "^10+$") {return ($n - 1), ($n + 1)}
  4.  
  5. $s = [String]$n
  6. $i = ($s.length + ($s.length % 2)) / 2 - 1
  7. $j = $i - $s.length % 2
  8. $a = -join $s[0..$i]
  9. $b = -join $a[$j..0]
  10. $x = [BigInt]($a + $b)
  11. if ($x -eq $n) {return $x}
  12.  
  13. $a = [String]([BigInt]$a + @(-1, 1)[$x -lt $n])
  14. $b = -join $a[$j..0]
  15. $y = [BigInt]($a + $b)
  16. switch ([BigInt]::Abs($x - $n) - [BigInt]::Abs($y - $n)) {
  17. {$_ -lt 0} {return $x}
  18. {$_ -eq 0} {return $x, $y}
  19. {$_ -gt 0} {return $y}
  20. }
  21. }
  22.  
  23. $q = 0, 17, 100, 123459321, [BigInt]"31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"
  24.  
  25. foreach ($n in $q) {"[$((NearestParindromeNumber $n) -join ", ")]"}
Not running #stdin #stdout 0s 0KB
stdin
-- 実行結果 --
[0]
[22]
[99, 101]
[123454321, 123464321]
[31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491319491103818397221984272575881537659472699732644708458115013971162391837116812903591959575630727503349061151491596414831256640288450350331100630952987634635171904529282690290251884718855136066007854273721419420627063933128466234540168430643296658465419091021725613876873328465748216443395665790188244691830394598492264469555011258391072014820547111848218049535271322850559064483907466032823156808412897607112435284308268998026826046187032954494790285015739939617914882059723833462648323979853562951413]
stdout
Standard output is empty