Arithmetic-Derivative/arithmetic_derivative.c

58 lines
948 B
C
Raw Normal View History

2016-12-10 12:26:56 +01:00
#define VERSION "0.10"
#define N 100
2016-12-09 22:14:05 +01:00
#include <stdio.h>
2016-12-10 12:26:56 +01:00
int derive (int n);
2016-12-09 22:14:05 +01:00
void split (int value, int *a, int *b);
int main () {
int n, res;
int sgn;
char again;
do {
sgn = 1;
2016-12-10 12:26:56 +01:00
printf ("Enter an integer: ");
scanf ("%d", &n);
2016-12-09 22:14:05 +01:00
if (n < 0)
sgn = -1;
2016-12-10 12:26:56 +01:00
res = derive (sgn * n);
2016-12-09 22:14:05 +01:00
res = sgn * res;
2016-12-10 12:26:56 +01:00
printf ("The arithmetic derivative of %d is: %d", n, res);
2016-12-09 22:14:05 +01:00
getchar ();
2016-12-10 12:26:56 +01:00
printf ("\nAgain? [y/n] ");
2016-12-09 22:14:05 +01:00
scanf ("%c", &again);
2016-12-10 12:26:56 +01:00
} while (again != 'n');
printf ("\n\n\nversion: %s", VERSION);
2016-12-09 22:14:05 +01:00
}
2016-12-10 12:26:56 +01:00
int derive (int n){
2016-12-09 22:14:05 +01:00
int a = 1, b = 1;
switch (n) {
case 0: return 1; break;
case 1: return 0; break;
default:
split (n, &a, &b);
2016-12-10 12:26:56 +01:00
if (b > 1){ // not a prime
n = derive (a) * b + a * derive (b);
2016-12-09 22:14:05 +01:00
} else {
n = 1;
}
return n;
break;
}
}
void split (int value, int *a, int *b) {
2016-12-10 12:26:56 +01:00
int i = 2;
int j = 0;
do {
if (value % i == 0) {
*a = i;
*b = value / *a;
2016-12-09 22:14:05 +01:00
j++;
}
2016-12-10 12:26:56 +01:00
i++;
} while (j == 0);
2016-12-09 22:14:05 +01:00
}