-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfibonacciLogaritmico.java
More file actions
51 lines (43 loc) · 1.33 KB
/
fibonacciLogaritmico.java
File metadata and controls
51 lines (43 loc) · 1.33 KB
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
/*
* Archivo: fibonacciLogaritmico.java
*
* Descripci'on: programa tal que dados un valor entero N, determina el
* fibonacci correspondiente fib.N. Esto con un orden de
* complejidad logaritmico.
* Permite probar operaciones b'asicas sobre enteros y
* condicionales e iteraciones sencillas.
* (algoritmo tomado del cap'itulo 5 del texto "Programming: The
* derivation of algorithms" de Anne Kaldewaij)
*
* Fecha: 27 de mayo de 2010
*
*/
class fibonacciLogaritmico {
public static void main (String args[]) {
final int N;
int x;
N = Console.readInt("Valor de N: ");
{
int a, b, n, y;
a = 0;
b = 1;
x = 0;
y = 1;
n = N;
while ( n != 0 ) {
if (n % 2 == 0) {
int aux = a;
a = a*a + b*b;
b = aux*b + b*aux + b*b;
n = n / 2;
} else if (n % 2 == 1) {
int aux = x;
x = a*x + b*y;
y = b*aux + a*y + b*y;
n = n - 1;
}
}
}
System.out.println("El valor de fibonacci de " + N + " es: " + x);
}
}