用java applet计算葛立恒数
发布网友
发布时间:2022-04-24 02:21
我来回答
共2个回答
热心网友
时间:2023-10-21 16:56
在维基百科看了葛立恒数的定义,尝试写了个程序,不是applet。
这类数增长太快,数太大,个人电脑也没有足够的内存、足够的计算能力去算出这些数。
import java.math.BigInteger;
public class Graham {
public BigInteger graham(int base, int arrow, BigInteger power) {
if (base < 1)
throw new IllegalArgumentException("参数base必须大于等于1。");
if (arrow < 1)
throw new IllegalArgumentException("参数arrow必须大于等于1。");
if (power == null)
throw new IllegalArgumentException("参数power不能为null。");
if (power.compareTo(BigInteger.ONE) < 0)
throw new IllegalArgumentException("参数power必须大于等于1。");
if (base == 1)
return BigInteger.ONE;
if (arrow == 1) {
return power(base, power);
} else {
return graham(base, arrow - 1, graham(base, arrow - 1, power));
}
}
public BigInteger power(int base, BigInteger power) {
if (power.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)
return BigInteger.valueOf(base).pow(power.intValue());
return (BigInteger.valueOf(base).pow(Integer.MAX_VALUE)).multiply(power(base,
power.subtract(BigInteger.valueOf(Integer.MAX_VALUE))));
}
public static void main(String[] args) {
Graham self = new Graham();
System.out.print("graham(2, 2, 2): ");
System.out.println(self.graham(2, 2, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 2): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 3): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(3)));
System.out.print("graham(3, 3, 3): ");
System.out.println(self.graham(3, 3, BigInteger.valueOf(3)));
}
}
2↑↑↑3就已经算不出来了。
热心网友
时间:2023-10-21 16:56
自己写方法吧....
热心网友
时间:2023-10-21 16:56
在维基百科看了葛立恒数的定义,尝试写了个程序,不是applet。
这类数增长太快,数太大,个人电脑也没有足够的内存、足够的计算能力去算出这些数。
import java.math.BigInteger;
public class Graham {
public BigInteger graham(int base, int arrow, BigInteger power) {
if (base < 1)
throw new IllegalArgumentException("参数base必须大于等于1。");
if (arrow < 1)
throw new IllegalArgumentException("参数arrow必须大于等于1。");
if (power == null)
throw new IllegalArgumentException("参数power不能为null。");
if (power.compareTo(BigInteger.ONE) < 0)
throw new IllegalArgumentException("参数power必须大于等于1。");
if (base == 1)
return BigInteger.ONE;
if (arrow == 1) {
return power(base, power);
} else {
return graham(base, arrow - 1, graham(base, arrow - 1, power));
}
}
public BigInteger power(int base, BigInteger power) {
if (power.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)
return BigInteger.valueOf(base).pow(power.intValue());
return (BigInteger.valueOf(base).pow(Integer.MAX_VALUE)).multiply(power(base,
power.subtract(BigInteger.valueOf(Integer.MAX_VALUE))));
}
public static void main(String[] args) {
Graham self = new Graham();
System.out.print("graham(2, 2, 2): ");
System.out.println(self.graham(2, 2, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 2): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 3): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(3)));
System.out.print("graham(3, 3, 3): ");
System.out.println(self.graham(3, 3, BigInteger.valueOf(3)));
}
}
2↑↑↑3就已经算不出来了。
热心网友
时间:2023-10-21 16:56
自己写方法吧....
热心网友
时间:2023-11-12 17:53
在维基百科看了葛立恒数的定义,尝试写了个程序,不是applet。
这类数增长太快,数太大,个人电脑也没有足够的内存、足够的计算能力去算出这些数。
import java.math.BigInteger;
public class Graham {
public BigInteger graham(int base, int arrow, BigInteger power) {
if (base < 1)
throw new IllegalArgumentException("参数base必须大于等于1。");
if (arrow < 1)
throw new IllegalArgumentException("参数arrow必须大于等于1。");
if (power == null)
throw new IllegalArgumentException("参数power不能为null。");
if (power.compareTo(BigInteger.ONE) < 0)
throw new IllegalArgumentException("参数power必须大于等于1。");
if (base == 1)
return BigInteger.ONE;
if (arrow == 1) {
return power(base, power);
} else {
return graham(base, arrow - 1, graham(base, arrow - 1, power));
}
}
public BigInteger power(int base, BigInteger power) {
if (power.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)
return BigInteger.valueOf(base).pow(power.intValue());
return (BigInteger.valueOf(base).pow(Integer.MAX_VALUE)).multiply(power(base,
power.subtract(BigInteger.valueOf(Integer.MAX_VALUE))));
}
public static void main(String[] args) {
Graham self = new Graham();
System.out.print("graham(2, 2, 2): ");
System.out.println(self.graham(2, 2, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 2): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 3): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(3)));
System.out.print("graham(3, 3, 3): ");
System.out.println(self.graham(3, 3, BigInteger.valueOf(3)));
}
}
2↑↑↑3就已经算不出来了。
热心网友
时间:2023-11-12 17:54
自己写方法吧....
热心网友
时间:2023-11-12 17:53
在维基百科看了葛立恒数的定义,尝试写了个程序,不是applet。
这类数增长太快,数太大,个人电脑也没有足够的内存、足够的计算能力去算出这些数。
import java.math.BigInteger;
public class Graham {
public BigInteger graham(int base, int arrow, BigInteger power) {
if (base < 1)
throw new IllegalArgumentException("参数base必须大于等于1。");
if (arrow < 1)
throw new IllegalArgumentException("参数arrow必须大于等于1。");
if (power == null)
throw new IllegalArgumentException("参数power不能为null。");
if (power.compareTo(BigInteger.ONE) < 0)
throw new IllegalArgumentException("参数power必须大于等于1。");
if (base == 1)
return BigInteger.ONE;
if (arrow == 1) {
return power(base, power);
} else {
return graham(base, arrow - 1, graham(base, arrow - 1, power));
}
}
public BigInteger power(int base, BigInteger power) {
if (power.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)
return BigInteger.valueOf(base).pow(power.intValue());
return (BigInteger.valueOf(base).pow(Integer.MAX_VALUE)).multiply(power(base,
power.subtract(BigInteger.valueOf(Integer.MAX_VALUE))));
}
public static void main(String[] args) {
Graham self = new Graham();
System.out.print("graham(2, 2, 2): ");
System.out.println(self.graham(2, 2, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 2): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(2)));
System.out.print("graham(2, 3, 3): ");
System.out.println(self.graham(2, 3, BigInteger.valueOf(3)));
System.out.print("graham(3, 3, 3): ");
System.out.println(self.graham(3, 3, BigInteger.valueOf(3)));
}
}
2↑↑↑3就已经算不出来了。
热心网友
时间:2023-11-12 17:54
自己写方法吧....