求1到20的阶乘的和 以及 1到21的阶乘的和,使用long和BigInteger
来源:互联网 发布:关于套路的网络用语 编辑:程序博客网 时间:2024/05/16 08:53
package cn.sdut.test;import java.math.BigInteger;/* * 求到20阶乘的和 以及 求到21阶乘的和 */public class Test04 {public static void main(String[] args) {int n =21;biginteger(n);longtype(n);System.out.println(Long.MAX_VALUE);//long x = 1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20;//会溢出long x = 1L*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20;//不会溢出
System.out.println("x="+x);
}private static void longtype(int n) {long sum = 0;long value =1;for(int i = 1;i<=n;i++){value*=i;sum+=value;}System.out.println(sum);}private static void biginteger(int n) {BigInteger sum = new BigInteger("0");BigInteger value = new BigInteger("1");for(int i =1;i<=n;i++){BigInteger temp = new BigInteger(i+"");value =value.multiply(temp);sum =sum.add(value);}System.out.println(sum.toString());}}
阅读全文
0 0
- 求1到20的阶乘的和 以及 1到21的阶乘的和,使用long和BigInteger
- 1到n的和、阶乘以及阶乘的和
- 求1到20的阶乘的和
- 求1到20的阶乘和(c++)
- 求1到给定数的阶乘的和
- java 求1到n的阶乘和
- java求1的阶乘到10的阶乘的叠加和
- 1到n的阶乘和
- 1到10的阶乘和
- 求阶乘的和
- 求1到20的阶乘之和
- 求1到20的阶乘之和
- 第十周 计算1到20的阶乘的和
- java方法计算1到20的阶乘的和
- 第三章习题3--第五题求1到20的阶乘的和
- 求1~10的阶乘的和
- 九九乘法表和1到10的阶乘和
- 程序求1!+2!+3!+...+1000!的和, 求1到1000阶乘之和
- java中经常遇到的Object与List<Object>取参数麻烦问题
- Unity中OnCollisionStay2D的使用
- 【LEDE】树莓派上玩LEDE终极指南-93-搭建MySQL Server
- MySQL优化-03
- 深度学习中的代码资源库------图像处理篇
- 求1到20的阶乘的和 以及 1到21的阶乘的和,使用long和BigInteger
- sql日期格式化
- ORACLE冷备文件恢复
- ArrayBlockingQueue源码分析
- shiro快速入门
- 解决websocket中localhost改为ip地址连接不上
- Springcloud框架搭建
- 连接企业无线网络时需要频繁输入用户密码的解决办法
- latex中局部使用中文环境