Haskell - Computations (2) P68540


Statement
 

pdf   zip

These problems are inspired in some of the problems from Project Euler. You can find them at https://projecteuler.net.

  1. The sum of the squares of the first 10 natural numbers is 12+22++102=3851^2+2^2+\dots+10^2=385. The square of the sum of the first 10 natural numbers is (1+2++10)2=552=3025(1+2+\dots+10)^2=55^2=3025. Therefore, the difference between the sum of the squares of the first 10 natural numbers and the square of the sum of the first 10 natural numbers is 3025385=26403025 - 385 = 2640.

    Write a function diffSqrs :: Integer -> Integer that, given a natural nn, returns the difference between the sum of the squares of the first nn natural numbers and the square of the sum of the first nn natural numbers.

  2. A Pythagorean triplet are three natural numbers (a,b,c)(a,b,c) such that a2+b2=c2a^2+b^2=c^2. Write a function pythagoreanTriplets :: Int -> [(Int, Int, Int)] that, given a natural n1n\ge1, returns the list of all Pythagorean tripletes that add up to nn. Each triplet must be sorted in such a way that abca\le b\le c and the list must be sorted according to aa.

  3. Write a function tartaglia :: [[Integer]] that returns an infinite list with the rowss of the Tartaglia’s triangle (also known as Pascal’s triangle).

  4. Write a function sumDigits :: Integer -> Integer that returns the sum of all digits of a natural number. Use high order functions rather than recursion.

  5. Write a function digitalRoot :: Integer -> Integer that returns the digital root of a natural number. Use high order functions rather than recursion.

Scoring

Each function scores 20 points.

Public test cases
  • Input

    diffSqrs 10
    map pythagoreanTriplets [3,12,84]
    take 5 tartaglia
    sumDigits 32768
    digitalRoot 65536
    

    Output

    2640
    [[],[(3,4,5)],[(12,35,37),(21,28,35)]]
    [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
    26
    7
    
  • Information
    Author
    Jordi Petit
    Language
    English
    Translator
    Jordi Petit
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    Haskell
    User solutions
    Haskell