這其實是兩道非常基礎和簡單地題。但somehow每隔一段時間我老是會不經意地想起這兩個問題,有時候卡克沒有一下想起解法還會急的直冒汗...................
言歸正傳,貼出這兩題代碼
(1)打印斐波拉契數列
// Java program for Fibonacci number using Loop. public static int fibonacciLoop(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number }
public static void prime( int number) { for (int i=2; isqrt(i)) { System.out.println(i); } } return 0; }