本文目錄一覽:
- 1、java怎麼寫求階乘?
- 2、java 的階乘問題
- 3、java輸入一個數n,計算n的階乘(5的階乘=1*2*3*4*5)
- 4、Java 求階乘問題
- 5、java階乘的演算法是什麼?
- 6、java階乘問題?
java怎麼寫求階乘?
親測可用
long jiecheng(int x)
{
long int i,k=1;
for(i=1;i=x;i++)
k=k*i;
return k;
}
int main()
{
long int j,k=0;
int i;
for(i=1;i=20;i++)
{
j=jiecheng(i);
k+=j;
}
printf(“%ld\n”,k);
}
輸出的結果是2561327494111820313
擴展資料:
一個正整數的階乘(factorial)是所有小於及等於該數的正整數的積,並且0的階乘為1。自然數n的階乘寫作n!。1808年,基斯頓·卡曼引進這個表示法。
亦即n!=1×2×3×…×n。階乘亦可以遞歸方式定義:0!=1,n!=(n-1)!×n。
計算方法:
大於等於1:
任何大於等於1 的自然數n 階乘表示方法:n! = 1×2×3×…×(n-1)n或n! = n×(n-1)!
0的階乘:0!=1。
參考資料:百度百科——階乘
java 的階乘問題
public static void main(String[] args){
pintResult();
}
public static void pintResult()
{
System.out.println(“請輸入要求階乘的數: “);
Scanner str = new Scanner(System.in);
long a = str.nextLong();
long b =factorial(a);
System.out.println(a+”! = ” + b);
pintResult();
}
public static long factorial(long n){
if((n == 1) || (n==0)){
return 1;
}else{
return n * factorial(n – 1 );
}
}
java輸入一個數n,計算n的階乘(5的階乘=1*2*3*4*5)
1、首先要理解一下階乘的公式:
n! =n*(n-1)*(n-2)*….*2*1, 5! = 5*4*3*2*1
#include //頭文件stdio.h在新浪博客中無法顯示加上就可以了
int main()
{
int t=5,i=4; //要是求其他的數的階乘的話,把t的值改為其他數,
//再把i改為(t-1)就行了
while(i=1)
{
t=t*i;
i–;
}
printf(“5的階乘結果是:%d\n”,t);
return 0;
}
2、運行結果如下:
3、上面這種方法雖然能求出結果,但是不能求任意的數,也沒有考慮到0!=1,這種情況,我們來改進一下;
#include // //頭文件stdio.h在新浪博客中無法顯示加上就可以了
int main()
{
int n,jc;
int jiecheng(int j);
printf(“請輸入任意一個整數\n”);
scanf(“%d”,n);
jc=jiecheng(n);
printf(“該數的階乘結果是:%d\n”,jc);
return 0;
}
int jiecheng(int j)
{
int i=j-1;
if(j==0 | j==1) // 因為0的階乘是1 ,1的階乘也是1
j=1;
while(i1) //
{
j=j*i;
i–;
}
return(j);
}
4、運行結果如下:
Java 求階乘問題
12!=479001600 (4億多)
13!=6227020800(62億多)
而java 中int一般是32位的,表示的值的範圍是-21億多到+21億多,因此從13 的階乘開始,int型就表示不了了。long 的表示範圍也有限。
java階乘的演算法是什麼?
public class Factorial { public static int factorial(int x) { if (x 0) { throw new IllegalArgumentException(x must be=0); } int fact = 1; for (int i = 2; i = x; i++) { fact *= i; } return fact; } public static void main(String args[]) { System.out.print(factorial(10)); }}這個是利用遞歸演算法製成的。public class factorial2 { public static int factorial2(int x) { if (x 0) { throw new IllegalArgumentException(x must be=0); } if (x = 1) { return 1; } else return x * factorial2(x – 1); } public static void main(String args[]) { System.out.print(factorial2(17)); }}這個是數組添加的方法製成的,可以計算更大的階乘。public class Factorial3 { static long[] table = new long[21]; static {table[0] = 1; } static int last = 0; public static long factorial(int x) throws IllegalArgumentException { if (x = table.length) { throw new IllegalArgumentException(Overflow; x is too large.); } if (x = 0) { throw new IllegalArgumentException(x must be non-negative.); } while (last x) { table[last + 1] = table[last] * (last + 1); last++; } return table[x]; } public static void main(String[] args) { System.out.print(factorial(4)); }}最後一個是利用BigInteger類製成的,這裡可以用更大的更大的階乘。import java.math.BigInteger;import java.util.*;public class Factorial4{ protected static ArrayList table = new ArrayList(); static{ table.add(BigInteger.valueOf(1));} public static synchronized BigInteger factorial(int x){ for(int size=table.size();size=x;size++){ BigInteger lastfact= (BigInteger)table.get(size-1); BigInteger nextfact= lastfact.multiply(BigInteger.valueOf(size)); table.add(nextfact); } return (BigInteger) table.get(x); } public static void main(String[] args) { System.out.print(factorial(4)); } }其實方法還有很多,這裡提供的也算是個框架形式。分享之
java階乘問題?
像你這個要設置一個最小值。用if,
當它階乘到了零的時候都應該停止運算。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/279468.html