This is the code i have written for the resistance problem. I have written recursive functions to calculate numerator and denominator. In the end I am reducing the fraction by recursively dividing numerator and denominator by their gcd until the gcd becomes 1. As per the formula 1/R = 1/R1 + 1/R2 ,therefore at any given time 1/R(n) = 1/R(n1) + 1/2 where R(n) is the equivalent resistance when there are n blocks. Say R(n1) = A/B , thus 1/R(n) = B/A + 1/2 Thus numerator of R(n) = 2A and denominator = 2B + A I have written two functions for calculating these.
long int num_calc(long int n) { if(n==1) return 1;
}
long int den_calc(long int n) { if(n==1) return 1;
} After that I keep reducing the fraction by continuously, dividing numerator and denominator by their gcd, until the gcd is 1.
void lowest_fraction(long int a, long int b) { if(gcd(a, b)==1) { num = a; den = b; }
} It works for n<29 but I start getting errors when n >=29 Can anyone please suggest any fixes for the code ? asked 04 Mar '14, 11:55
