Don't be intimidated by factorial to calculate the factorial

Source: Internet
Author: User

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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.