By GokiSoft.com| 20:26 30/01/2023|
Java Basic

In chuỗi Fibonaci

Viết chương trình hiển thị dãy Fibonaci biết

F(0) = 1

F(1) = 1

F(n) = F(n-1) + F(n-2)

Nhập vào số max. Yêu cầu in ra tất cả các số Fibonaci có số lớn nhất là max bằng các giải thuật sau

1. Dùng đệ quy

2. Dùng đệ quy có nhớ

3. Không dùng đệ quy

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

NgNhan [APROTRAIN_ADF]
NgNhan

2020-06-04 07:45:11




import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = Integer.parseInt(scanner.nextLine());

        int f1 = 0;
        int f2 = 1;
        int next = 0;
        
        System.out.print("Fibonaci smaller than " + n + ": ");

        while (next <= n) {            
            System.out.print(" "+next+" ");
            
            f1 = f2;
            f2 = next;
            next = f1+f2;
        }



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-04-13 09:07:36




package Fibonaci;

import java.util.Scanner;

public class Fibonaci {

    public static int fibonaci(int n) {
        if (n < 0) {
            return -1;
        } else if (n == 0 || n == 1) {
            return 1;
        } else {
            return fibonaci(n - 1) + fibonaci(n - 2);
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập vào max: ");
        int n = scan.nextInt();
        int i = 0;
        System.out.println("Dãy fibonacci nhỏ hơn " + n + " là: ");
            while(fibonaci(i) <  n) {
                System.out.print(fibonaci(i) + " ");
                i++;
            }
        
}}



hoangkhiem [C1907L]
hoangkhiem

2020-04-13 08:37:04

dùng đệ quy



public class FibonacciExample2 {

    public static void main(String[] args) {
        System.out.println("Nhập số max : ");
        Scanner nhap = new Scanner(System.in);
        int n = nhap.nextInt();
        System.out.println("Dãy số fibonacci là : ");
        for (int i = 0; i < n; i++) {
            System.out.print(fibonacci(i) + " ");
        }
    }

    public static int fibonacci(int n) {
        if (n < 0) {
            return -1;
        } else if (n == 0 || n == 1) {
            return n;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }
}



hoangkhiem [C1907L]
hoangkhiem

2020-04-13 08:36:32

Không dùng đệ quy


public class FibonacciExample1 {
     public static void main(String[] args) {
        System.out.println("Nhập số max : ");
        Scanner nhap = new Scanner(System.in);
        int n = nhap.nextInt();
         System.out.println("Dãy số fibonacci là : ");
        for (int i = 0; i < n; i++) {
            System.out.print(fibonacci(i) + " ");
        }
    }
    public static int fibonacci(int n) {
        int f0 = 0;
        int f1 = 1;
        int fn = 1;
 
        if (n < 0) {
            return -1;
        } else if (n == 0 || n == 1) {
            return n;
        } else {
            for (int i = 2; i < n; i++) {
                f0 = f1;
                f1 = fn;
                fn = f0 + f1;
            }
        }
        return fn;
    }
}



Hoàng Quang Huy [C1907L]
Hoàng Quang Huy

2020-04-13 06:35:01




package Fibonaci;

import java.util.Scanner;

public class Main {

    public static int fibonaci(int n) {
        if (n < 0) {
            return -1;
        } else if (n == 0 || n == 1) {
            return 1;
        } else {
            return fibonaci(n - 1) + fibonaci(n - 2);
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập số max: ");
        int n = scan.nextInt();
        int i = 0;
        System.out.println("Cách 1: ");
        System.out.println("Dãy fibonacci nhỏ hơn " + n + " là: ");
            while(fibonaci(i) <  n) {
                System.out.print(fibonaci(i) + " ");
                i++;
            }
        System.out.println("");
        System.out.println("Cách 3: ");
        int f1 = 1;
        int f2 = 1;
        int f = 0;
        boolean flag = true;
        if(n >= 1){
            System.out.println("Dãy fibonacci nhỏ hơn " + n + " là: ");
            System.out.print(f1 + " " + f2 + " ");
        }else{
            System.out.println("Không có dãy fibonaci thỏa mãn");
            flag = false;
        }
        while(flag){
                f = f1 + f2;
                f1 = f2;
                f2 = f;
                if(f < n){
                    System.out.print(f + " ");
                }else{
                    System.out.println("");
                    break;
                }
            }      
    }
}



trung [C1907L]
trung

2020-04-12 09:32:44



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Fibonaci;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author prdox
 */
public class Fibonaci {
    Integer index,value;
    private static List<Fibonaci> fibList;

    public Fibonaci() {
    }
    
    public int getFib(int n) throws Exception {
        // n > 0
        if (n < 1) {
            throw new Exception("Index have to be larger than 1");
        }
        //loop through fibList to check for exist -> return
        if (fibList == null) fibList = new ArrayList<>();
        for (Fibonaci fib : fibList) {
            if (fib.index == n) return fib.value;
        }
        // if not exist then find
        Fibonaci newF = new Fibonaci();
        if (n == 1 || n == 2) {
            newF.index = 1;
            newF.value = 1;
        }else{
            newF.index = n;
            newF.value = getFib(n-1) + getFib(n-2);
        }
        fibList.add(newF);
        return newF.value;
    }
    
    public int getFibWithoutRem(int n) throws Exception {
        // n > 0
        if (n < 1) {
            throw new Exception("Index have to be larger than 1");
        }
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return getFibWithoutRem(n-1) + getFibWithoutRem(n-2);
        }
    }
    
    public int getFibUsingLoop(int n) throws Exception {
        if (n < 1) {
            throw new Exception("Index have to be larger than 1");
        }
        int n0 = 0,n1 = 1,indx = 0;
        while (++indx < n) {
            n1 += n0;
            n0 = n1 - n0;
        }
        return n1;
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Fibonaci;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Main {
    public static void main(String[] args) {
        Fibonaci fib = new Fibonaci();
        System.out.println("Nhap vao max");
        Scanner inp = new Scanner(System.in);
        int max = Integer.parseInt(inp.nextLine());
        int fiNum,index = 1;
        try {
            fiNum = fib.getFib(index);
            while (fiNum < max) {
                System.out.println(fiNum);
                fiNum = fib.getFib(++index);
            }
            
            index = 1;
            fiNum = fib.getFibUsingLoop(index);
            while (fiNum < max) {
                System.out.println(fiNum);
                fiNum = fib.getFibUsingLoop(++index);
            }
           
            index = 1;
            fiNum = fib.getFibWithoutRem(index);
            while (fiNum < max) {
                System.out.println(fiNum);
                fiNum = fib.getFibWithoutRem(++index);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-04-11 05:21:34



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChuoiFibonaci;

import java.util.Scanner;

/**
 *
 * @author Redmibook 14
 */
public class Fibonaci {

    static int fibonaci(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }
        return fibonaci(n - 1) + fibonaci(n - 2);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter n :");
        int n = input.nextInt();
        for (int i = 1; i < n; i++) {
            System.out.println(fibonaci(i));
        }
        int a1 = 1;
        int a2 = 1;
        int a;
        for (int i = 0; i < n; i++) {
            a = a1 + a2;
            a1 = a2;
            a2 = a;
            System.out.println(a);
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-04-11 02:22:26



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package April10Exam;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Fibbonacci {
    static int dem = 2;
    static List<Integer> array = new ArrayList<>();
    
    public static void main(String[] args) {
        System.out.print("Nhap vao so max: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println("\n\t 1.De quy:");
        fibbonacciDQ(n,1,1);
        System.out.println("\n\t 2.De quy co nho:");
        fibbonacciRemember(n);
        System.out.println("\n\t 3.Khong dung De quy:");
        fibbonacciNon(n);
    }
    
    static void fibbonacciDQ(int n,int n1,int n2){
        if(dem==2){
            if(n==0){
                System.out.println("F(0) = 1");
            }
            if(n>=1){
                System.out.println("F(0) = 1");
                System.out.println("F(1) = 1");
            }
        }
        int n3 = n1 + n2;
        if(n3<=n){
            System.out.println("F("+dem+") = "+n3);
            dem++;
            fibbonacciDQ(n,n2,n3);
        }else{
            dem=2;
        }
    }
    
    static void fibbonacciRemember(int n){
        if(dem==2){
            if(n==0){
                array.add(1);
            }
            if(n>=1){
                array.add(1);
                array.add(1);
            }
        }
        int gt=0;
        if(n!=0){
            gt = array.get(dem-2)+array.get(dem-1);
        }
        if(gt<=n && n!=0){
            array.add(gt);
            dem++;
            fibbonacciRemember(n);
        }else{
            for (int i=0;i<array.size();i++) {
                System.out.println("F("+i+") = "+array.get(i));
            }
            dem=2;
        }
    }
    
    static void fibbonacciNon(int n){
        if(dem==2){
            if(n==0){
                System.out.println("F(0) = 1");
            }
            if(n>=1){
                System.out.println("F(0) = 1");
                System.out.println("F(1) = 1");
            }
        }
        int n1=1,n2=1;
        for(int i=2;;i++){
            int n3=n1+n2;
            if(n3>n){
                break;
            }
            System.out.println("F("+i+") = "+n3);
            n1=n2;
            n2=n3;
        }
        dem=2;
    }
}