File tree Expand file tree Collapse file tree 1 file changed +10
-7
lines changed Expand file tree Collapse file tree 1 file changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -31,19 +31,22 @@ long SnafuToLong(string snafu) {
31
31
}
32
32
33
33
string LongToSnafu ( long d ) {
34
- // Almost standard base conversion, but when dealing with digits 3 and 4
35
- // we need to increment the higher decimal place and subtract 2 or 1.
34
+ // Almost standard base conversion, but when dealing with digits 3
35
+ // and 4 we need to increment the higher decimal place so that we have
36
+ // something to subtract 2 and 1 from.
37
+
36
38
var res = "" ;
37
39
while ( d > 0 ) {
38
- var digit = d % 5 ;
39
- d /= 5 ;
40
- switch ( digit ) {
40
+ switch ( d % 5 ) {
41
41
case 0 : res = '0' + res ; break ;
42
42
case 1 : res = '1' + res ; break ;
43
43
case 2 : res = '2' + res ; break ;
44
- case 3 : res = '=' + res ; d ++ ; break ;
45
- case 4 : res = '-' + res ; d ++ ; break ;
44
+ // add 5 and emit -2 because 3 = 5 -2
45
+ case 3 : d += 5 ; res = '=' + res ; break ;
46
+ // add 5 and emit -1 because 4 = 5 -1
47
+ case 4 : d += 5 ; res = '-' + res ; break ;
46
48
}
49
+ d /= 5 ;
47
50
}
48
51
return res ;
49
52
}
You can’t perform that action at this time.
0 commit comments