代码高尔夫:Collat​​z猜想

受http://xkcd.com/710/的启发,这里是一个代码高尔夫球。

挑战

给定一个大于0的正整数,打印出该数字的雹石序列。

冰雹序列

请参阅维基百科了解更多详情。

  • 如果数字是偶数,则将其除以二。
  • 如果数字是奇数,则将其三倍并加一。

用产生的数字重复此操作直到它达到1(如果它继续在1之后,将进入1 -> 4 -> 2 -> 1...的无限循环)

有时代码是解释的最好方法,所以这里是一些来自Wikipedia

 function collatz(n) show n if n > 1 if n is odd call collatz(3n + 1) else call collatz(n / 2) 

此代码有效,但是我增加了额外的挑战。 该程序不能容易发生堆栈溢出 。 所以它必须使用迭代或尾recursion。

此外,如果它可以计算大数字和语言的奖励点还没有实施。 (或者如果你用固定长度的整数重新实现大数字支持)

testing用例

 Number: 21 Results: 21 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 Number: 3 Results: 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 

此外,代码高尔夫必须包括完整的用户input和输出。

x86汇编,1337个字符

 ; ; To assemble and link this program, just run: ; ; >> $ nasm -f elf collatz.asm && gcc -o collatz collatz.o ; ; You can then enjoy its output by passing a number to it on the command line: ; ; >> $ ./collatz 123 ; >> 123 --> 370 --> 185 --> 556 --> 278 --> 139 --> 418 --> 209 --> 628 --> 314 ; >> --> 157 --> 472 --> 236 --> 118 --> 59 --> 178 --> 89 --> 268 --> 134 --> 67 ; >> --> 202 --> 101 --> 304 --> 152 --> 76 --> 38 --> 19 --> 58 --> 29 --> 88 ; >> --> 44 --> 22 --> 11 --> 34 --> 17 --> 52 --> 26 --> 13 --> 40 --> 20 --> 10 ; >> --> 5 --> 16 --> 8 --> 4 --> 2 --> 1 ; ; There's even some error checking involved: ; >> $ ./collatz ; >> Usage: ./collatz NUMBER ; section .text global main extern printf extern atoi main: cmp dword [esp+0x04], 2 jne .usage mov ebx, [esp+0x08] push dword [ebx+0x04] call atoi add esp, 4 cmp eax, 0 je .usage mov ebx, eax push eax push msg .loop: mov [esp+0x04], ebx call printf test ebx, 0x01 jz .even .odd: lea ebx, [1+ebx*2+ebx] jmp .loop .even: shr ebx, 1 cmp ebx, 1 jne .loop push ebx push end call printf add esp, 16 xor eax, eax ret .usage: mov ebx, [esp+0x08] push dword [ebx+0x00] push usage call printf add esp, 8 mov eax, 1 ret msg db "%d --> ", 0 end db "%d", 10, 0 usage db "Usage: %s NUMBER", 10, 0 

Befunge

 &>:.:1-| >3*^ @ |%2: < v>2/>+ 

LOLCODE:406 CHARAKTERZ

 HAI BTW COLLATZ SOUNDZ JUS LULZ CAN HAS STDIO? I HAS A NUMBAR BTW, I WANTS UR NUMBAR GIMMEH NUMBAR VISIBLE NUMBAR IM IN YR SEQUENZ MOD OF NUMBAR AN 2 BOTH SAEM IT AN 0, O RLY? YA RLY, NUMBAR R QUOSHUNT OF NUMBAR AN 2 NO WAI, NUMBAR R SUM OF PRODUKT OF NUMBAR AN 3 AN 1 OIC VISIBLE NUMBAR DIFFRINT 2 AN SMALLR OF 2 AN NUMBAR, O RLY? YA RLY, GTFO OIC IM OUTTA YR SEQUENZ KTHXBYE 

TESTD UNDR JUSTIN J. MEZA的解释 。 KTHXBYE!

Python – 95 64 51 46 char

显然不会产生堆栈溢出。

 n=input() while n>1:n=(n/2,n*3+1)[n%2];print n 

Perl的

我决定要有一点反竞争性,并说明你通常在Perl中如何编写这样的问题。
最后还有一个46(总计)的char代码 – 高尔夫项目。

这三个例子都是从这个头文件开始的。

 #! /usr/bin/env perl use Modern::Perl; # which is the same as these three lines: # use 5.10.0; # use strict; # use warnings; while( <> ){ chomp; last unless $_; Collatz( $_ ); } 
  • 简单的recursion版本

     use Sub::Call::Recur; sub Collatz{ my( $n ) = @_; $n += 0; # ensure that it is numeric die 'invalid value' unless $n > 0; die 'Integer values only' unless $n == int $n; say $n; given( $n ){ when( 1 ){} when( $_ % 2 != 0 ){ # odd recur( 3 * $n + 1 ); } default{ # even recur( $n / 2 ); } } } 
  • 简单的迭代版本

     sub Collatz{ my( $n ) = @_; $n += 0; # ensure that it is numeric die 'invalid value' unless $n > 0; die 'Integer values only' unless $n == int $n; say $n; while( $n > 1 ){ if( $n % 2 ){ # odd $n = 3 * $n + 1; } else { #even $n = $n / 2; } say $n; } } 
  • 优化的迭代版本

     sub Collatz{ my( $n ) = @_; $n += 0; # ensure that it is numeric die 'invalid value' unless $n > 0; die 'Integer values only' unless $n == int $n; # state @next; $next[1] //= 0; # sets $next[1] to 0 if it is undefined # # fill out @next until we get to a value we've already worked on until( defined $next[$n] ){ say $n; # if( $n % 2 ){ # odd $next[$n] = 3 * $n + 1; } else { # even $next[$n] = $n / 2; } # $n = $next[$n]; } say $n; # finish running until we get to 1 say $n while $n = $next[$n]; } 

现在我将展示如何使用v5.10.0之前版本的Perl来做最后一个例子

 #! /usr/bin/env perl use strict; use warnings; while( <> ){ chomp; last unless $_; Collatz( $_ ); } { my @next = (0,0); # essentially the same as a state variable sub Collatz{ my( $n ) = @_; $n += 0; # ensure that it is numeric die 'invalid value' unless $n > 0; # fill out @next until we get to a value we've already worked on until( $n == 1 or defined $next[$n] ){ print $n, "\n"; if( $n % 2 ){ # odd $next[$n] = 3 * $n + 1; } else { # even $next[$n] = $n / 2; } $n = $next[$n]; } print $n, "\n"; # finish running until we get to 1 print $n, "\n" while $n = $next[$n]; } } 

基准

首先,IO总是会成为缓慢的一部分。 所以,如果你真的把它们作为基准,你应该得到每一个速度相同的速度。

为了testing这些,我打开了一个文件句柄到/dev/null$null ),然后编辑每一个say $n来代替读say {$null} $n 。 这是为了减less对IO的依赖。

 #! /usr/bin/env perl use Modern::Perl; use autodie; open our $null, '>', '/dev/null'; use Benchmark qw':all'; cmpthese( -10, { Recursive => sub{ Collatz_r( 31 ) }, Iterative => sub{ Collatz_i( 31 ) }, Optimized => sub{ Collatz_o( 31 ) }, }); sub Collatz_r{ ... say {$null} $n; ... } sub Collatz_i{ ... say {$null} $n; ... } sub Collatz_o{ ... say {$null} $n; ... } 

运行了10次之后,下面是一个有代表性的示例输出:

            速率recursion迭代优化
recursion1715 / s  -  -27%-46%
迭代2336 / s 36% -  -27%
优化3187 / s 86%36% - 

最后,一个真正的代码高尔夫项目:

 perl -nlE'say;say$_=$_%2?3*$_+1:$_/2while$_>1' 

共46个字符

如果您不需要打印起始值,则可以删除5个以上的字符。

 perl -nE'say$_=$_%2?3*$_+1:$_/2while$_>1' 

共41个字符
实际代码部分为31个字符,但是代码在没有-n开关的情况下将不起作用。 所以我把整个例子都包括进去了。

哈斯格尔,62个字符63 76 83,86,97,137

 c 1=[1] cn=n:c(div(n`mod`2*(5*n+2)+n)2) main=readLn>>=print.c 

用户input,打印输出,使用常量内存和堆栈,可以使用任意大的整数。

这个代码的示例运行 ,给定了所有'1(!)作为input的80位数字,看起来非常有趣。


原创,function唯一版本:

哈斯克尔51个字符

 fn=n:[[],f([n`div`2,3*n+1]!!(n`mod`2))]!!(1`mod`n) 

无论如何谁是@&^#需要条件?

(编辑:我是“聪明的”,并使用修复。没有它,代码降低到54个字符编辑2:下降到51通过分解f()

Golfscript:20个字符

  ~{(}{3*).1&5*)/}/1+` # # Usage: echo 21 | ruby golfscript.rb collatz.gs 

这相当于

 stack<int> s; s.push(21); while (s.top() - 1) { int x = s.top(); int numerator = x*3+1; int denominator = (numerator&1) * 5 + 1; s.push(numerator/denominator); } s.push(1); return s; 

BC41字

我想这种问题是bc发明的:

 for(n=read();n>1;){if(n%2)n=n*6+2;n/=2;n} 

testing:

 bc1 -q collatz.bc 21 64 32 16 8 4 2 1 

适当的代码:

 for(n=read();n>1;){if(n%2)n=n*3+1else n/=2;print n,"\n"} 

bc处理高达INT_MAX数字的数字

编辑: 维基百科文章提到这个猜想已被检查所有值高达20×2 58aprox。5.76e18 )。 这个程序:

 c=0;for(n=2^20000+1;n>1;){if(n%2)n=n*6+2;n/=2;c+=1};n;c 

68秒内testing2个20,000 +1 (约3.98e6,020 ), 144,404个周期。

Perl:31个字符

 perl -nE 'say$_=$_%2?$_*3+1:$_/2while$_>1' # 123456789 123456789 123456789 1234567 

编辑删除2个不必要的空间。

编辑删除1个不必要的空间。

MS Excel,35个字符

 =IF(A1/2=ROUND(A1/2,0),A1/2,A1*3+1) 

从维基百科直接采取:

 In cell A1, place the starting number. In cell A2 enter this formula =IF(A1/2=ROUND(A1/2,0),A1/2,A1*3+1) Drag and copy the formula down until 4, 2, 1 

它只复制/粘贴公式111次,得到1000的起始数字的结果;;)

C:64个字符

 main(x){for(scanf("%d",&x);x>=printf("%d,",x);x=x&1?3*x+1:x/2);} 

大整数支持:431(必要)字符

 #include <stdlib.h> #define B (w>=m?d=realloc(d,m=m+m):0) #define S(a,b)t=a,a=b,b=t main(m,w,i,t){char*d=malloc(m=9);for(w=0;(i=getchar()+2)/10==5;) B,d[w++]=i%10;for(i=0;i<w/2;i++)S(d[i],d[wi-1]);for(;;w++){ while(w&&!d[w-1])w--;for(i=w+1;i--;)putchar(i?d[i-1]+48:10);if( w==1&&*d==1)break;if(*d&1){for(i=w;i--;)d[i]*=3;*d+=1;}else{ for(i=w;i-->1;)d[i-1]+=d[i]%2*10,d[i]/=2;*d/=2;}B,d[w]=0;for(i=0 ;i<w;i++)d[i+1]+=d[i]/10,d[i]%=10;}} 

注意 :不要删除#include <stdlib.h>而至less没有原型化malloc / realloc,因为这样做在64位平台上将不安全(64位void *将被转换为32位int)。

这个还没有经过强有力的testing。 它也可以使用一些缩短。


之前的版本:

 main(x){for(scanf("%d",&x);printf("%d,",x),x-1;x=x&1?3*x+1:x/2);} // 66 

(删除了12个字符,因为没有人遵循输出格式…:|)

另一个汇编版本。 这个不限于32位数字,它可以处理多达10 65534的数字,尽pipeMS-DOS使用的“.com”格式限制为80位数字。 写为A86汇编程序,并需要一个Win-XP的DOS框运行。 汇编到180字节:

  mov ax,cs mov si,82h add ah,10h mov es,ax mov bh,0 mov bl,byte ptr [80h] cmp bl,1 jbe ret dec bl mov cx,bx dec bl xor di,di p1:lodsb sub al,'0' cmp al,10 jae ret stosb loop p1 xor bp,bp push es pop ds p2:cmp byte ptr ds:[bp],0 jne p3 inc bp jmp p2 ret p3:lea si,[bp-1] cld p4:inc si mov dl,[si] add dl,'0' mov ah,2 int 21h cmp si,bx jne p4 cmp bx,bp jne p5 cmp byte ptr [bx],1 je ret p5:mov dl,'-' mov ah,2 int 21h mov dl,'>' int 21h test byte ptr [bx],1 jz p10 ;odd mov si,bx mov di,si mov dx,3 dec bp std p6:lodsb mul dl add al,dh aam mov dh,ah stosb cmp si,bp jnz p6 or dh,dh jz p7 mov al,dh stosb dec bp p7:mov si,bx mov di,si p8:lodsb inc al xor ah,ah aaa stosb or ah,ah jz p9 cmp si,bp jne p8 mov al,1 stosb jmp p2 p9:inc bp jmp p2 p10:mov si,bp mov di,bp xor ax,ax p11:lodsb test ah,1 jz p12 add al,10 p12:mov ah,al shr al,1 cmp di,bx stosb jne p11 jmp p2 

直stream – 24个字符25 28

dc是这个序列的一个好工具:

 ?[d5*2+d2%*+2/pd1<L]dsLx 
 dc -f collat​​z.dc
 21
 64
 32
 16
 8
 4
 2
 1

还有24个字符使用Golfscript条目的公式:

 ?[3*1+d2%5*1+/pd1<L]dsLx 

57个字符符合规格:

 [Number: ]n?[Results: ]ndn[d5*2+d2%*+2/[ -> ]ndnd1<L]dsLx 
 dc -f collat​​z-spec.dc
编号:3
结果:3→10→5→16→8→4→2→1

scheme:72

 (define(cn)(if(= n 1)`(1)(cons n(if(odd? n)(c(+(* n 3)1))(c(/ n 2)))))) 

这使用recursion,但调用是尾recursion的,所以我认为他们将被优化迭代。 在一些快速testing中,我无法find堆栈溢出的数字。 举个例子:

(c 9876543219999999999000011234567898888777766665555444433332222 7777777777777777777777777777777798797657657651234143375987342987 5398709812374982529830983743297432985230985739287023987532098579 058095873098753098370938753987)

…运行得很好。 [这都是一个数字 – 我已经把它打破在屏幕上。]

Mathematica,45 50 字符

 c=NestWhileList[If[OddQ@#,3#+1,#/2]&,#,#>1&]& 

ruby,50个字符,没有堆栈溢出

基本上是makapuf的Python解决scheme的直接翻版 :

 def c(n)while n>1;n=n.odd?? n*3+1: n/2;pn end end 

ruby,45个字符,将溢出

基本上是问题中提供的代码的直接翻版:

 def c(n)pn;n.odd?? c(3*n+1):c(n/2)if n>1 end 
 import java.math.BigInteger; public class SortaJava { static final BigInteger THREE = new BigInteger("3"); static final BigInteger TWO = new BigInteger("2"); interface BiFunc<R, A, B> { R call(A a, B b); } interface Cons<A, B> { <R> R apply(BiFunc<R, A, B> func); } static class Collatz implements Cons<BigInteger, Collatz> { BigInteger value; public Collatz(BigInteger value) { this.value = value; } public <R> R apply(BiFunc<R, BigInteger, Collatz> func) { if(BigInteger.ONE.equals(value)) return func.call(value, null); if(value.testBit(0)) return func.call(value, new Collatz((value.multiply(THREE)).add(BigInteger.ONE))); return func.call(value, new Collatz(value.divide(TWO))); } } static class PrintAReturnB<A, B> implements BiFunc<B, A, B> { boolean first = true; public B call(A a, B b) { if(first) first = false; else System.out.print(" -> "); System.out.print(a); return b; } } public static void main(String[] args) { BiFunc<Collatz, BigInteger, Collatz> printer = new PrintAReturnB<BigInteger, Collatz>(); Collatz collatz = new Collatz(new BigInteger(args[0])); while(collatz != null) collatz = collatz.apply(printer); } } 

Python 45字符

从makapuf的答案刮了一个字符。

 n=input() while~-n:n=(n/2,n*3+1)[n%2];print n 

TI-BASIC

不是最短的,而是一种新颖的方法。 肯定会大幅减慢,但不应该溢出。

 PROGRAM:COLLATZ :ClrHome :Input X :Lbl 1 :While X≠1 :If X/2=int(X/2) :Then :Disp X/2→X :Else :Disp X*3+1→X :End :Goto 1 :End 

哈斯克尔:50

 c 1=[1];cn=n:(c$if odd n then 3*n+1 else n`div`2) 

不是最短的,而是一个优雅的clojure解决scheme

 (defn collatz [n] (print n "") (if (> n 1) (recur (if (odd? n) (inc (* 3 n)) (/ n 2))))) 

C#:216个字符

 using C=System.Console;class P{static void Main(){var p="start:";System.Action<object> o=C.Write;o(p);ulong i;while(ulong.TryParse(C.ReadLine(),out i)){o(i);while(i > 1){i=i%2==0?i/2:i*3+1;o(" -> "+i);}o("\n"+p);}}} 

长forms:

 using C = System.Console; class P { static void Main() { var p = "start:"; System.Action<object> o = C.Write; o(p); ulong i; while (ulong.TryParse(C.ReadLine(), out i)) { o(i); while (i > 1) { i = i % 2 == 0 ? i / 2 : i * 3 + 1; o(" -> " + i); } o("\n" + p); } } } 

新版本,接受一个数字作为input通过命令行提供,没有inputvalidation。 173 154个字符。

 using System;class P{static void Main(string[]a){Action<object>o=Console.Write;var i=ulong.Parse(a[0]);o(i);while(i>1){i=i%2==0?i/2:i*3+1;o(" -> "+i);}}} 

长forms:

 using System; class P { static void Main(string[]a) { Action<object>o=Console.Write; var i=ulong.Parse(a[0]); o(i); while(i>1) { i=i%2==0?i/2:i*3+1; o(" -> "+i); } } } 

我可以通过在这个答案中删除一个使用for循环而不是一段时间的想法来剃掉几个字符。 150个字符。

 using System;class P{static void Main(string[]a){Action<object>o=Console.Write;for(var i=ulong.Parse(a[0]);i>1;i=i%2==0?i/2:i*3+1)o(i+" -> ");o(1);}} 

ruby,43个字符

支持堆肥,堆溢stream敏感性:

 def c(n)pn;n%2>0?c(3*n+1):c(n/2)if n>1 end 

…和50个字符,支持bignum,没有堆栈溢出:

 def d(n)while n>1 do pn;n=n%2>0?3*n+1:n/2 end end 

荣誉约旦。 我不知道“p”作为替代投入。

nroff 1

运行nroff -U hail.g

 .warn .pl 1 .pso (printf "Enter a number: " 1>&2); read x; echo .nr x $x .while \nx>1 \{\ . ie \nx%2 .nr x \nx*3+1 . el .nr x \nx/2 \nx .\} 

1. groff版本

斯卡拉+斯卡拉

 import scalaz._ import Scalaz._ val collatz = (_:Int).iterate[Stream](a=>Seq(a/2,3*a+1)(a%2)).takeWhile(1<) // This line: 61 chars 

并在行动:

 scala> collatz(7).toList res15: List[Int] = List(7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2) 

斯卡拉2.8

 val collatz = Stream.iterate(_:Int)(a=>Seq(a/2,3*a+1)(a%2)).takeWhile(1<) :+ 1 

这也包括尾随1。

 scala> collatz(7) res12: scala.collection.immutable.Stream[Int] = Stream(7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1) 

以下隐含

 implicit def intToEven(i:Int) = new { def ~(even: Int=>Int, odd: Int=>Int) = { if (i%2==0) { even(i) } else { odd(i) } } } 

这可以缩短到

 val collatz = Stream.iterate(_:Int)(_~(_/2,3*_+1)).takeWhile(1<) :+ 1 

编辑 – 58个字符 (包括input和输出,但不包括初始编号)

 var n=readInt;while(n>1){n=Seq(n/2,n*3+1)(n%2);println(n)} 

如果你不需要换行符,可以减2

F#,90个字符

 let c=Seq.unfold(function|n when n<=1->None|n when n%2=0->Some(n,n/2)|n->Some(n,(3*n)+1)) > c 21;; val it : seq<int> = seq [21; 64; 32; 16; ...] 

或者,如果您不使用F#交互式显示结果,则有102个字符:

 let c=Seq.unfold(function|n when n<=1->None|n when n%2=0->Some(n,n/2)|n->Some(n,(3*n)+1))>>printf"%A" 

Common Lisp,141个字符:

 (defun c () (format t"Number: ") (loop for n = (read) then (if(oddp n)(+ 1 nnn)(/ n 2)) until (= n 1) do (format t"~d -> "n)) (format t"1~%")) 

testing运行:

 Number: 171 171 -> 514 -> 257 -> 772 -> 386 -> 193 -> 580 -> 290 -> 145 -> 436 -> 218 -> 109 -> 328 -> 164 -> 82 -> 41 -> 124 -> 62 -> 31 -> 94 -> 47 -> 142 -> 71 -> 214 -> 107 -> 322 -> 161 -> 484 -> 242 -> 121 -> 364 -> 182 -> 91 -> 274 -> 137 -> 412 -> 206 -> 103 -> 310 -> 155 -> 466 -> 233 -> 700 -> 350 -> 175 -> 526 -> 263 -> 790 -> 395 -> 1186 -> 593 -> 1780 -> 890 -> 445 -> 1336 -> 668 -> 334 -> 167 -> 502 -> 251 -> 754 -> 377 -> 1132 -> 566 -> 283 -> 850 -> 425 -> 1276 -> 638 -> 319 -> 958 -> 479 -> 1438 -> 719 -> 2158 -> 1079 -> 3238 -> 1619 -> 4858 -> 2429 -> 7288 -> 3644 -> 1822 -> 911 -> 2734 -> 1367 -> 4102 -> 2051 -> 6154 -> 3077 -> 9232 -> 4616 -> 2308 -> 1154 -> 577 -> 1732 -> 866 -> 433 -> 1300 -> 650 -> 325 -> 976 -> 488 -> 244 -> 122 -> 61 -> 184 -> 92 -> 46 -> 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 

该计划frm杰里棺材有整数溢出,尝试这一个:

 #include <iostream> int main(unsigned long long i) { int j = 0; for( std::cin>>i; i>1; i = i&1? i*3+1:i/2, ++j) std::cout<<i<<" -> "; std::cout<<"\n"<<j << " iterations\n"; } 

经过testing

总数不到1亿,总停留时间最长的是63,728,127,有949步。

总数不到10亿,总停留时间最长的是670,617,279,有986步。

ruby43,可能符合I / O要求


ruby -n hail运行

 n=$_.to_i (n=n%2>0?n*3+1: n/2 pn)while n>1 

C#:659字符与BigInteger支持

 using System.Linq;using C=System.Console;class Program{static void Main(){var v=C.ReadLine();C.Write(v);while(v!="1"){C.Write("->");if(v[v.Length-1]%2==0){v=v.Aggregate(new{s="",o=0},(r,c)=>new{s=r.s+(char)((c-48)/2+r.o+48),o=(c%2)*5}).s.TrimStart('0');}else{var q=v.Reverse().Aggregate(new{s="",o=0},(r, c)=>new{s=(char)((c-48)*3+r.o+(c*3+ro>153?c*3+ro>163?28:38:48))+rs,o=c*3+ro>153?c*3+ro>163?2:1:0});var t=(q.o+qs).TrimStart('0').Reverse();var x=t.First();q=t.Skip(1).Aggregate(new{s=x>56?(x-57).ToString():(x-47).ToString(),o=x>56?1:0},(r,c)=>new{s=(char)(c-48+r.o+(c+ro>57?38:48))+rs,o=c+ro>57?1:0});v=(q.o+qs).TrimStart('0');}C.Write(v);}}} 

Ungolfed

 using System.Linq; using C = System.Console; class Program { static void Main() { var v = C.ReadLine(); C.Write(v); while (v != "1") { C.Write("->"); if (v[v.Length - 1] % 2 == 0) { v = v .Aggregate( new { s = "", o = 0 }, (r, c) => new { s = rs + (char)((c - 48) / 2 + ro + 48), o = (c % 2) * 5 }) .s.TrimStart('0'); } else { var q = v .Reverse() .Aggregate( new { s = "", o = 0 }, (r, c) => new { s = (char)((c - 48) * 3 + ro + (c * 3 + ro > 153 ? c * 3 + ro > 163 ? 28 : 38 : 48)) + rs, o = c * 3 + ro > 153 ? c * 3 + ro > 163 ? 2 : 1 : 0 }); var t = (qo + qs) .TrimStart('0') .Reverse(); var x = t.First(); q = t .Skip(1) .Aggregate( new { s = x > 56 ? (x - 57).ToString() : (x - 47).ToString(), o = x > 56 ? 1 : 0 }, (r, c) => new { s = (char)(c - 48 + ro + (c + ro > 57 ? 38 : 48)) + rs, o = c + ro > 57 ? 1 : 0 }); v = (qo + qs) .TrimStart('0'); } C.Write(v); } } }