12345678910111213141516171819202122232425 |
- -- Author: estelendur
- -- Given a base b and a number of digits n, how many palindromic integers?
- -- What proportion of possible integers are palindromes?
- -- Can we learn anything interesting from this?
- --half :: (Integral a) => a -> a
- --half x = ceiling(x/2)
- --pals :: (Integral a) => a -> a -> a
- pals n b = (b-1)*(b^(ceiling((n-1)/2))
- --num :: (Integral a) => a -> a -> a
- num n b = (b-1)*(b^(n-1))
- prop :: (Integral a) => a -> a -> Double
- prop n b = (pals n b)/(num n b)
- -- Find out the proportion of palindromes for all n, b up to nm, bm
- test :: (Integral a) => a -> a -> String
- test 1 1 = "1 digit in base 1 gives 1 palindrome\n"
- test n 1 = n ++ " digits in base 1 gives 1 palindrome\n"
- test 1 b = "1 digit in base " ++ b ++ " gives " ++ (b-1) ++ " palindromes\n"
- test nm bm = nm ++ " digits in base " ++ bm ++ " gives " ++ (prop nm bm)*100 ++ " percent palindromes\n" ++ (test (nm-1) bm) ++ (test nm (bm-1))
|