martes, 22 de marzo de 2016

LINQ Recipe No. 2-2: Mathematics and Statistics - How to Generate Pythagorean Triples

Contents

1. Introductin
2. Key Words
3. Problem
4. Solution
5. Discussion
5.1 Range generator function
5.2 Select projector function
5.3 Babylonian method
6. Practice: Pythagorean Triples with the Babylonians Generator Method
7. Conclusions
8. Literature & Links

1. Introduction

In this new LINQ recipe we are going to learn how to generate Pythagorean triples: a set of three integers which constitutes the sides of a right-triangle. For this purpose, it will require the usage of the Range generator function and the Select projector function. Inclusive, an anonymous type will be created to structure the names of the sides of triangle: Length, Height, and Hypotenuse.


(LINQPad must be installed on our system in order to code the examples and exercises.)

2. Key Words

  • Anonymous type
  • Generator function
  • Lambda expression
  • LINQ
  • Projector function
  • Pythagorean triple

3. Problem

Write an LINQ sentence to generate Pythagorean triples.

4. Solution

The solution is based on two general approaches: one technical and another one based on an ancient mathematical method:
  • Technical:
    • Range generator function
    • Select projector function
  • Method:
    • Babylonians Pythagorean triples generator method

5. Discussion

5.1 Range generator function

The Range ("Enumerable.Range Method", 2016) generator function lets us generate a sequence of integral numbers. The sequence is generated within a specified range.

A very simple example for this generator function is 

Enumerable.Range(1, 10)

This code generates the integral numbers between 1 and 10:
Range generator function example
Illustration 1. Range generator function example.

5.2 Select projector function

How to modify, for example, the elements generated by the Range projector function? The Select ("Enumerable.Select Method", 2016) projector function allows us to project each elements of a given sequence into a new representation or form.


The example presented in the previous section can be extended to something like this:

Enumerable.Range(1, 10).Select( num => num * num)


Once it has been executed, we get 
Select generator function example
Illustration 2. Select generator function example.

Note that each integral number from 1 to 10 has been squared using the lambda expression num => num * num inside the Select projector function.

5.3 Babylonians method

Maybe, you know that the most common Pythagorean triple is {3, 4, 5}, and one simple method to generate subsequent triples consists in multiplying each element of previous triple by an arbitrary k integer number: {3k, 4k, 5k}. But a more sophisticated mechanism exists for generating Pythagorean triples; each sides of the triangle is computed with this general formula:
  • Base: c * c - 1
  • Height: 2 * c
  • Hypotenuse: c * c + 1
We must take into account that the c variable is an integer type instance which must be greater than or equal to 2.

This method was defined by the Babylonian mathematicians (Mukherjee, 2014) and it can be written in a LINQ expression with little code, making it readable and expressive.

6. Practice: Pythagorean Triples with Babylonians Generator Method

For this example, we are going to generate the first ten Pythagorean triples ranging from 2.

The LINQ code for this practical example is:

Enumerable.Range(2, 10)
.Select(c => new {
Length = 2 * c,
Height = c * c - 1,
Hypotenuse = c * c + 1
}).Dump("Pythagorean Triples")

The first step consists in generating 10 integers starting from 2: Range(2, 10). Once we have the 10 integers we transform them according to the expressions assigned to the attributes of the anonymous type:
  • Length = 2 * c
  • Height = c * c - 1
  • Hypotenuse = c * c + 1
What is the result of this LINQ expression:
LINQ expression to generate Pythagorean triples
Illustration 4. LINQ expression to generate Pythagorean triples.
We must note the simplicity for generating the sequence of Pythagorean triples. We also can write an implementation in C# without using LINQ standard operators, but it, possibly, will be more extended and less readable.

7. Conclusions

We have learned more about the generator and projector functions: Range and Select. These functions are extremely useful to code readable and simple implementation code as we have seen in the previous section: the generation of Pythagorean triples using the Babylonians method.

In this next LINQ recipe we will centered in finding a weighted sum -a vector dot product application-.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Enumerable.Range Method (System.Linq) (2016, March 22). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.range%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396
Enumerable.Select Method (System.Linq) (2016, March 22). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.select(v=vs.110).aspx


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.