展开全部
import java.math.BigDecimal;
import java.util.Random;
public class Increase {
public static boolean isPrime(int a) {
e69da5e6ba903231313335323631343130323136353331333337393462boolean flag = true;
if (a
return false;
} else {
for (int i = 2; i <= Math.sqrt(a); i++) {
if (a % i == 0) {// 若能被整除,则说明不是素数,返回false
flag = false;
break;// 跳出循环
}
}
}
return flag;
}
public static void test1() {
int i,n,k=0;
System.out.println("1-1000内素数");
for (n = 3; n<=1000; n++) { //3~1000的所有数
i=2;
while (i
if (n%i==0) break; //若能整除说明n不是素数,跳出当前循环
i++;
}
if (i==n) { //如果i==n则说明n不能被2~n-1整除,是素数
k++; //统计输出数的个数
System.out.print(i+ "\t ");
if (k %6==0) //每输出5个则换行
System.out.println();
}
}
}
public static void test2()
{
Random r = new Random();
System.out.println(r.nextInt(301)-100);
}
public static int test3(int m,int n){
//辗转相除法
int r;
do{
if(m
{
r = m;
m = n;
n = r;
}
r = m%n;
m = n;
n = r;
}while(r!=0);
return m;
}
public static double test4(int n){
double e=1f;
double total=1.0;
for(int i = 0; i
{
total /= i+1;
e+=total;
}
BigDecimal b = new BigDecimal(e);
e = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); // 表明四舍五入,保留四位小数
return e;
}
public static void main(String[] args) {
//第一题测试
System.out.println(isPrime(131));
test1();
//第二题测试
test2();
//第三题测试
System.out.println("最大公约数为:"+test3(1302,19924));
//第四题测试
System.out.println("e="+test4(100));
}
}
运行结果:
true
1-1000内素数
3 5 7 11 13 17
19 23 29 31 37 41
43 47 53 59 61 67
71 73 79 83 89 97
101 103 107 109 113 127
131 137 139 149 151 157
163 167 173 179 181 191
193 197 199 211 223 227
229 233 239 241 251 257
263 269 271 277 281 283
293 307 311 313 317 331
337 347 349 353 359 367
373 379 383 389 397 401
409 419 421 431 433 439
443 449 457 461 463 467
479 487 491 499 503 509
521 523 541 547 557 563
569 571 577 587 593 599
601 607 613 617 619 631
641 643 647 653 659 661
673 677 683 691 701 709
719 727 733 739 743 751
757 761 769 773 787 797
809 811 821 823 827 829
839 853 857 859 863 877
881 883 887 907 911 919
929 937 941 947 953 967
971 977 983 991 997 106
最大公约数为:2
e=2.7183