-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.c
102 lines (36 loc) · 886 Bytes
/
branch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bce.h"
#define EPSILON 0.001
/* int main() */
/* { */
/* printf("%f\n", branch_factor(52,5)); */
/* } */
double fn(double b, double d, double n)
{
return pow(b, d+1) - n*b + n - 1;
}
double fn_prime(double b, double d, double n)
{
return (d+1) * pow(b, d) - n;
}
double branch_factor(int nodes, int depth)
{
double b = pow(nodes, 1.0/depth);
int i;
for (i=0;i<1000;i++)
{
double denom = fn_prime(b, depth, nodes);
double num = fn(b, depth, nodes);
double b_next;
double diff;
if (denom == 0.0) denom = 0.001;
b_next = b - num / denom;
diff = (b>b_next) ? b-b_next : b_next -b;
if (diff<EPSILON)
return b;
b = b_next;
}
return -1;
}