1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 System.out.printf("%s\t%s\n", "p", "2^p - 1"); 6 7 for(int i = 2; i <= 31; i++) 8 { 9 if(isPrime((int)getMersenne(i))) 10 System.out.printf("%d\t%d\n", i, (int)getMersenne(i)); 11 } 12 } 13 14 public static double getMersenne(int n) 15 { return Math.pow(2, n) - 1; } 16 17 public static boolean isPrime(int n) 18 { 19 if(n == 1) 20 return false; 21 else if(n == 2) 22 return true; 23 else 24 { 25 for(int i = 2; i < n; i++) 26 { 27 if(n % i == 0) 28 return false; 29 } 30 return true; 31 } 32 } 33 }
时间: 2024-10-05 05:31:15