Curiosities: Arithmetic Progression [Learning to Program]

in #programming7 years ago

At school or at college we are not taught to love math or how fun it can be ...

Arithmetic progression

An  arithmetic progression is a succession of numbers such that each of  them (except the first) is equal to the previous one plus a fixed number  called difference.


Some Progressions Interesting with 9:

Take the number 9 and multiply it by any number example 12 is 108, now add each of the number 108 will get 9:

108 = 1+0+8 = 9


Take another number for example 2, 2 * 9 = 18 now add 1 + 8 you will get 9.

Mathematics  can really stop being so boring, at school or at university they do not  teach us how to love them or how fun they can be, in this Journey of  Learning to Schedule I will try to encourage them with mathematics and  Programming, and we will use The math to do incredible things we will see, hope in God !!!

This  time I bring you one of the exercises that I found really interesting  from the book Learn-to-think-as-programmer-in-python As you know  available in [pdf] format on the Internet, exercise is the arithmetic  progression of Any  number such as n, when checking for even if it is divided by 2, but if  it is odd it is multiplied by 3 and one is added until the value of n is  1 so we are talking about a cycle and in programming We must use while since we do not know when it ends.


Developer Tips:

In  programming we will use FOR loops for the defined cycles, and WHILE for  the undefined ones or for which we need to enter an exact string or  press a key, in this case we do not know when it ends so we must use  WHILE since the final is not defined Of the cycle.

Thus, in python:

n = int(input( "Introduce el valor de n: "))
while n != 1: # Aqui se revisa si n es diferente de 1
print n,
    if n%2 == 0: # Aquí se revisa si n sea par
         n = n/2 # Aquí se ejecuta la ecuación en caso de que n sea par
    else: # Aquí se revisa si n sea impar implícitamente es par o no lo es
         n = n*3+1 # Aquí se ejecuta la ecuación en caso de que n sea impar

If we execute the code we will ask for the value of n, if for example we enter 55 it will return us:

 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484  242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526  263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132  566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858  2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308  1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35  106 53 160 80 40 20 10 5 16 8 4 2

If we look at  the progression is not so easy to see and previously I showed you a  caesar-type cipher that I call Enigma because of its resemblance to  Enigma, I'm totally sure it's not so easy to break.


The program  just throw us a fairly strange number, but it is not random is the  mathematical sequence of 55 to get to 1 using the parameters of the  comparisons for each equation made previously, now that would happen if  for example we put -2 we would enter a cycle Infinite  since the negative number will never become positive then we must place  an exception or rather a conditional if the number is less than 1 the  program does not enter the loop and requires only positive numbers.

Developer Tips:


It  is important to make your program always fail, this is called Test  level, while your program fails here and not in Production everything  will be better.

Note Production is when your Program is already in the market.
n = int(input( "Introduce el valor de N: "))
if n < 1:
    print "Solo Numero positivos mayores que 0"
else:
    while n != 1:
         print n,
         if n%2 == 0:
              n = n/2
         else:
              n = n*3+1

What happens if the number is not integer and why? For example, enter 55.6.

Test ... Test ... Test ... Test ... That's a good programmer

I hope you have liked them, and take responsively this re-factorization of my POST, God Bless you.



Sort:  

I like this program because my math is very weak so i learning online as this program.

GGGG if you like, vote and I continue making interesting mathematical modules that do things that we do not know: D

When I'm in school in math I hate arithmetic chapter!!

Everyone hates Mrs. Mathematics until they meet her, greeting, thanks for commenting

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63834.78
ETH 2627.38
USDT 1.00
SBD 2.78