[Haskell-cafe] complex multiplication function in Haskell
Richard A. O'Keefe
ok at cs.otago.ac.nz
Thu Apr 27 00:04:15 UTC 2017
> On 27/04/2017, at 12:02 AM, Mahmoud Murad <mahmoudmurad92 at gmail.com> wrote:
>
> Hi there,
> The number 354162 has 6 digits and contains all digits from 1 to 6, and it can be considered as a multiplication of 3 * 54 = 162.
> so what I need is a function that will take all the real number and return the number that can be split as above,
> 354162 .. 3 * 54 = 162
> I have no idea how to do such a thing! any help!
Start by spelling out just what the conditions are.
- Are you looking specifically for 6-digit numbers or could it be more?
(Because you say "ALL the real number[s]".
- Are you sure you want REAL numbers and not INTEGERS?
One way to read what you have written is
- find a permutation [a,b,c,d,e,f] of [1,2,3,4,5,6]
- such that a*(b*10+c) == (d*10+e)*10+f.
There are only 6! = 720 permutations of six digits,
so not a lot to check. For a computer program, I'd just
code that.
We can restructure this as
- find three different digits 1 <= a,b,c <= 6
- compute def = a*(b*10+c)
- check that the digits of def are all 1..6 and all
different and different from a,b,c.
There are 120 cases to check here.
As it happens, Data.List provides permutations
Prelude> import Data.List
Prelude Data.List> permutations [1..3]
[[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]
Prelude Data.List> length (permutations [1..6])
720
You can combine this with a list comprehension to get the numbers
you want.
More information about the Haskell-Cafe
mailing list