본문 바로가기

JavaCode(review)

nSum(int n), factorial(int n) 계승, fibonacci(int n) 수열 코딩

반응형

문제는 하단에

 

nSum, factorial, fibonacci method

package my.com;

public class Series {

        public static long nSum(int n){
            return (n*(n+1)) / 2;       // 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.
        }

        public static long factorial(int n){        //계승 > 0!=1(약속), 3!=3*2*1, 4!=4*3*2*1, 2!
            if(n == 0){							// 3!=3*2!  ,4!=4*3! 
                return 1;
            }
            long fact = 1;                 //i = n
            for(int i=1; i<=n; i++){	//       	(n)
                fact *= i;             // fact = fact * i // 변수 fact에 곱한 후, 다시 변수 fact에 대입>
            }			 // 1! <- 1 =  1   *   1
            return fact ;	 // 2! <- 2 =  1!  *   2         
        }			// 3! <- 6 =  2!  *   3
        
    //facotrial method 함수 이용표현			//5!=5*4! = 5*4*3*2*1
   //     if(n == 1) return 1;
  //      else{
  //		return n * factorial(n-1);	}

        public static long fibonacci(int n){         //수열
                                             //F(0)=0, F(1)=1, F(n)= F(n-2) +F(N-1)
            if(n==0){                                         //F(2) = F(0)+F(1) = 1 항상
                return 0;
            } else if(n==1){
                return 1;
            }else{                  //n>1일때 // 2부터시작
                long fn0 = 0;       //f(0)=0 //initialization : n이 최소한 >= 2이니
                long fn1 = 1;       //f(1)=1
                long fn = 0;               //변수 fib initialization
                for(int i=1; i<n; i++){
                    fn = fn0 + fn1 ;
                    fn0 = fn1 ;
                    fn1 = fn;
                }
                return fn;
            }           // for문 이용안하고 걍 return fibonacci(n-2) + fibonacci(n-1) ; 으로 하기도/

        }

 

Main - execute

package my.com;

import my.com.Series;

public class Main {

    public static void main(String[] args) {
	    for(int n=0; n<=10; n++){               // n = 0~10  출력
            System.out.println(Series.nSum(n)); //Class.method
        }

        System.out.println("********************************");     //n= 0~10일때 출력
	    for(int n=0; n<=10 ; n++){
            System.out.println(Series.factorial(n));        //class method
        }
        System.out.println("******************************");
	    for(int n=0; n<=10 ; n++){                          // n =0~0일때 출력
            System.out.println(Series.fibonacci(n));        //class.method
        }
    }
}


Output

D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=51350:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\NewProject\out\production\NewProject;D:\IT\NewProject\Series\out\artifacts\Series_jar\Series.jar my.gabin.com.Main
0
1
3
6
10
15
21
28
36
45
55
********************************
1
1
2
6
24
120
720
5040
40320
362880
3628800
******************************
0
1
1
2
3
5
8
13
21
34
55

Process finished with exit code 0

 

// // Create a suitably named package containing a class called Series
// // with the following static methods:
// // nSum(int n) returns the sum of all numbers from 0 to n. The first 10 numbers are:
// // 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.
// //
// // factorial(int n) returns the product of all numbers from 1 to n
// // i.e. 1 * 2 * 3 * 4 ... * (n - 1) * n.
// // The first 10 factorials are:
// // 0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800.
// //
// // fibonacci(n) returns the nth Fibonacci number. These are defined as:
// // f(0) = 0
// // f(1) = 1
// // f(n) = f(n-1) + f(n-2)
// // (so f(2) is also 1. The first 10 fibonacci numbers are:
// // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.
// //
// // When you have tested your functions, delete the Main class and
// // produce a jar file.
// //
// // Create a new project and add your Series library, then test the
// // three methods in the main() method of your new project.

'JavaCode(review)' 카테고리의 다른 글

Account coding / access modifier 'private' example  (0) 2020.06.28
Scope 개념 코딩  (0) 2020.06.28
자바 연산  (0) 2020.06.27
Generics Challenge - 기본예제에 league 추가  (0) 2020.06.24
Naming convention  (0) 2020.06.24