Java语言程序设计与数据结构(基础篇)课后练习题 第十章(三)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
10.18

==================================================================

这个代码应该没错,但是运行时间太长了,没等到结果。。。。。。

import java.math.BigInteger;

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

int count = 0;

BigInteger n = new BigInteger(Long.MAX_VALUE + “”);

while (count < 5) {

if (isPrime(n)) {

System.out.print(n + " ");

count++;

}

n = n.add(BigInteger.ONE);

}

}

public static boolean isPrime(BigInteger i) {

for (BigInteger divisor1 = new BigInteger(“2”); divisor1

.compareTo(i.divide(new BigInteger(“2”))) <= 0; divisor1 = divisor1.add(BigInteger.ONE)) {

if (i.remainder(divisor1).compareTo(BigInteger.ZERO) == 0)

return false;

}

return true;

}

}

10.19

==================================================================

import java.math.BigInteger;

public class dishizhang {

public static void main(String[] args) {

System.out.println(“p\t2^p-1”);

for (int i = 1; i <= 100; i++) {

BigInteger mersenne = new BigInteger(index(i) + “”);

BigInteger b1 = new BigInteger(i + “”);

if (isMersennePrime(i) && isPrime(b1)) {

System.out.print(b1);

System.out.print(“\t”);

System.out.println(mersenne);

}

}

}

public static boolean isPrime(BigInteger b) {

boolean r = true;

BigInteger b2 = new BigInteger(2 + “”);

if (b.equals(b2))

r = true;

else if (b.equals(new BigInteger(1 + “”)))

r = false;

else {

for (; b.compareTo(b2) == 1; b2 = b2.add(BigInteger.ONE)) {

if ((b.mod(b2)).equals(new BigInteger(0 + “”))) {

r = false;

continue;

}

}

}

return r;

}

public static boolean isMersennePrime(int i) {

boolean r2 = false;

if (isPrime(index(i)))

r2 = true;

return r2;

}

public static BigInteger index(int i) {

BigInteger i1 = new BigInteger(1 + “”);

BigInteger i2 = new BigInteger(2 + “”);

for (int b = 1; b <= i; b++) {

i1 = i1.multiply(i2);

}

BigInteger f = i1.subtract(BigInteger.ONE);

return f;

}

}

10.20

==================================================================

import java.math.BigDecimal;

import java.math.BigInteger;

public class dishizhang {

public static void main(String[] args) {

dishizhang computeE = new dishizhang();

for (int i=100;i<=1000;i+=100){

computeE.ComputeE(i);

}

}

public static BigDecimal factorial(int number) {

BigDecimal result=BigDecimal.valueOf(1L);

if (number==1){

return result;

}else {

for (int i=1;i<=number;i++){

BigDecimal iVal=BigDecimal.valueOf(i);

result=result.multiply(iVal);

}

return result;

}

}

public BigDecimal ComputeE(int index) {

BigDecimal result=BigDecimal.valueOf(1);

for (int i=1;i<=index;i++){

BigDecimal one=new BigDecimal(1);

BigDecimal tem=one.divide(factorial(i),25,BigDecimal.ROUND_UP);

result=result.add(tem);

}

System.out.println(“i:”+index+" e= "+result.toString());

return result;

}

}

10.21

==================================================================

这个代码应该也没有错误,还是没有等到答案,可能是运行时间太长。

import java.math.BigInteger;

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

BigInteger n = new BigInteger(Long.MAX_VALUE + “”);

int count = 0;

while (count < 10) {

if (divide(n)) {

System.out.print(n + " ");

count++;

}

n = n.add(BigInteger.ONE);

}

}

public static boolean divide(BigInteger i) {

BigInteger s1 = new BigInteger(5+“”);

BigInteger s2 = new BigInteger(6+“”);

BigInteger[] r1 = i.divideAndRemainder(s1);

BigInteger[] r2 = i.divideAndRemainder(s2);

if(r1[1]==BigInteger.ZERO && r2[1] == BigInteger.ZERO)

return true;

return false;

最后

这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档

祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!

感谢大家的支持!!

image.png

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
r2 = i.divideAndRemainder(s2);

if(r1[1]==BigInteger.ZERO && r2[1] == BigInteger.ZERO)

return true;

return false;

最后

这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档

祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!

感谢大家的支持!!

[外链图片转存中…(img-EFErnueS-1714500598233)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 18
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值