loading
본문 바로가기
Knowledge/기초수학

[ BASIC MATH ] 05. 점화식과 재귀함수 with java

by NeuLyeo 2023. 11. 28.

[ BASIC MATH ] 05. 점화식과 재귀함수 with java

 

 

 

 

📚 Table of Contents

     

     

     

     

    개념

     

    [ BASIC MATH ] 05. 점화식과 재귀함수

    [ BASIC MATH ] 05. 점화식과 재귀함수 📚 Table of Contents 점화식 ( Recurrence ) 점화식 이란? 수열의 각 항 간에 관계를, 간단하게 표현하는 관계식 ( 단순 나열이 아닌 규칙으로 ) 수열의 N번째 항을, 그

    leungnyeok.tistory.com

     

     

     

     

     

    피보나치 수열

     

    public class Main {
    
        // 재귀함수
        static int recursion(int n) {  
            if (n < 3) {  
                return 1;  
            }  
            return recursion(n - 2) + recursion(n -1);  
        } 
    
        public static void main(String[] args) {
    
        // 반복문
        if (n <= 2) {
            result = 1;
    
        } else {
            for (int i = 2; i < n; i++) {
                result = a1 + a2;
                a1 = a2;
                a2 = result;
            }
        }
    
        System.out.println(result);  // 8
        System.out.println(recursion(n)); // 8
        }
    }

     

     

     

     

     

    팩토리얼

     

    public class Main {
    
        // 재귀함수
        static int factorial(int n) {
            if (n == 1) {  
                return 1;  
            }  
            return n * factorial(n - 1);  
        }  
    
        public static void main(String[] args) {  
            System.out.println(factorial(1));  // 1
            System.out.println(factorial(2));  // 2
            System.out.println(factorial(3));  // 6
            System.out.println(factorial(4));  // 24
            System.out.println(factorial(5));  // 120
        }