45 lines
2.2 KiB
Haskell
45 lines
2.2 KiB
Haskell
-- widened borrow of a particular repeated amount
|
|
-- i.e., for borrow2 2, the borrow is 22 = 100
|
|
borrow2 b | b == 1 = error "Cannot borrow 1: implies positional symbol zero disallowed"
|
|
| otherwise = borrow' [] b
|
|
where borrow' zs b (x:y:z:xs) | abs x >= b = zipUp ys zs
|
|
| otherwise = borrow' (x:zs) b (y:z:xs)
|
|
where ys = r : y-x+r : z+q : xs
|
|
(q, r) = x `quotRem` b
|
|
zipUp = foldl (flip (:))
|
|
|
|
--degenerate `borrow2 1 `that remedies inadequacies in below for 1, i.e., the borrow is 11 = 100
|
|
--this system is VERY bad because we invoke the place value '0' to expand into
|
|
--borrow1' zs (x:y:z:xs) | abs x > 1 = zipUp ys zs
|
|
-- | otherwise = borrow1' (x:zs) (y:z:xs)
|
|
-- where ys = (signum x):(y-x+signum x):z+(x-1):xs
|
|
|
|
-- truncate the adic expansion to n digits, for a system where borrows are 2 wide and both b
|
|
truncadic b n = take n . (!! n) . iterate (borrow2 b)
|
|
|
|
--same but, produce a list of expansions, taking modulo m in between borrows
|
|
truncadicmod b m n = map (take n) . take n . iterate (map (`rem` m) . borrow2 2)
|
|
|
|
-- find moduli starting from s where truncations agree
|
|
-- caveat: the last of truncadicmod is not guaranteed to exhaust terms outside alphabet
|
|
-- i.e., for b = 2, the alphabet {-1, 0, 1}
|
|
findaccurate b n s xs = map fst $ filter ((==good) . snd) bads
|
|
where good = truncadic b n xs
|
|
bads = map (\m -> (,) m $ last $ truncadicmod b m n xs) [s..]
|
|
|
|
--given an amount of digits `n`, a 2-wide borrow `b`, and a canonical representation of `b`
|
|
--construct integer multiples of `b`
|
|
evens n b = ([0]:) . map (take n) . iterate addb
|
|
--add b to an adic expansion
|
|
where addb = (!! n ) . iterate (borrow2 b) . (b:) . tail
|
|
|
|
--first 200 digits of cendree-adic expansions of even numbers
|
|
adics = evens 200 2 $ 0:0:cycle [1,-1]
|
|
adic2 = adics !! 1
|
|
adic4 = adics !! 2
|
|
adic4' = truncadic 2 200 $ (++repeat 0) $ map (*2) adic2
|
|
|
|
-- note: `truncadic 2 n $ 4:repeat 0` is the aggressive application of the carry to 4
|
|
-- but this could be done better since there are only 3 terms in the head, and no higher
|
|
-- series terms get in the way; the remainders would be emitted while the thunk continued
|