I will collect the solution of different mathematical problems addressed in a variety of programming languages.
This is a problem proposed for undergraduate students of Mathematical Sciences at the University of Buenos Aires (FCEyN, UBA). The problem has to be solved in the haskell programming language. Haskell is a purely funcional programming language, that is specially suitable for mathematical purposes.
Exercise
Write a function factorizar :: Integer -> [(Integer,Integer)] that receives as a parameter an integer n >= 1 and returns
a list representing the factorization of n. Formally, the list has to be structured like
[(p1; k1), (p2, k2),...,(pm, km)] such that
- p1, p2, . . . , pm and k1, k2, . . . ,km are positive integers.
- p1, p2, . . . , pm are primes.
- p1 < p2 < . . . < pm.
- p1k1 x p2k2 x . . . x pmkm = n
For the value 1, the answer is an empty list, because it has no prime divisors.
Examples of the factorizar function
| Query in terminal | Should return |
| factorizar 6 | [(2,1),(3,1)] |
| factorizar 7 | [(7,1)] |
| factorizar 81 | [(3,4)] |
| factorizar 1176 | [(2,3),(3,1),(7,2)] |
| factorizar 1 | [ ] |
| factorizar 0 | *** Exception: ... |
| factorizar -1 | *** Exception: ... |
References