Imaginary base numbers: Difference between revisions
Content deleted Content added
Line 1,085:
=={{header|Haskell}}==
<lang Haskell>
import Data.Maybe
import Data.List
import Data.Complex
Line 1,092 ⟶ 1,093:
base = 0 :+ 2
quotRemPositive
let (q, r) = quotRem a b in
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r)
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10)
shiftRight n = let (l, h) = (last n, init n) in
if (l == '0') then h else h ++ "." ++ [l]
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a')))
fromQItoComplex num b =
let dot = fromMaybe (length num) (elemIndex '.' num) in
Line 1,116 ⟶ 1,112:
(delete '.' num)
euclidEr
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l)
fromIntToQI 0 = [0]
fromIntToQI x = tail (euclidEr x (floor $ realPart (base^^2)) [])
getCuid c = (imagPart c)*(floor $ imagPart (-base))
qizip c = let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) in
let m = min (length r) (length i) in
Line 1,133 ⟶ 1,125:
++ reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
fromComplexToQI c = shiftRight (map intToDigitQI (qizip c))
|