From 702dfc9cc3b7b323c3a42afaa41a51c41326e26d Mon Sep 17 00:00:00 2001 From: Racoonicorn Date: Fri, 9 Dec 2016 22:14:05 +0100 Subject: [PATCH] Create arithmetic_derivative.c --- arithmetic_derivative.c | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 arithmetic_derivative.c diff --git a/arithmetic_derivative.c b/arithmetic_derivative.c new file mode 100644 index 0000000..ce8fd11 --- /dev/null +++ b/arithmetic_derivative.c @@ -0,0 +1,66 @@ +#define VER "0.3.1" +#define N 20 + +#include + +int derivata (int n); +void split (int value, int *a, int *b); + +int main () { + int n, res; + int sgn; + char again; + do { + sgn = 1; + do { + printf ("Inserisci numero: "); + scanf ("%d", &n); + } while (0); // vanno messe condizioni + if (n < 0) + sgn = -1; + res = derivata (sgn * n); + res = sgn * res; + printf ("La derivata di %d è: %d", n, res); + getchar (); + printf ("\nAncora? [s/n] "); + scanf ("%c", &again); + } while (again == 's'); + printf ("\n\n\nversion: %s", VER); +} + +int derivata (int n){ + int a = 1, b = 1; + switch (n) { + case 0: return 1; break; + case 1: return 0; break; + default: + split (n, &a, &b); + if (a > 1 && b > 1){ // cioè non è primo + n = derivata(a) * b + a * derivata(b); + } else { + n = 1; + } + return n; + break; + } +} + +void split (int value, int *a, int *b) { + int i; + int j = 1; // res[0] è il numero di fattori di value + int res[N]; + + for (i = 2; i <= value; i++) { //andrebbe ottimizzato + if (value % i == 0){ + value = value / i; + res[j] = i; + j++; + i = 1; + } + } + res[0] = --j; + for (i = 1; i < res[0]; i++) { + *a = *a * res[i]; + } + *b = res[res[0]]; +}