Maybe we should keep the lists of combinations around instead of the base values; that would get us out of the divMod business, though OTOH it means hanging on to those lists; as it stands, I bet that they're being garbage collected.
The problem is that the bitmap moves us back to O(n), where n is the number of digits, rather than a fixed amount of work (because there's a fixed upper bound on the number of nonzero digits). We could roll our own bit fields, and stuff up to three bitsIn (n `div` 2) values into an Int or an Integer (actually, Haskell has Int8, Int16, Int32, and Int64 types to choose from; 21 bits a shot would allow for up to two million digits, times two for both halves of the palindrome--that's a lot bigger than the second official data set needs), but that seems kind of cheesy.
I'll have to think about that some more. In the meantime, I restructured the code a little bit to make the code that generates Ys more clearly parallel to the code that just counts them. The counting code:
numNDigitYs :: Int -> Int
numNDigitYs 1 = 3
numNDigitYs n = numTwoTwos n + numOneTwos n + numNoTwos n
where numTwoTwos n = if even n then 1 else 2
numOneTwos n = if even n then 0 else n `div` 2
numNoTwos n = if even n then s else 2 * s
where h = n `div` 2 - 1
s = sum [h `choose` i | i <- [0..min h 3]]
The generating code:
nDigitYs :: Int -> [Integer]
nDigitYs 1 = [1,2,3]
nDigitYs n = sort (noTwos n ++ oneTwos n ++ twoTwos n)
where twoTwos n
| even n = [twoTwosNoOnes]
| otherwise = [twoTwosNoOnes,
twoTwosNoOnes + tenToThe (n `div` 2)]
where twoTwosNoOnes = 2 * tenToThe (n - 1) + 2
oneTwos n
| even n = []
| otherwise = map (+ 2 * tenToThe halfN)
(justOnes n (min 1 (halfN - 1)))
where halfN = n `div` 2
noTwos n
| even n = base
| otherwise = concat [[p, p + tenToThe halfN] | p <- base]
where halfN = n `div` 2
base = justOnes n (min 3 (halfN - 1))
Yeah, I guess that shows I'm seriously at the point of diminishing returns.
random notes and thoughts, mostly about Haskell these days, of a rather past middle-aged programmer
Subscribe to:
Post Comments (Atom)
Riddler Classic, May 23, 2020—Holy Mackerel!
Another one using Peter Norvig's word list . It turns out that the word "mackerel" has a curious property: there is exactly ...
-
Back in the Cretaceous era I worked at the University of Oklahoma as a student assistant at Remote 1. OU was a [shudder] IBM big iron shop a...
-
You've probably heard about how the notion of sum types (e.g. Algol 68 union s, Rust enum s, Haskell type s) and product types (e.g. tup...
-
Verbal Abuse as Entertainment When I grew up, my parents always told me that there was a sort of person who needed to tear down others t...
No comments:
Post a Comment