Imaginary base numbers: Difference between revisions

Added a Ruby implementation.
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(Added a Ruby implementation.)
Line 3,366:
31433.3487654321-2902.4480452675i.&to-base( 6i) = PERL6.ROCKS : 'PERL6.ROCKS'.&from-base( 6i) = 31433.3487654321-2902.4480452675i
-3544.29+26541.468i.&to-base(-10i) = Raku.FTW : 'Raku.FTW'.&from-base(-10i) = -3544.29+26541.468i</pre>
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
===The Functions===
<lang ruby># Convert a quarter-imaginary base value (as a string) into a base 10 Gaussian integer.
 
def base2i_decode(qi)
return 0 if qi == '0'
md = qi.match(/^(?<int>[0-3]+)(?:\.(?<frc>[0-3]+))?$/)
raise 'ill-formed quarter-imaginary base value' if !md
ls_pow = md[:frc] ? -(md[:frc].length) : 0
value = 0
(md[:int] + (md[:frc] ? md[:frc] : '')).reverse.each_char.with_index do |dig, inx|
value += dig.to_i * (2i)**(inx + ls_pow)
end
return value
end
 
# Convert a base 10 Gaussian integer into a quarter-imaginary base value (as a string).
 
def base2i_encode(gi)
odd = gi.imag.to_i.odd?
frac = (gi.imag.to_i != 0)
real = gi.real.to_i
imag = (gi.imag.to_i + 1) / 2
value = ''
phase_real = true
while (real != 0) || (imag != 0)
if phase_real
real, rem = real.divmod(4)
real = -real
else
imag, rem = imag.divmod(4)
imag = -imag
end
value.prepend(rem.to_s)
phase_real = !phase_real
end
value = '0' if value == ''
value.concat(odd ? '.2' : '.0') if frac
return value
end</lang>
===The Task===
<lang ruby># Extend class Integer with a string conveter.
 
class Integer
def as_str()
return to_s()
end
end
 
# Extend class Complex with a string conveter (works only with Gaussian integers).
 
class Complex
def as_str()
return '0' if self == 0
return real.to_i.to_s if imag == 0
return imag.to_i.to_s + 'i' if real == 0
return real.to_i.to_s + '+' + imag.to_i.to_s + 'i' if imag >= 0
return real.to_i.to_s + '-' + (-(imag.to_i)).to_s + 'i'
end
end
 
# Emit various tables of conversions.
 
1.step(16) do |gi|
puts(" %4s -> %8s -> %4s %4s -> %8s -> %4s" %
[gi.as_str, base2i_encode(gi), base2i_decode(base2i_encode(gi)).as_str,
(-gi).as_str, base2i_encode(-gi), base2i_decode(base2i_encode(-gi)).as_str])
end
puts
1.step(16) do |gi|
gi *= 0+1i
puts(" %4s -> %8s -> %4s %4s -> %8s -> %4s" %
[gi.as_str, base2i_encode(gi), base2i_decode(base2i_encode(gi)).as_str,
(-gi).as_str, base2i_encode(-gi), base2i_decode(base2i_encode(-gi)).as_str])
end
puts
0.step(3) do |m|
0.step(3) do |l|
0.step(3) do |h|
qi = (100 * h + 10 * m + l).to_s
gi = base2i_decode(qi)
md = base2i_encode(gi).match(/^(?<num>[0-3]+)(?:\.0)?$/)
print(" %4s -> %6s -> %4s" % [qi, gi.as_str, md[:num]])
end
puts
end
end</lang>
{{out}}
Conversions given in the task.
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
2 -> 2 -> 2 -2 -> 102 -> -2
3 -> 3 -> 3 -3 -> 101 -> -3
4 -> 10300 -> 4 -4 -> 100 -> -4
5 -> 10301 -> 5 -5 -> 203 -> -5
6 -> 10302 -> 6 -6 -> 202 -> -6
7 -> 10303 -> 7 -7 -> 201 -> -7
8 -> 10200 -> 8 -8 -> 200 -> -8
9 -> 10201 -> 9 -9 -> 303 -> -9
10 -> 10202 -> 10 -10 -> 302 -> -10
11 -> 10203 -> 11 -11 -> 301 -> -11
12 -> 10100 -> 12 -12 -> 300 -> -12
13 -> 10101 -> 13 -13 -> 1030003 -> -13
14 -> 10102 -> 14 -14 -> 1030002 -> -14
15 -> 10103 -> 15 -15 -> 1030001 -> -15
16 -> 10000 -> 16 -16 -> 1030000 -> -16
 
1i -> 10.2 -> 1i -1i -> 0.2 -> -1i
2i -> 10.0 -> 2i -2i -> 1030.0 -> -2i
3i -> 20.2 -> 3i -3i -> 1030.2 -> -3i
4i -> 20.0 -> 4i -4i -> 1020.0 -> -4i
5i -> 30.2 -> 5i -5i -> 1020.2 -> -5i
6i -> 30.0 -> 6i -6i -> 1010.0 -> -6i
7i -> 103000.2 -> 7i -7i -> 1010.2 -> -7i
8i -> 103000.0 -> 8i -8i -> 1000.0 -> -8i
9i -> 103010.2 -> 9i -9i -> 1000.2 -> -9i
10i -> 103010.0 -> 10i -10i -> 2030.0 -> -10i
11i -> 103020.2 -> 11i -11i -> 2030.2 -> -11i
12i -> 103020.0 -> 12i -12i -> 2020.0 -> -12i
13i -> 103030.2 -> 13i -13i -> 2020.2 -> -13i
14i -> 103030.0 -> 14i -14i -> 2010.0 -> -14i
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre>
{{out}}
From the OEIS Wiki [http://oeis.org/wiki/Quater-imaginary_base].
<pre> 0 -> 0 -> 0 100 -> -4 -> 100 200 -> -8 -> 200 300 -> -12 -> 300
1 -> 1 -> 1 101 -> -3 -> 101 201 -> -7 -> 201 301 -> -11 -> 301
2 -> 2 -> 2 102 -> -2 -> 102 202 -> -6 -> 202 302 -> -10 -> 302
3 -> 3 -> 3 103 -> -1 -> 103 203 -> -5 -> 203 303 -> -9 -> 303
10 -> 2i -> 10 110 -> -4+2i -> 110 210 -> -8+2i -> 210 310 -> -12+2i -> 310
11 -> 1+2i -> 11 111 -> -3+2i -> 111 211 -> -7+2i -> 211 311 -> -11+2i -> 311
12 -> 2+2i -> 12 112 -> -2+2i -> 112 212 -> -6+2i -> 212 312 -> -10+2i -> 312
13 -> 3+2i -> 13 113 -> -1+2i -> 113 213 -> -5+2i -> 213 313 -> -9+2i -> 313
20 -> 4i -> 20 120 -> -4+4i -> 120 220 -> -8+4i -> 220 320 -> -12+4i -> 320
21 -> 1+4i -> 21 121 -> -3+4i -> 121 221 -> -7+4i -> 221 321 -> -11+4i -> 321
22 -> 2+4i -> 22 122 -> -2+4i -> 122 222 -> -6+4i -> 222 322 -> -10+4i -> 322
23 -> 3+4i -> 23 123 -> -1+4i -> 123 223 -> -5+4i -> 223 323 -> -9+4i -> 323
30 -> 6i -> 30 130 -> -4+6i -> 130 230 -> -8+6i -> 230 330 -> -12+6i -> 330
31 -> 1+6i -> 31 131 -> -3+6i -> 131 231 -> -7+6i -> 231 331 -> -11+6i -> 331
32 -> 2+6i -> 32 132 -> -2+6i -> 132 232 -> -6+6i -> 232 332 -> -10+6i -> 332
33 -> 3+6i -> 33 133 -> -1+6i -> 133 233 -> -5+6i -> 233 333 -> -9+6i -> 333</pre>
 
=={{header|Sidef}}==