以下是Java程序,当给出k和n时,该程序将打印出数字n的第k个素数。
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; i++) { while(number%i == 0) { if(temp == 0) { factor = i; } temp--; System.out.println(i+" "); number = number/i; } } System.out.println(k+"t给定数字的h素数是:"+factor); if(number >2) { System.out.println(number); } } }
输出结果
输入数字: 216 输入k值: 3 2 2 2 3 3 3 3t给定数字的h素数是:2