Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:8913a85b9514, 2021-03-26 (annotated)
- Committer:
- rescudev
- Date:
- Fri Mar 26 22:04:33 2021 +0000
- Revision:
- 0:8913a85b9514
Ejercicio 3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rescudev | 0:8913a85b9514 | 1 | #include <stdio.h> |
| rescudev | 0:8913a85b9514 | 2 | #include "mbed.h" |
| rescudev | 0:8913a85b9514 | 3 | |
| rescudev | 0:8913a85b9514 | 4 | void pi(void) |
| rescudev | 0:8913a85b9514 | 5 | { |
| rescudev | 0:8913a85b9514 | 6 | //Número de decimales que se expresan |
| rescudev | 0:8913a85b9514 | 7 | const int n=10000; |
| rescudev | 0:8913a85b9514 | 8 | //Tamaño del array y definición del resto de variables |
| rescudev | 0:8913a85b9514 | 9 | const int dim =3334; |
| rescudev | 0:8913a85b9514 | 10 | int i, j, k, resto, digant, nueves, aux; |
| rescudev | 0:8913a85b9514 | 11 | int pi[dim+1]; |
| rescudev | 0:8913a85b9514 | 12 | |
| rescudev | 0:8913a85b9514 | 13 | //Código del algoritmo |
| rescudev | 0:8913a85b9514 | 14 | for (i=1;i<= dim;i ++) |
| rescudev | 0:8913a85b9514 | 15 | { |
| rescudev | 0:8913a85b9514 | 16 | pi[i]=2; |
| rescudev | 0:8913a85b9514 | 17 | nueves=0; |
| rescudev | 0:8913a85b9514 | 18 | digant =0; |
| rescudev | 0:8913a85b9514 | 19 | } |
| rescudev | 0:8913a85b9514 | 20 | for (i=1;i<=n;i++) |
| rescudev | 0:8913a85b9514 | 21 | { |
| rescudev | 0:8913a85b9514 | 22 | resto=0; |
| rescudev | 0:8913a85b9514 | 23 | for (j= dim;j >=1;j--) |
| rescudev | 0:8913a85b9514 | 24 | { |
| rescudev | 0:8913a85b9514 | 25 | aux =10*pi[j]+resto*j; |
| rescudev | 0:8913a85b9514 | 26 | pi[j]= aux % (2*j-1); |
| rescudev | 0:8913a85b9514 | 27 | resto=aux /(2*j-1); |
| rescudev | 0:8913a85b9514 | 28 | } |
| rescudev | 0:8913a85b9514 | 29 | pi[1]=resto % 10; |
| rescudev | 0:8913a85b9514 | 30 | resto=resto/10; |
| rescudev | 0:8913a85b9514 | 31 | if(resto==9) |
| rescudev | 0:8913a85b9514 | 32 | { |
| rescudev | 0:8913a85b9514 | 33 | nueves++; |
| rescudev | 0:8913a85b9514 | 34 | } |
| rescudev | 0:8913a85b9514 | 35 | else if (resto==10) |
| rescudev | 0:8913a85b9514 | 36 | { |
| rescudev | 0:8913a85b9514 | 37 | printf("%i", digant); |
| rescudev | 0:8913a85b9514 | 38 | for (k=1;k<=nueves;k++) |
| rescudev | 0:8913a85b9514 | 39 | { |
| rescudev | 0:8913a85b9514 | 40 | printf ("0"); |
| rescudev | 0:8913a85b9514 | 41 | digant=0; |
| rescudev | 0:8913a85b9514 | 42 | nueves=0; |
| rescudev | 0:8913a85b9514 | 43 | } |
| rescudev | 0:8913a85b9514 | 44 | } |
| rescudev | 0:8913a85b9514 | 45 | else |
| rescudev | 0:8913a85b9514 | 46 | { |
| rescudev | 0:8913a85b9514 | 47 | printf("%i", digant); |
| rescudev | 0:8913a85b9514 | 48 | digant=resto; |
| rescudev | 0:8913a85b9514 | 49 | if(nueves!=0) |
| rescudev | 0:8913a85b9514 | 50 | { |
| rescudev | 0:8913a85b9514 | 51 | for(k=1;k<=nueves;k ++) |
| rescudev | 0:8913a85b9514 | 52 | { |
| rescudev | 0:8913a85b9514 | 53 | printf ("9"); |
| rescudev | 0:8913a85b9514 | 54 | nueves=0; |
| rescudev | 0:8913a85b9514 | 55 | } |
| rescudev | 0:8913a85b9514 | 56 | } |
| rescudev | 0:8913a85b9514 | 57 | } |
| rescudev | 0:8913a85b9514 | 58 | } |
| rescudev | 0:8913a85b9514 | 59 | printf("%i", digant); |
| rescudev | 0:8913a85b9514 | 60 | } |
| rescudev | 0:8913a85b9514 | 61 | |
| rescudev | 0:8913a85b9514 | 62 | |
| rescudev | 0:8913a85b9514 | 63 | int main() |
| rescudev | 0:8913a85b9514 | 64 | { |
| rescudev | 0:8913a85b9514 | 65 | while(1){ |
| rescudev | 0:8913a85b9514 | 66 | //Llamada a la función dentro del bucle |
| rescudev | 0:8913a85b9514 | 67 | printf("\nPrimeros 10000 decimales de PI:\n\r"); |
| rescudev | 0:8913a85b9514 | 68 | pi(); |
| rescudev | 0:8913a85b9514 | 69 | printf("\n"); |
| rescudev | 0:8913a85b9514 | 70 | wait(1); |
| rescudev | 0:8913a85b9514 | 71 | } |
| rescudev | 0:8913a85b9514 | 72 | } |