java 阶乘算法_Java 实现阶乘算法

java 阶乘算法_Java 实现阶乘算法Java实现阶乘算法阶乘算法如下:以下列出0至20的阶乘:0!=1,(0的阶乘是存在的)1!=1,2!=2,3!=6,4!=24,5!=120,6!=720,7!=5040,8!=403209!=36288010!=362880011!=3991680012!=47900160013!=622702080014!=8717829120015!=130767436800016!=2092…

大家好,又见面了,我是你们的朋友全栈君。

Java 实现阶乘算法

阶乘算法如下:

以下列出 0 至 20 的阶乘:

0!=1,(0 的阶乘是存在的)

1!=1,

2!=2,

3!=6,

4!=24,

5!=120,

6!=720,

7!=5040,

8!=40320

9!=362880

10!=3628800

11!=39916800

12!=479001600

13!=6227020800

14!=87178291200

15!=1307674368000

16!=20922789888000

17!=355687428096000

18!=6402373705728000

19!=121645100408832000

20!=2432902008176640000

而当 n≥5 时,n!的个位数字都是0。

java代码实现

package com.leo.kang.interview;

import java.math.BigDecimal;

public class Factorial {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(“——–递归算法——-“);

System.out.println(factorialRecursive(20));

System.out.println(“——–循环算法——-“);

System.out.println(factorialLoop(25));

System.out.println(“——–BigDecimal算法——-“);

System.out.println(factorial(new BigDecimal(100)));

}

/**

* 递归实现阶乘算法

*

* @param n

* @return

*/

public static long factorialRecursive(int n) {

// 阶乘对整数才有意义

if (n < 0) {

return -1;

}

// 0!=1,(0 的阶乘是存在的)

if (n == 0) {

return 1;

}

if (n < 2)

return n * 1;

return n * factorialRecursive(n – 1);

}

/**

* 循环实现阶乘算法

* @param n

* @return

*/

public static long factorialLoop(int n) {

// 阶乘对整数才有意义

if (n < 0) {

return -1;

}

// 0!=1,(0 的阶乘是存在的)

if (n == 0) {

return 1;

}

// 初始值必须为1才有意义

long result = 1;

for (int i = n; i > 0; i–) {

result *= i;

}

return result;

}

public static BigDecimal factorial(BigDecimal n){

BigDecimal bd1 = new BigDecimal(1);//BigDecimal类型的1

BigDecimal bd2 = new BigDecimal(2);//BigDecimal类型的2

BigDecimal result = bd1;//结果集,初值取1

while(n.compareTo(bd1) > 0){//参数大于1,进入循环

result = result.multiply(n.multiply(n.subtract(bd1)));//实现result*(n*(n-1))

n = n.subtract(bd2);//n-2后继续

}

return result;

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163989.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Not enough information to list image symbols. Not enough information to list load addresses in …「建议收藏」

    Not enough information to list image symbols. Not enough information to list load addresses in …「建议收藏」linking…..\Objects\BBQ_Wifi.axf:Error:L6218E:UndefinedsymbolClear_Led_Timer(referredfrommain.o)…\Objects\BBQ_Wifi.axf:Error:L6218E:UndefinedsymbolGet_Led_Timer(referredfrommain.o…

    2022年9月17日
    6
  • element-ui中表格获取当前行的索引index[通俗易懂]

    element-ui中表格获取当前行的索引index[通俗易懂]前言弄文件上传时,需要对上传列表的文件进行一定的操作,例如暂停/取消等等,因为我是使用element-ui中表格展示上传文件列表的,这时的操作却需要使用到当前行的索引下,如何获取索引就是我接下来要做的工作了:获取当前行的索引index使用scope.$index,scope.row即可实现获取索引<el-table-columnlabel=”排序”min-width=”100″><templateslot-scope=”scope”>{{sco

    2025年8月29日
    5
  • maven快照更新策略_eclipse更新maven包

    maven快照更新策略_eclipse更新maven包1、为什么会有快照?开发中,A项目依赖于项目B,没有快照时,B每次改动我们就需要赋予给他一个新版本号,然后在A的pom.xml中修改B的版本,这不仅浪费版本号,而且会带来很多的沟通成本。快照就是为了解决这个问题而生的,每次B发布到私服,maven都会将B打上时间戳,A更新时会检查B的时间戳,如果晚于本地仓库B的时间戳,那么就会进行更新。2、快照更新策略注意,快照并不是每次ins

    2022年10月4日
    2
  • java——Scanner中nextLine()方法和next()方法的区别

    java——Scanner中nextLine()方法和next()方法的区别        遇到一个有意思的东西,在整理字符串这块知识的时候,发现我在用Scanner函数时,在字符串中加入空格,结果空格后面的东西没有输出来(/尴尬),不多说直接上代码:importjava.util.Scanner;//Scanner中nextLine()方法和next()方法的区别publicclassScannerString{publicstatic…

    2022年4月27日
    44
  • scope=prototype有什么作用_prototype设计模式

    scope=prototype有什么作用_prototype设计模式@Scope(“prototype”)//多例模式

    2022年8月19日
    8
  • SpringCloud搭建Eureka集群

    SpringCloud搭建Eureka集群介绍上一篇讲了下如何搭建Eureka服务注册中心,那个是单机版的,本篇介绍下集群版搭建。搭建Eureka集群1.新建模块:cloud-eureka-server7002参考cloud-eureka-server7001新建模块cloud-eureka-server7002。2.修改hosts文件路径:C:\Windows\System32\drivers\etc\hosts,在最后面追加以下内容:#springcloud127.0.0.1eureka7001.com127.0.

    2022年5月1日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号