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))
|
||||||
getchar ();
|
res = sgn * res; // (-n)' = -(n')
|
||||||
printf ("\nAgain? [y/n] ");
|
printf ("The arithmetic derivative of %lli is: %lli", n, res);
|
||||||
scanf ("%c", &again);
|
getchar ();
|
||||||
} while (again != 'n');
|
printf ("\nAgain? [y/n] ");
|
||||||
printf ("\n\n\nversion: %s", VERSION);
|
scanf ("%c", &again);
|
||||||
|
} while (again != 'n');
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
||||||
switch (n) {
|
long long a = 1, b = 1;
|
||||||
case 0: return 1; break;
|
switch (n) {
|
||||||
case 1: return 0; break;
|
case 0: return 0; break; // 0' = 0
|
||||||
default:
|
case 1: return 0; break; // 1' = 0;
|
||||||
split (n, &a, &b);
|
default:
|
||||||
if (b > 1){ // not a prime
|
split (n, &a, &b); // n = a * b
|
||||||
n = derive (a) * b + a * derive (b);
|
if (b > 1) // if n is not a prime
|
||||||
} else {
|
n = derive (a) * b + a * derive (b);
|
||||||
n = 1;
|
else
|
||||||
}
|
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