Você está na página 1de 4

#include <stdio.

h>
#include <stdlib.h>

int fatorial(int n){


unsigned long long resultado=1;
for(int i=1;i<=n;i++){
resultado*=i;
}
return resultado;
}

int main(int argc,char* argv[]){


int n = atoi(argv[1]);
unsigned long long resposta;
for (int i=0;i<1000000;i++){
resposta=fatorial(n);
}
printf("O fatorial de %d é: %lld",n,resposta);
return 0;
}
#include <stdio.h>
#include <stdlib.h>

int fatorial(int n){


if ((n==0)||(n==1)){
return 1;
}
return fatorial(n-1)*n;
}

int main(int argc,char* argv[]){


int n = atoi(argv[1]);
unsigned long long resposta;
for (int i=0;i<1000000;i++){
resposta=fatorial(n);
}
printf("O fatorial de %d é: %lld",n,resposta);
return 0;
}
#!/bin/bash

TIMEFORMAT='%9R'

gcc fatorial-for.c -o fatorial-for

gcc fatorial-recursivo.c -o fatorial-recurcivo

touch tempo-for.txt

touch tempo-recursivo.txt

for counter in $(seq 1 12);

do

{ time ./fatorial-recurcivo $counter ;} 2>> tempo-recursivo.txt;

{ time ./fatorial-for $counter ;} 2>> tempo-for.txt;

done
Coluna3 Coluna4

14
Título do Gráfico
12 14
12
10
8
10 6
4
2
0
Tempo recusivo

0.004

0.008

0.011

0.016

0.020

0.025

0.028

0.034

0.036

0.042

0.045

0.050
8

Tempo 0.006 0.007 0.008 0.010 0.012 0.014 0.016 0.019 0.021 0.024 0.027 0.029
4
s

Coluna3 Coluna4
2

0
TEMPO 0.004 0.008 0.011 0.016 0.020 0.025 0.028 0.034 0.036 0.042 0.045 0.050
RECUSIVO
TEMPO S 0.006 0.007 0.008 0.010 0.012 0.014 0.016 0.019 0.021 0.024 0.027 0.029

Você também pode gostar