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