Update arithmetic_derivative.c
This commit is contained in:
parent
4d10d23b71
commit
9400987fc0
|
@ -1,57 +1,56 @@
|
|||
#define VERSION "0.10"
|
||||
#define N 100
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int derive (int n);
|
||||
void split (int value, int *a, int *b);
|
||||
long long derive (long long n);
|
||||
void split (long long value, long long *a, long long *b);
|
||||
|
||||
int main () {
|
||||
int n, res;
|
||||
int sgn;
|
||||
char again;
|
||||
do {
|
||||
sgn = 1;
|
||||
printf ("Enter an integer: ");
|
||||
scanf ("%d", &n);
|
||||
if (n < 0)
|
||||
sgn = -1;
|
||||
res = derive (sgn * n);
|
||||
res = sgn * res;
|
||||
printf ("The arithmetic derivative of %d is: %d", n, res);
|
||||
getchar ();
|
||||
printf ("\nAgain? [y/n] ");
|
||||
scanf ("%c", &again);
|
||||
} while (again != 'n');
|
||||
printf ("\n\n\nversion: %s", VERSION);
|
||||
long long n, res;
|
||||
int sgn;
|
||||
char again;
|
||||
do {
|
||||
sgn = 1;
|
||||
printf ("Enter an integer: ");
|
||||
scanf ("%lli", &n);
|
||||
if (n < 0)
|
||||
sgn = -1;
|
||||
if ( sgn * n > 9999999999999999)
|
||||
printf ("Warning! Number too big. The result could be wrong\n");
|
||||
res = derive (sgn * n); // derive (abs (n))
|
||||
res = sgn * res; // (-n)' = -(n')
|
||||
printf ("The arithmetic derivative of %lli is: %lli", n, res);
|
||||
getchar ();
|
||||
printf ("\nAgain? [y/n] ");
|
||||
scanf ("%c", &again);
|
||||
} while (again != 'n');
|
||||
}
|
||||
|
||||
int derive (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 (b > 1){ // not a prime
|
||||
n = derive (a) * b + a * derive (b);
|
||||
} else {
|
||||
n = 1;
|
||||
}
|
||||
return n;
|
||||
break;
|
||||
}
|
||||
// if n=ab (a and b > 1) -> n'=a'b + ab'
|
||||
long long derive (long long n){
|
||||
long long a = 1, b = 1;
|
||||
switch (n) {
|
||||
case 0: return 0; break; // 0' = 0
|
||||
case 1: return 0; break; // 1' = 0;
|
||||
default:
|
||||
split (n, &a, &b); // n = a * b
|
||||
if (b > 1) // if n is not a prime
|
||||
n = derive (a) * b + a * derive (b);
|
||||
else
|
||||
n = 1; // if n is a prime
|
||||
return n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void split (int value, int *a, int *b) {
|
||||
int i = 2;
|
||||
int j = 0;
|
||||
// n = a * b
|
||||
void split (long long value, long long *a, long long *b) {
|
||||
long long i = 2;
|
||||
int stop = 0;
|
||||
do {
|
||||
if (value % i == 0) {
|
||||
*a = i;
|
||||
*b = value / *a;
|
||||
j++;
|
||||
stop++;
|
||||
}
|
||||
i++;
|
||||
} while (j == 0);
|
||||
} while (stop == 0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue