Nombre: Serie Fibonacci with Java
Descripción:
-1) Verificar que el primer número de la serie de Fibonacci con 91 dígitos es:
1387277127804783827114186103186246392258450358171783690079918032136025225954602593712568353
-2) Pasar como parámetro el número de dígitos que se desean para el número de la serie.
-3) El número de dígitos debe leerse desde la línea de comandos interactivamente.
URL: http://www.mygnet.net/codigos/java/analisisnumericos/serie_fibonacci_with_java.2720
Código Fuente:
/*
L. I. Gerardo Ángeles Nava.
Original Julio 09, 2008
Actualizado Julio 16, 2008
Fibonacci::Objetivos:
-0) Salir
-2) El número de dígitos debe leerse desde la línea de comandos interactivamente.
-1) Verificar que el primer número de la serie de Fibonacci con 91 dígitos es:
1387277127804783827114186103186246392258450358171783690079918032136025225954602593712568353
-) Pasar como parámetro el número de dígitos que se desean para el número de la serie.
Validaciones:
* Validar la entrada de datos
x Generar numeros de la serie de un tamano entre 1 y 300 digitos, cualquier valor que indique el usuario que este fuera de este rango no sera valido
* En número debe desplegarse como un número natural.
* No usar notación exponencial
* Por último (de manera opcional), verificar que es un número primo.
Notas:
+ Explicar la complejidad espacial y temporal de tu implementación
**/
import java.io.*;
import java.math.BigInteger;
class SerieFibonacci {
void Menu(){
System.out.println("ntPresst[-0] to Exit");
System.out.println("ntt[-1] to Verify if the first number with 91 places is equal and..");
System.out.println("ntt[-2] to Give the number of the places when the serie needs stop");
System.out.println("ntt[-3] to Get Fibonacci");
System.out.print("ntOption: ");
}
void Read(String sLine){
if(sLine.toString().length() > 300 ){
System.err.println("ntLa cifra solo puede contener hasta 300 digitos");
System.exit(1);
}
if (sLine.equals("-0")){
System.out.print("nntThanks for trying, July 2008 - Gerardo Angeles Nava, Mexico, D.F.nn");
System.exit(1);
}
int iValidate = 0;
try {
iValidate = Integer.parseInt(sLine);
} catch (NumberFormatException e) {
System.err.println("ntt[E1] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntOption: ");
return;
}catch(Exception e){
System.err.println("ntt[E2] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntOption: ");
return;
}
switch(Integer.parseInt(sLine)) {
case -1:
this.PrintHeader();
BigInteger biCompare = this.FibCompare(91);
this.Menu();
break;
case -2:
System.out.print("ntHow many places needs have Fibonacci: ");
this.f2();
break;
case -3:
System.out.print("ntFibonacci: ");
this.f3();
break;
//case 4:
//break;
default:
System.err.println("nttPlease enter a valid option!");
System.out.print("ntOption: ");
}
/*
if(sLine.equals("-1")){
this.PrintHeader();
BigInteger biCompare = this.FibCompare(91);
this.Menu();
}
if(sLine.equals("-2")){
System.out.print("ntStop when places are: ");
this.f2();
}
if(sLine.equals("-3")){
System.out.print("ntFibonacci: ");
this.f3();
}
*/
}
void f2(){
while(true){
try {
int iValidate = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sLine = br.readLine();
if(sLine.toString().length() > 300 ){
System.err.println("ntLa cifra solo puede contener hasta 300 digitos");
return;
//System.exit(1);
}
if (sLine.equals("-0")){
System.out.print("nntThanks for trying, July 2008 - Gerardo Angeles Nava, Mexico, D.F.nn");
return;
//System.exit(1);
}
try {
iValidate = Integer.parseInt(sLine);
if(iValidate < 0 ){
System.err.println("nttPlease enter a positive number!");
System.out.print("ntHow many places needs have Fibonacci: ");
this.f2();
break;
//System.exit(1);
}
} catch (NumberFormatException e) {
System.err.println("ntt[E1] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntHow many places needs have Fibonacci: ");
this.f2();
break;
}catch(Exception e){
System.err.println("ntt[E2] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntStop when places are: ");
this.f2();
break;
}
this.PrintHeader();
BigInteger biCompare = this.FibDig(iValidate);
this.Menu();
break;
}catch (IOException ioe) {
System.out.println("IO error trying to read input");
System.exit(1);
}
}
}
void f3(){
while(true){
try {
int iValidate = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sLine = br.readLine();
if(sLine.toString().length() > 300 ){
System.err.println("ntLa cifra solo puede contener hasta 300 digitos");
System.exit(1);
}
if (sLine.equals("-0")){
System.out.print("nntThanks for trying, July 2008 - Gerardo Angeles Nava, Mexico, D.F.nn");
System.exit(1);
}
try {
iValidate = Integer.parseInt(sLine);
if(iValidate < 0 ){
System.err.println("nttPlease enter a positive number!");
System.out.print("ntHow many places needs have Fibonacci: ");
this.f3();
break;
//System.exit(1);
}
} catch (NumberFormatException e) {
System.err.println("ntt[E1] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntFibonacci: ");
this.f3();
break;
}catch(Exception e){
System.err.println("ntt[E2] Argument must be an integer between [" + Integer.MIN_VALUE + " - " + Integer.MAX_VALUE + "]");
System.out.print("ntFibonacci: ");
this.f3();
break;
}
this.PrintHeader();
BigInteger biCompare = this.LongitudElemento(iValidate);
this.Menu();
break;
}catch (IOException ioe) {
System.out.println("IO error trying to read input");
System.exit(1);
}
}
}
void PrintHeader(){
System.out.println("_________________________________________________________");
System.out.println("nElementotFibonaccitDigitostIs Prime?");
System.out.println("---------------------------------------------------------");
}
BigInteger LongitudElemento(int le){
BigInteger bi = BigInteger.valueOf(1);
int k = 0;
BigInteger bj = BigInteger.valueOf(0);
BigInteger bt = BigInteger.valueOf(0);
int n = le;
for(k = 0; k <= n; k++){
bt = bi.add(bj);
bi = bj;
bj = bt;
if(bi.isProbablePrime(10000)){
System.out.println(k + "tt" + bi + "tt" + bi.toString().length() + "tis probably prime");
}else{
System.out.println(k + "tt" + bi + "tt" + bi.toString().length() + "tis not prime");
}
}
return bi;
}
BigInteger FibCompare(int le){
BigInteger bi = BigInteger.valueOf(1);
int k = 0;
BigInteger bj = BigInteger.valueOf(0);
BigInteger bt = BigInteger.valueOf(0);
String s91 = "1387277127804783827114186103186246392258450358171783690079918032136025225954602593712568353";
bi = BigInteger.valueOf(1);
k = 0;
bj = BigInteger.valueOf(0);
bt = BigInteger.valueOf(0);
boolean Continuar = true;
while(Continuar){
bt = bi.add(bj);
bi = bj;
//System.out.println(k + "t" + bi + "t" + bi.toString().length());
if(bi.toString().length() == 91){
Continuar = false;
}
bj = bt;
k++;
}
System.out.println(k + "t" + bi + "t" + bi.toString().length());
System.out.println("t" + s91);
if(bi.toString().equals(s91)){
System.out.println("tAre equal");
}else{
System.out.println("tSon diferentes");
}
return bi;
}
BigInteger FibDig(int le){
BigInteger bi = BigInteger.valueOf(1);
int k = 0;
BigInteger bj = BigInteger.valueOf(0);
BigInteger bt = BigInteger.valueOf(0);
bi = BigInteger.valueOf(1);
k = 0;
bj = BigInteger.valueOf(0);
bt = BigInteger.valueOf(0);
boolean Continuar = true;
while(Continuar){
bt = bi.add(bj);
bi = bj;
if(bi.toString().length() == le){
Continuar = false;
}
if(bi.isProbablePrime(10000)){
System.out.println(k + "tt" + bi + "tt" + bi.toString().length() + "tis probably prime");
}else{
System.out.println(k + "tt" + bi + "tt" + bi.toString().length() + "tis not prime");
}
bj = bt;
k++;
}
return bi;
}
/**************************************************************************************************************************/
public static void main (String[] args) {
SerieFibonacci f = new SerieFibonacci();
f.Menu();
while(true){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
f.Read(s);
} catch (IOException ioe) {
System.out.println("IO error trying to read input");
System.exit(1);
}
}
}
/**************************************************************************************************************************/
}