This commit is contained in:
Racoonicorn 2016-12-10 12:26:56 +01:00 committed by GitHub
parent 702dfc9cc3
commit 4d10d23b71
1 changed files with 21 additions and 30 deletions

View File

@ -1,9 +1,9 @@
#define VER "0.3.1" #define VERSION "0.10"
#define N 20 #define N 100
#include <stdio.h> #include <stdio.h>
int derivata (int n); int derive (int n);
void split (int value, int *a, int *b); void split (int value, int *a, int *b);
int main () { int main () {
@ -12,31 +12,29 @@ int main () {
char again; char again;
do { do {
sgn = 1; sgn = 1;
do { printf ("Enter an integer: ");
printf ("Inserisci numero: ");
scanf ("%d", &n); scanf ("%d", &n);
} while (0); // vanno messe condizioni
if (n < 0) if (n < 0)
sgn = -1; sgn = -1;
res = derivata (sgn * n); res = derive (sgn * n);
res = sgn * res; res = sgn * res;
printf ("La derivata di %d è: %d", n, res); printf ("The arithmetic derivative of %d is: %d", n, res);
getchar (); getchar ();
printf ("\nAncora? [s/n] "); printf ("\nAgain? [y/n] ");
scanf ("%c", &again); scanf ("%c", &again);
} while (again == 's'); } while (again != 'n');
printf ("\n\n\nversion: %s", VER); printf ("\n\n\nversion: %s", VERSION);
} }
int derivata (int n){ int derive (int n){
int a = 1, b = 1; int a = 1, b = 1;
switch (n) { switch (n) {
case 0: return 1; break; case 0: return 1; break;
case 1: return 0; break; case 1: return 0; break;
default: default:
split (n, &a, &b); split (n, &a, &b);
if (a > 1 && b > 1){ // cioè non è primo if (b > 1){ // not a prime
n = derivata(a) * b + a * derivata(b); n = derive (a) * b + a * derive (b);
} else { } else {
n = 1; n = 1;
} }
@ -46,21 +44,14 @@ int derivata (int n){
} }
void split (int value, int *a, int *b) { void split (int value, int *a, int *b) {
int i; int i = 2;
int j = 1; // res[0] è il numero di fattori di value int j = 0;
int res[N]; do {
for (i = 2; i <= value; i++) { //andrebbe ottimizzato
if (value % i == 0) { if (value % i == 0) {
value = value / i; *a = i;
res[j] = i; *b = value / *a;
j++; j++;
i = 1;
} }
} i++;
res[0] = --j; } while (j == 0);
for (i = 1; i < res[0]; i++) {
*a = *a * res[i];
}
*b = res[res[0]];
} }