Thursday, May 21, 2009

Craccare Windowblinds

Fibonacci function Fibonacci function with Dynamic Programming Problem

We will solve the Fibonacci

function by dynamic programming

, as you know this function corresponds to equation : F (x) = f (x-1) + f (x-2)

can make this problem by dynamic programming to avoid repeatedly calculating the same value. The precondition to exhibit this problem is that you can not find the value of fibonacci function for negative numbers.

S olution to the problem The solution to the problem we have is an example of dynamic programming algorithm

, with this implementation is a slight improvement in the example presented in the book's didactic recursive search "function Fibonacci." Temporal Complexity



This code contains multiple sentences and multiple linear directions, when the time complexity of finding the only thing we want to look is the only simple loop there. Given that the time complexity would be O (n) where n is the number of passes that have to do the loop. Fibonacci function implemented with

Dynamic Programming with Java: I found this implementation class that creates a program that solves the Fibonacci
function using Java and


dynamic programming: Fibonacci class

Source code

import java.io. *;
public class Fibonacci {BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
 int opcionSalir;  public Fibonacci () { mostrarMenu ();}   

fibonacci_pd public static long (long n) throws
NumeroException {if (n \u0026lt;0) {throw new NumeroException
("Can not enter numbers negative ");}



if (n == 0 return n;}

long [] a = new long [(int) n +1];
a [0] = 0;
a [1] = 1;
for (int i = 2; i \u0026lt;= n , i + +) {
a [i] = a [i-1] + a [i-2];
}
return to [(int) n];
}

private void mostrarMenu () {

opcionSalir = 2;

int option;

do {String [] menu = {
"\\ n -------------- -------------- Fibonacci "
" 1 .- Enter Value "

" "
" 2 .- Exit "
"------------------------
for
----------------"}; (Int i = 0; i \u0026lt;menu.length; i + +) {System.out.println
(menu [i]);}


System.out.println ();
pedirOpcion option = ();
System . out.println ();
procesarOpcion (option);
System.out.println ();}

while (option! = opcionSalir)
System.out.println ("Program complete.");}

private void

procesarOpcion (int option) {
switch (option) {case 1
:
calculate ();
break;

case 2:
System.exit (0);
break;


}} private int

pedirOpcion () {int
option = 0;

do {option = pedirEntero ("Options");
if (option \u0026lt;1 & & option> opcionSalir) {
System.out.println ("Invalid option, repeat");}


} while (choice \u0026lt;1
} catch (IOException ex) {System.out.println
(
"Generic Error Input-Output with the keyboard");

} catch (NumberFormatException ex) {System.out.println
("Invalid format for number ");}

return integer;


} public void calculate () {System.out.println
(
" \\ n Please enter the number from which you want to calculate the Fibonacci sequence: ");



try { long number = Long.parseLong (teclado.readLine ());
System.out.println (
"\\ nThe value of the Fibonacci function for the number"
+ number + "is:" + fibonacci_pd (number));

} catch (NumeroException ex) {
System.err.println ("You can only enter numbers");


} catch (IOException ex) {
System.err.println (ex.getMessage ());

} catch (NumberFormatException ex) {
System.err.println (ex.getMessage ());}



} public static void main (String [ ] args) {
new Fibonacci ();
}}


NUMEROEXCEPTION

CLASS public class
NumeroException extends Exception {public

NumeroException (String message) {
super (message);
}}







previous Code import java.io. *;

public class Fibonacci {

/ **
* fibonacci function is implemented with dynamic programming technique.
* It is noted that the number is positive.
* @ param n * @ throws long
NumeroException

* @ return long * / public static long
fibonacci_pd (long n) throws
NumeroException {if (n \u0026lt;0) {throw new NumeroException
("Can not enter negative numbers ");

} if (n == 0) {throw new NumeroException
(" The number must be greater than 0 ");}

long [] f = new long [(int) n + 1 ];
f [0] = 0;
f [1] = 1;
for (int i = 2; i \u0026lt;n + 1, i + +) {
f [i] = f [i - 1] + f [i - 2];

} return f [(int) n];}


/ ** * Function Main
. It asks the user a number to calculate its value by
Fibonacci function * * @
param String args []
* /
public static void main (String [] args) {BufferedReader
reader = new BufferedReader (new InputStreamReader (System.in)); System.out.println
(
"\\ n Please enter the number that you want to calculate the Fibonacci sequence:");

try {long number = Long.parseLong (reader.readLine ());
System.out.println (
"\\ nThe value of the Fibonacci function for the number" + number +
"is:" + fibonacci_pd (number));}

catch (NumberFormatException NFE) {
System.err.println ("You can only enter numbers");
main (args);

} catch (NumeroException ne) {System.err.println
(ne.getMessage ());
main (args);

} catch (IOException ioe) {System.err.println
(ioe.getMessage ());

}}



} Exception used in the code above


NumeroException
public class extends Exception {public

NumeroException (String message) {
super (message);
}}





0 comments:

Post a Comment