Hello coders,
I’d like to ask if it is possible to fix this solution for resistance problem:
I realized that circuit is recursive (see the picture):
In the picture there is green R2 and red R3 in R4.
This can be described with formulae:
if I use
I’m getting
using formulae for R_n I’m getting
so far so good. When I used BigDecimal in Java R_100 is calculated well, so formulae seems to be ok, but BigDecimals are to slow to get 200.000th. When I use longs and use modulo k in each I’m getting wrong results from n = 9
UPD: I found, that when I skip finding of gcd it is ok, but is there a way to use gcd ?(because without tutorial I won’t know, that these numbers are Fibonacci numbers and gcd is always 1)
public static void solve( final int n, final int mod ) {
final long[] a = new long[ n + 1 ];
final long[] b = new long[ n + 1 ];
a[1] = 1;
b[1] = 1;
for ( int i = 2; i <= n; i++ ) {
a[i] = ( a[i - 1] + b[i - 1] ) % mod;
b[i] = ( a[i - 1] + 2 * b[i - 1] ) % mod;
// long g = gcd(a[i], b[i]);
// a[i] /= g;
// b[i] /= g;
}
System.out.printf( "%d/%d\n", a[n], b[n] );
}