Scrapbook-0.4.0: code examples
Copyright© Frank Jung 2020
LicenseGPL-3
Safe HaskellSafe-Inferred
LanguageHaskell2010

PolyDivisors

Description

Find poly-divisible numbers such that we use digits from 1 to 9, and the first n digits are modulo n for all digits in the number.

I first read about puzzle in Matt Parkers book, Things to make and do in the fourth dimension.

The poly divisors listed here are in descending order of performance. i.e. isPolyMod better than isPolyMod' which is better than isPolyMod''

Method

  • convert string input to int
  • permute a list of digits 1..9
  • filter modulo (length) for length digits

Exploration

Investigate how to use a fold instead of recursion:

>>> x = 123456789
>>> n = length (show x)
>>> λ> map (\p -> x `div` 10^p ) [0..n-1]
[123456789,12345678,1234567,123456,12345,1234,123,12,1]
>>> λ> f x = let n = length (show x) in x `mod` n == 0
>>> λ> f 12
True
>>> λ> foldr (\x -> (&&) (f x)) True xs
False

Examples

Load in GHCi:

>>> $ cabal repl
>>> λ> :load PolyDivisors
>>> λ> findPolyDiv 123
[123,321]
>>> λ> findPolyDiv 123456
[123654,321654]
>>> λ> findPolyDiv 123456789
[381654729]
Synopsis

Documentation

findPolyDiv :: Int -> [Int] Source #

Find poly-divisor of input string.

isPolyMod :: Int -> Bool Source #

isPolyMod: Test number is modulo n ... 1.

isPolyMod' :: Int -> Bool Source #

isPolyMod'': Test number is modulo n ... 1.

isPolyMod'' :: Int -> Bool Source #

isPolyMod'': Test number is modulo n ... 1.