Difference between revisions of "Euler problems/181 to 190"

From HaskellWiki
Jump to navigation Jump to search
m
Line 2: Line 2:
 
Investigating in how many ways objects of two different colours can be grouped.
 
Investigating in how many ways objects of two different colours can be grouped.
   
  +
{{sect-stub}}
Solution: This was my code, published here without my permission nor any attribution, shame on whoever put it here. [[User:Daniel.is.fischer|Daniel.is.fischer]]
 
   
 
== [http://projecteuler.net/index.php?section=problems&id=182 Problem 182] ==
 
== [http://projecteuler.net/index.php?section=problems&id=182 Problem 182] ==
Line 9: Line 9:
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
fun a1 b1 =
+
fun a1 b1 = sum [ e | e <- [2..a*b-1],
  +
gcd e (a*b) == 1,
sum [ e |
 
e <- [2..a*b-1],
+
gcd (e-1) a == 2,
gcd e (a*b) == 1,
+
gcd (e-1) b == 2 ]
gcd (e-1) a == 2,
+
where a = a1-1
gcd (e-1) b == 2
+
b = b1-1
  +
]
 
 
problem_182 = fun 1009 3643
where
 
a=a1-1
 
b=b1-1
 
problem_182=fun 1009 3643
 
 
</haskell>
 
</haskell>
   

Revision as of 22:24, 24 February 2008

Problem 181

Investigating in how many ways objects of two different colours can be grouped.

Problem 182

RSA encryption.

Solution:

fun a1 b1 = sum [ e | e <- [2..a*b-1],
                      gcd e (a*b) == 1,
                      gcd (e-1) a == 2,
                      gcd (e-1) b == 2 ]
  where a = a1-1
        b = b1-1

problem_182 = fun 1009 3643

Problem 183

Maximum product of parts.

Solution:

-- Does the decimal expansion of p/q terminate?
terminating p q = 1 == reduce [2,5] (q `div` gcd p q)
        where reduce   []   n = n
              reduce (x:xs) n | n `mod` x == 0 = reduce (x:xs) (n `div` x)
                              | otherwise      = reduce xs n

-- The expression (round $ fromIntegral n / e) computes the integer k
-- for which (n/k)^k is at a maximum. Also note that, given a rational number
-- r and a natural number k, the decimal expansion of r^k terminates if
-- and only if the decimal expansion of r does.
answer = sum [if terminating n (round $ fromIntegral n / e) then -n else n
                | n <- [5 .. 10^4]]
        where e = exp 1

main = print answer