complex multiplication function in Haskell

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

complex multiplication function in Haskell

Mahmoud Murad
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! 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: complex multiplication function in Haskell

Gregory Popovitch
Maybe just enumerate the possibilities
 
1 * 1 = 1 => 111
1 * 2 = 2 => 112
1 * 3 = 3 => 113
...
2 * 1 = 2 => 212
...


From: Haskell-Cafe [mailto:[hidden email]] On Behalf Of Mahmoud Murad
Sent: Wednesday, April 26, 2017 8:03 AM
To: [hidden email]
Subject: [Haskell-cafe] complex multiplication function in Haskell

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! 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: complex multiplication function in Haskell

Richard A. O'Keefe
In reply to this post by Mahmoud Murad

> On 27/04/2017, at 12:02 AM, Mahmoud Murad <[hidden email]> 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.





_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.