2.1
import java.util.Scanner;
public class suanfa {
public static void main(String args[]) {
long k1;
Scanner scan = new Scanner(System.in);
System.out.println("Enter one number:");
k1 = scan.nextInt();
isPrime(k1);
}
public static int isPrime(long a) {
boolean e = true;
for (long i = a - 1; i > 1; i--) {
if (a % i == 0) {
e = false;
}
}
if (e) {
System.out.println(1);
} else
System.out.println(0);
return 0;
}
}
2.2
import java.util.Random;
import java.util.Scanner;
public class suanfa {
public static void main(String args[]) {
int k1;
Scanner scan = new Scanner(System.in);
System.out.println("Enter one number:");
k1 = scan.nextInt();
System.out.println(createRndInteger(k1));// 生成随机字节
}
public static long createRndInteger(int n) {
int a = 0;
// 判断输入的值
if (n <= 0)
return 0;
StringBuffer result = new StringBuffer("1");
for (int i = 0; i < n - 1; i++) {
boolean b = new Random().nextBoolean();
if (b) {
a = 1;
} else
a = 0;
result.append(a);
}
return Long.parseLong(result.toString());
}
}