第 n 個卡特蘭數根據二項式係數計算,其公式為 (n + k )/k,其中 k 從 2 變化到 n,且 n ≥ 0。即 Cn = (2n)!/((n+1)!n!)程式public class NthCatalanNumber { public static long fact(int i) { if(i
以下是列印數字 n 的第 k 個素因子的 Java 程式,其中給出 k 和 n。程式import java.util.Scanner; public class KthPrimeFactor { public static void main(String args[]) { int number, k, factor = 0; Scanner sc = new Scanner(System.in); System.out.println("輸入一個數字:"); number = sc.nextInt(); System.out.println("輸入 k 值:"); k = sc.nextInt(); int temp = k-1; for(int i = 2; i< number; ... 閱讀更多
以下是列印給定數字的所有因數的 Java 程式。程式import java.util.Scanner; public class DivisorsOfNaturalNumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("輸入所需的數字:"); int num = sc.nextInt(); for(int i = 1; i
程式以下是計算陣列數字的最大公約數的示例。線上演示public class GCDOfArrayofNumbers{ public static int gcd(int a,int b){ int res = 0; while (b > 0){ int temp = b; b = a % b; a = temp; res = a; } return res; } public static void main(String arg[]){ int[] myArray = {3, 6, 8}; int result = gcd(myArray[0],myArray[1]); for(int i = 2; i < myArray.length; i++){ result = gcd(result, myArray[i]); } System.out.println("n 個數字的最大公約數是:"+result); } }輸出n 個數字的最大公約數是:1