Difference between revisions of "99 questions/31 to 41"

From HaskellWiki
Jump to navigation Jump to search
m
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
This is part of [[H-99:_Ninety-Nine_Haskell_Problems|Ninety-Nine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/ Ninety-Nine Prolog Problems].
 
This is part of [[H-99:_Ninety-Nine_Haskell_Problems|Ninety-Nine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/ Ninety-Nine Prolog Problems].
   
== Arithmetic ==
 
   
 
== Problem 31 ==
 
== Problem 31 ==
  +
<div style="border-bottom:1px solid #eee">(**) Determine whether a given integer number is prime. <span style="float:right"><small>[[99 questions/Solutions/31|Solutions]]</small></span>
 
  +
</div>
(**) Determine whether a given integer number is prime.
 
  +
&nbsp;<br>
   
 
Example:
 
Example:
Line 19: Line 19:
   
 
<haskell>
 
<haskell>
P31> isPrime 7
+
λ> isPrime 7
 
True
 
True
 
</haskell>
 
</haskell>
   
[[99 questions/Solutions/31 | Solutions]]
 
   
 
 
 
== Problem 32 ==
 
== Problem 32 ==
  +
<div style="border-bottom:1px solid #eee">(**) Determine the greatest common divisor of two positive integer numbers. <span style="float:right"><small>[[99 questions/Solutions/32|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
(**) Determine the greatest common divisor of two positive integer numbers. Use [http://en.wikipedia.org/wiki/Euclidean_algorithm Euclid's algorithm].
+
Use [http://en.wikipedia.org/wiki/Euclidean_algorithm Euclid's algorithm].
   
 
Example:
 
Example:
Line 40: Line 42:
   
 
<haskell>
 
<haskell>
[myGCD 36 63, myGCD (-3) (-6), myGCD (-3) 6]
+
λ> [myGCD 36 63, myGCD (-3) (-6), myGCD (-3) 6]
 
[9,3,3]
 
[9,3,3]
 
</haskell>
 
</haskell>
   
[[99 questions/Solutions/32 | Solutions]]
 
   
 
== Problem 33 ==
 
== Problem 33 ==
  +
<div style="border-bottom:1px solid #eee">(*) Determine whether two positive integer numbers are coprime. <span style="float:right"><small>[[99 questions/Solutions/33|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
(*) Determine whether two positive integer numbers are coprime. Two numbers are coprime if their greatest common divisor equals 1.
+
Two numbers are coprime if their greatest common divisor equals 1.
   
 
Example:
 
Example:
Line 60: Line 64:
   
 
<haskell>
 
<haskell>
* coprime 35 64
+
λ> coprime 35 64
 
True
 
True
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/33 | Solutions]]
 
   
 
 
 
== Problem 34 ==
 
== Problem 34 ==
  +
<div style="border-bottom:1px solid #eee">(**) Calculate Euler's totient function phi(m). <span style="float:right"><small>[[99 questions/Solutions/34|Solutions]]</small></span>
 
  +
</div>
(**) Calculate Euler's totient function phi(m).
 
  +
&nbsp;<br>
   
 
Euler's so-called totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m.
 
Euler's so-called totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m.
Line 85: Line 88:
   
 
<haskell>
 
<haskell>
* totient 10
+
λ> totient 10
 
4
 
4
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/34 | Solutions]]
 
   
   
 
== Problem 35 ==
 
== Problem 35 ==
  +
<div style="border-bottom:1px solid #eee">(**) Determine the prime factors of a given positive integer. <span style="float:right"><small>[[99 questions/Solutions/35|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
(**) Determine the prime factors of a given positive integer. Construct a flat list containing the prime factors in ascending order.
+
Construct a flat list containing the prime factors in ascending order.
   
 
Example:
 
Example:
Line 106: Line 110:
   
 
<haskell>
 
<haskell>
> primeFactors 315
+
λ> primeFactors 315
 
[3, 3, 5, 7]
 
[3, 3, 5, 7]
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/35 | Solutions]]
 
   
 
 
 
== Problem 36 ==
 
== Problem 36 ==
  +
<div style="border-bottom:1px solid #eee">(**) Determine the prime factors and their multiplicities of a given positive integer. <span style="float:right"><small>[[99 questions/Solutions/36|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
 
Construct a list containing each prime factor and its multiplicity.
(**) Determine the prime factors of a given positive integer.
 
 
Construct a list containing the prime factors and their multiplicity.
 
   
 
Example:
 
Example:
Line 129: Line 132:
   
 
<haskell>
 
<haskell>
*Main> prime_factors_mult 315
+
λ> prime_factors_mult 315
 
[(3,2),(5,1),(7,1)]
 
[(3,2),(5,1),(7,1)]
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/36 | Solutions]]
 
   
 
 
 
== Problem 37 ==
 
== Problem 37 ==
  +
<div style="border-bottom:1px solid #eee">(**) Calculate Euler's totient function phi(m) (improved). <span style="float:right"><small>[[99 questions/Solutions/37|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
 
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:
(**) 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>
Line 150: Line 152:
 
Note that a ** b stands for the b'th power of a.
 
Note that a ** b stands for the b'th power of a.
   
<i>Note</i>: Actually, the official problems show this as a sum, but it should be a product.
 
 
[[99 questions/Solutions/37 | Solutions]]
 
   
 
== Problem 38 ==
 
== Problem 38 ==
  +
<div style="border-bottom:1px solid #eee">(*) Compare the two methods of calculating Euler's totient function. <span style="float:right"><small>(no solution required)</small></span>
  +
</div>
  +
&nbsp;<br>
   
 
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.
(*) 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)
 
   
 
 
 
== Problem 39 ==
 
== Problem 39 ==
  +
<div style="border-bottom:1px solid #eee">(*) A list of prime numbers in a given range. <span style="float:right"><small>[[99 questions/Solutions/39|Solutions]]</small></span>
 
  +
</div>
(*) A list of prime numbers.
 
  +
&nbsp;<br>
   
 
Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
 
Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
Line 172: Line 171:
   
 
<haskell>
 
<haskell>
P29> primesR 10 20
+
λ> primesR 10 20
 
[11,13,17,19]
 
[11,13,17,19]
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/39 | Solutions]]
 
   
   
 
== Problem 40 ==
 
== Problem 40 ==
  +
<div style="border-bottom:1px solid #eee">(**) Goldbach's conjecture. <span style="float:right"><small>[[99 questions/Solutions/40|Solutions]]</small></span>
 
  +
</div>
(**) Goldbach's conjecture.
 
  +
&nbsp;<br>
   
 
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.
 
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.
Line 195: Line 193:
   
 
<haskell>
 
<haskell>
*goldbach 28
+
λ> goldbach 28
 
(5, 23)
 
(5, 23)
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/40 | Solutions]]
 
   
   
 
== Problem 41 ==
 
== Problem 41 ==
  +
<div style="border-bottom:1px solid #eee">(**) A list of even numbers and their Goldbach compositions in a given range. <span style="float:right"><small>[[99 questions/Solutions/41|Solutions]]</small></span>
  +
</div>
  +
&nbsp;<br>
   
(**) Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
+
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.
 
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.
Line 228: Line 227:
   
 
<haskell>
 
<haskell>
*Exercises> goldbachList 9 20
+
λ> goldbachList 9 20
 
[(3,7),(5,7),(3,11),(3,13),(5,13),(3,17)]
 
[(3,7),(5,7),(3,11),(3,13),(5,13),(3,17)]
*Exercises> goldbachList' 4 2000 50
+
λ> goldbachList' 4 2000 50
 
[(73,919),(61,1321),(67,1789),(61,1867)]
 
[(73,919),(61,1321),(67,1789),(61,1867)]
 
</haskell>
 
</haskell>
 
[[99 questions/Solutions/41 | Solutions]]
 
   
   

Latest revision as of 02:30, 11 June 2023


This is part of Ninety-Nine Haskell Problems, based on Ninety-Nine Prolog Problems.


Problem 31

(**) Determine whether a given integer number is prime. Solutions

 

Example:

* (is-prime 7)
T

Example in Haskell:

λ> isPrime 7
True


Problem 32

(**) Determine the greatest common divisor of two positive integer numbers. Solutions

 

Use Euclid's algorithm.

Example:

* (gcd 36 63)
9

Example in Haskell:

λ> [myGCD 36 63, myGCD (-3) (-6), myGCD (-3) 6]
[9,3,3]


Problem 33

(*) Determine whether two positive integer numbers are coprime. Solutions

 

Two numbers are coprime if their greatest common divisor equals 1.

Example:

* (coprime 35 64)
T

Example in Haskell:

λ> coprime 35 64
True


Problem 34

(**) Calculate Euler's totient function phi(m). Solutions

 

Euler's so-called 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:

* (totient-phi 10)
4

Example in Haskell:

λ> totient 10
4


Problem 35

(**) Determine the prime factors of a given positive integer. Solutions

 

Construct a flat list containing the prime factors in ascending order.

Example:

* (prime-factors 315)
(3 3 5 7)

Example in Haskell:

λ> primeFactors 315
[3, 3, 5, 7]


Problem 36

(**) Determine the prime factors and their multiplicities of a given positive integer. Solutions

 

Construct a list containing each prime factor and its multiplicity.

Example:

* (prime-factors-mult 315)
((3 2) (5 1) (7 1))

Example in Haskell:

λ> prime_factors_mult 315
[(3,2),(5,1),(7,1)]


Problem 37

(**) Calculate Euler's totient function phi(m) (improved). Solutions

 

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.


Problem 38

(*) Compare the two methods of calculating Euler's totient function. (no solution required)

 

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.


Problem 39

(*) A list of prime numbers in a given range. Solutions

 

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

Example in Haskell:

λ> primesR 10 20
[11,13,17,19]


Problem 40

(**) Goldbach's conjecture. Solutions

 

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)


Problem 41

(**) A list of even numbers and their Goldbach compositions in a given range. Solutions

 

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:

* (goldbach-list 9 20)
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
* (goldbach-list 1 2000 50)
992 = 73 + 919
1382 = 61 + 1321
1856 = 67 + 1789
1928 = 61 + 1867

Example in Haskell:

λ> goldbachList 9 20
[(3,7),(5,7),(3,11),(3,13),(5,13),(3,17)]
λ> goldbachList' 4 2000 50
[(73,919),(61,1321),(67,1789),(61,1867)]