Interpreting an algorithm

3

I have to solve a problem with the following statement:

  

The houses on that street

     

Context

     

A computer programmer lives on a street with houses on only one side . The houses on this street are numbered sequentially starting from number 1 (one). Every day the programmer leaves the house to walk and, randomly choosing a direction, right or left, goes to the end of the street and back. One day she decided to add up the numbers of the houses she went to and from, excluding her own house. The next day she took the other direction and also added the numbers of the houses she passed, on the way to and from the house, excluding her own house. To their surprise the sums were the same.

     

Description

     

Write a program that prints pairs of integers and positives, the first representing the number of a house and the second representing the number of houses on the street, so that the property observed by the programmer occurs. For example, the first pair of numbers with this property is 6 and 8. That is, if the programmer lives in house 6 on a street that has 8 houses numbered sequentially, then the sum of the houses on the way back and forth to the end the street will be the same in both directions. The second pair with this property will be 35 and 49.

I have not yet realized the "mathematics" and / or relationship behind these houses on the street. For example: the first pair is 6 and 8, the second is 35 and 49. What is the relationship between them? Could anyone explain or create a simple pseudocode template so I have an idea?

    
asked by anonymous 29.05.2014 / 17:51

1 answer

5

As indicated in the example: (6,8)

The programmer lives in house 6 of 8, so:

// Right:

{7,8} -> 7 + 8 = 15

Left:

{1,2,3,4,5} -> 1 + 2 + 3 + 4 + 5 = 15

PseudoCode

Inteiro esquerda = 0
Inteiro direita = 0 
//Direita
Para i = numeroCasaProgramadora faça:
     Se i < UltimaCasa Então:
        direita + = 1
     i + = 1
//esquerda
Para i = numeroCasaProgramadora faça:
     Se i > 0 Entao:
        esquera + = 1
     i - = 1    
Se esquerda == direita Então:
   Imprime "O par " + numeroCasaProgramadora + " e " + UltimaCasa + "é válido"
    
29.05.2014 / 19:33