Factorial is a very interesting function, the general solution has these:
Package com.threetop.www;
Methods for solving factorial public class Jiechen {//Method one: Recursive solution public static int FAC (int n) {int sum; if (n==0| |
N==1) {return 1;
} SUM=FAC (n-1) *n;
return sum;
}//Method Two: Non-recursive solution (recursive solution) public static int Fac2 (int n) {int sum=1; if (n==0| |
N==1) {return 1;
for (int i=2;i<=n;i++) {sum*=i;
return sum;
}//Similar to the method of the binary (Gaussian additive method) public static int fac3 (int n) {int begain=1,sum=1; if (n==0| |
N==1) {return 1;
} while (true) {if (begain==n)//If the same number is reached, the product of only one number {sum*=n; else {sum*= (begain*n); Two-way both sides do product operations} if ((N-begain) ==1| |
(n-begain) ==0)//termination conditions are either adjacent to two digits or equal to {return sum; } begain++;
Two numbers to the middle close to n--;
} public static void Main (String []args) {System.out.println (FAC (10));
System.out.println (FAC2 (10));
System.out.println (FAC3 (10));
}
}
The results of the operation are as follows:
Two. Question 1: Given an integer n, then n's factorial n. How many 0 are there at the end? For example: N=10,n. =3 628 800,n. Has two 0 at the end.
Question 2: Find N. The position of the lowest bit 1 in the binary representation
The specific implementation is as follows:
Import Java.util.Scanner;
public class Test
{public
static void Main (String args[])
{
Scanner in = new Scanner (system.in);
System.out.print ("Please enter a number that requires factorial:");
int N = In.nextint ();
int n = n;
int sumten = 0;
int sumtwo = 0;
for (int i=1; i<=n; i++)
{
int k = i;
while (k%5 = = 0)//integer I can be divisible by 5 of the number
{
Sumten = sumten + 1;
K = K/5;
}
int j = i;
while (j%2 = = 0)//integer I can be divisible by 2 of the number
{
sumtwo = sumtwo + 1;
j = j/2;
}
}
The end of SYSTEM.OUT.PRINTLN (n+ "!):" +sumten+ "0");
SYSTEM.OUT.PRINTLN (n+ "!) the position of the lowest bit 1 in the binary representation:" +sumtwo);
}
The results of the operation are as follows: