#include <stdio.h>
int super (int n, int m) {
if (m == 1) return 1;
if (n == 1) return super (m - 1, m - 1);
return n * super(n - 1, m);
}
int sfat(n) {
return sfat2(1, n, 1); // Vai de 1 a n, e o valor acumulado é 1
}
int sfat2(inicio, fim, acumulador) {
if ( inicio > fim )
return acumulador;
return acumulador * sfat2(inicio+1, fim, inicio*acumulador);
}
int sfat_cauda(n) {
return sfat2_cauda(1, n, 1, 1); // Acumula (n-1)! e o resultado
}
int sfat2_cauda(inicio, fim, fat_acc, sfat_acc) {
if ( inicio > fim )
return sfat_acc;
return sfat2_cauda(inicio+1, fim, inicio*fat_acc, inicio*fat_acc*sfat_acc);
}
int main(void) {
for ( int i = 1 ; i < 8 ; i++ )
printf("%d %d %d\n", super
(i
,i
), sfat
(i
), sfat_cauda
(i
)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgc3VwZXIgKGludCBuLCBpbnQgbSkgewogICBpZiAobSA9PSAxKSByZXR1cm4gMTsKICAgaWYgKG4gPT0gMSkgcmV0dXJuIHN1cGVyIChtIC0gMSwgbSAtIDEpOwogICByZXR1cm4gbiAqIHN1cGVyKG4gLSAxLCBtKTsKfQoKaW50IHNmYXQobikgewogICAgcmV0dXJuIHNmYXQyKDEsIG4sIDEpOyAvLyBWYWkgZGUgMSBhIG4sIGUgbyB2YWxvciBhY3VtdWxhZG8gw6kgMQp9CgppbnQgc2ZhdDIoaW5pY2lvLCBmaW0sIGFjdW11bGFkb3IpIHsKICAgIGlmICggaW5pY2lvID4gZmltICkKICAgICAgICByZXR1cm4gYWN1bXVsYWRvcjsKICAgIHJldHVybiBhY3VtdWxhZG9yICogc2ZhdDIoaW5pY2lvKzEsIGZpbSwgaW5pY2lvKmFjdW11bGFkb3IpOwp9CgppbnQgc2ZhdF9jYXVkYShuKSB7CiAgICByZXR1cm4gc2ZhdDJfY2F1ZGEoMSwgbiwgMSwgMSk7IC8vIEFjdW11bGEgKG4tMSkhIGUgbyByZXN1bHRhZG8KfQoK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