99 questions/31 to 41
From HaskellWiki
(→Problem 37: delete a note) 

(42 intermediate revisions by 16 users not shown)  
Line 1:  Line 1:  
__NOTOC__ 
__NOTOC__ 

−  These are Haskell translations of [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L99_NinetyNine_Lisp_Problems.html Ninety Nine Lisp Problems]. 
+  This is part of [[H99:_NinetyNine_Haskell_ProblemsNinetyNine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p99/ NinetyNine Prolog Problems]. 
−  If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields. 
+  == Arithmetic == 
−  
== Problem 31 == 
== Problem 31 == 

−  Determine whether a given integer number is prime. 
+  (**) Determine whether a given integer number is prime. 
−  <pre> 

Example: 
Example: 

+  
+  <pre> 

* (isprime 7) 
* (isprime 7) 

T 
T 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  P31> isPrime 7 

−  True 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  isPrime :: Integral a => a > Bool 
+  P31> isPrime 7 
−  isPrime p = all (\n > p `mod` n /= 0 ) $ takeWhile (\n > n*n <= x) [2..] 
+  True 
</haskell> 
</haskell> 

−  Well, a natural number p is a prime number iff no natural number n with n >= 2 and n^2 <= p is a divisor of p. That's exactly what is implemented: we take the list of all integral numbers starting with 2 as long as their square is at most p and check that for all these n there is a remainder concerning the division of p by n. 
+  [[99 questions/Solutions/31  Solutions]] 
+  
== Problem 32 == 
== Problem 32 == 

−  <Problem description> 
+  (**) Determine the greatest common divisor of two positive integer numbers. Use [http://en.wikipedia.org/wiki/Euclidean_algorithm Euclid's algorithm]. 
−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (gcd 36 63) 
+  9 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  [myGCD 36 63, myGCD (3) (6), myGCD (3) 6] 
+  [9,3,3] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/32  Solutions]] 
−  +  
== Problem 33 == 
== Problem 33 == 

−  <Problem description> 
+  (*) Determine whether two positive integer numbers are coprime. Two numbers are coprime if their greatest common divisor equals 1. 
−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (coprime 35 64) 
+  T 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  * coprime 35 64 
+  True 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/33  Solutions]] 
+  
== Problem 34 == 
== Problem 34 == 

−  <Problem description> 
+  (**) Calculate Euler's totient function phi(m). 
+  
+  Euler's socalled totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m. 

+  
+  Example: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1. 

−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (totientphi 10) 
+  4 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  * totient 10 
+  4 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/34  Solutions]] 
−  +  
+  
== Problem 35 == 
== Problem 35 == 

−  <Problem description> 
+  (**) Determine the prime factors of a given positive integer. Construct a flat list containing the prime factors in ascending order. 
−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (primefactors 315) 
+  (3 3 5 7) 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  > primeFactors 315 
+  [3, 3, 5, 7] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/35  Solutions]] 
+  
== Problem 36 == 
== Problem 36 == 

−  <Problem description> 
+  (**) Determine the prime factors of a given positive integer. 
+  
+  Construct a list containing the prime factors and their multiplicity. 

−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (primefactorsmult 315) 
+  ((3 2) (5 1) (7 1)) 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  *Main> prime_factors_mult 315 
+  [(3,2),(5,1),(7,1)] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/36  Solutions]] 
+  
== Problem 37 == 
== Problem 37 == 

−  <Problem description> 
+  (**) Calculate Euler's totient function phi(m) (improved). 
+  
+  See problem 34 for the definition of Euler's totient function. If the list of the prime factors of a number m is known in the form of problem 36 then the function phi(m) can be efficiently calculated as follows: Let ((p1 m1) (p2 m2) (p3 m3) ...) be the list of prime factors (and their multiplicities) of a given number m. Then phi(m) can be calculated with the following formula: 

<pre> 
<pre> 

−  Example: 
+  phi(m) = (p1  1) * p1 ** (m1  1) * 
−  <example in lisp> 
+  (p2  1) * p2 ** (m2  1) * 
−  +  (p3  1) * p3 ** (m3  1) * ... 

−  Example in Haskell: 

−  <example in Haskell> 

</pre> 
</pre> 

−  Solution: 
+  Note that a ** b stands for the b'th power of a. 
−  <haskell> 
+  
−  <solution in haskell> 
+  [[99 questions/Solutions/37  Solutions]] 
−  </haskell> 

−  <description of implementation> 

−  
== Problem 38 == 
== Problem 38 == 

−  <Problem description> 
+  (*) Compare the two methods of calculating Euler's totient function. 
−  <pre> 
+  Use the solutions of problems 34 and 37 to compare the algorithms. Take the number of reductions as a measure for efficiency. Try to calculate phi(10090) as an example. 
−  Example: 
+  
−  <example in lisp> 
+  (no solution required) 
+  
+  
+  == Problem 39 == 

+  
+  (*) A list of prime numbers. 

+  
+  Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range. 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 
+  P29> primesR 10 20 
+  [11,13,17,19] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/39  Solutions]] 
−  
−  == Problem 39 == 

−  <Problem description> 

−  <pre> 
+  == Problem 40 == 
+  
+  (**) Goldbach's conjecture. 

+  
+  Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers (much larger than we can go with our Prolog system). Write a predicate to find the two prime numbers that sum up to a given even integer. 

+  
Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (goldbach 28) 
+  (5 23) 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  *goldbach 28 
+  (5, 23) 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/40  Solutions]] 
−  
−  == Problem 40 == 

−  <Problem description> 

−  <pre> 
+  == Problem 41 == 
+  
+  (**) Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition. 

+  
+  In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000. 

+  
Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (goldbachlist 9 20) 
+  10 = 3 + 7 

+  12 = 5 + 7 

+  14 = 3 + 11 

+  16 = 3 + 13 

+  18 = 5 + 13 

+  20 = 3 + 17 

+  * (goldbachlist 1 2000 50) 

+  992 = 73 + 919 

+  1382 = 61 + 1321 

+  1856 = 67 + 1789 

+  1928 = 61 + 1867 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  *Exercises> goldbachList 9 20 
+  [(3,7),(5,7),(3,11),(3,13),(5,13),(3,17)] 

+  *Exercises> goldbachList' 4 2000 50 

+  [(73,919),(61,1321),(67,1789),(61,1867)] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/41  Solutions]] 
−  +  
[[Category:Tutorials]] 
[[Category:Tutorials]] 
Latest revision as of 05:06, 25 February 2012
This is part of NinetyNine Haskell Problems, based on NinetyNine Prolog Problems.
[edit] 1 Arithmetic
[edit] 2 Problem 31
(**) Determine whether a given integer number is prime.
Example:
* (isprime 7) T
Example in Haskell:
P31> isPrime 7 True
[edit] 3 Problem 32
(**) Determine the greatest common divisor of two positive integer numbers. Use Euclid's algorithm.
Example:
* (gcd 36 63) 9
Example in Haskell:
[myGCD 36 63, myGCD (3) (6), myGCD (3) 6] [9,3,3]
[edit] 4 Problem 33
(*) Determine whether two positive integer numbers are coprime. Two numbers are coprime if their greatest common divisor equals 1.
Example:
* (coprime 35 64) T
Example in Haskell:
* coprime 35 64 True
[edit] 5 Problem 34
(**) Calculate Euler's totient function phi(m).
Euler's socalled totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m.
Example: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.
Example:
* (totientphi 10) 4
Example in Haskell:
* totient 10 4
[edit] 6 Problem 35
(**) Determine the prime factors of a given positive integer. Construct a flat list containing the prime factors in ascending order.
Example:
* (primefactors 315) (3 3 5 7)
Example in Haskell:
> primeFactors 315 [3, 3, 5, 7]
[edit] 7 Problem 36
(**) Determine the prime factors of a given positive integer.
Construct a list containing the prime factors and their multiplicity.
Example:
* (primefactorsmult 315) ((3 2) (5 1) (7 1))
Example in Haskell:
*Main> prime_factors_mult 315 [(3,2),(5,1),(7,1)]
[edit] 8 Problem 37
(**) Calculate Euler's totient function phi(m) (improved).
See problem 34 for the definition of Euler's totient function. If the list of the prime factors of a number m is known in the form of problem 36 then the function phi(m) can be efficiently calculated as follows: Let ((p1 m1) (p2 m2) (p3 m3) ...) be the list of prime factors (and their multiplicities) of a given number m. Then phi(m) can be calculated with the following formula:
phi(m) = (p1  1) * p1 ** (m1  1) * (p2  1) * p2 ** (m2  1) * (p3  1) * p3 ** (m3  1) * ...
Note that a ** b stands for the b'th power of a.
[edit] 9 Problem 38
(*) Compare the two methods of calculating Euler's totient function.
Use the solutions of problems 34 and 37 to compare the algorithms. Take the number of reductions as a measure for efficiency. Try to calculate phi(10090) as an example.
(no solution required)
[edit] 10 Problem 39
(*) A list of prime numbers.
Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
Example in Haskell:
P29> primesR 10 20 [11,13,17,19]
[edit] 11 Problem 40
(**) Goldbach's conjecture.
Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers (much larger than we can go with our Prolog system). Write a predicate to find the two prime numbers that sum up to a given even integer.
Example:
* (goldbach 28) (5 23)
Example in Haskell:
*goldbach 28 (5, 23)
[edit] 12 Problem 41
(**) Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.
Example:
* (goldbachlist 9 20) 10 = 3 + 7 12 = 5 + 7 14 = 3 + 11 16 = 3 + 13 18 = 5 + 13 20 = 3 + 17 * (goldbachlist 1 2000 50) 992 = 73 + 919 1382 = 61 + 1321 1856 = 67 + 1789 1928 = 61 + 1867
Example in Haskell:
*Exercises> goldbachList 9 20 [(3,7),(5,7),(3,11),(3,13),(5,13),(3,17)] *Exercises> goldbachList' 4 2000 50 [(73,919),(61,1321),(67,1789),(61,1867)]