Maxcompute factorial-Java version

Source: Internet
Author: User

This section describes the Java program for maxcompute. This section uses dynamic arrays to store intermediate and final results of the computing process. Each short array element represents four decimal numbers. By the way, this is my first Java program.

import java.util.Scanner;/** * * @author liangbch@263.net */public class Fac {      public Fac() {    }        public static void Calc(int n)    {        int RAD=10000;        int buffSize=(int)(n * Math.log10((n+1)/2) / Math.log10(RAD)+1);        short[] buff = new short[buffSize];        int len=1;        buff[0]=1;        for (int i=1;i<=n;i++)        {            int c=0;            for (int j=0;j<len;j++)            {                int prod=(buff[j]*i+c);                buff[j]=(short)(prod % RAD);                c=prod / RAD;            }            while (c>0)            {                buff[len++]= (short)(c % RAD);                c=c/RAD;            }        }                System.out.printf("%d!=%d", n, buff[len-1]);        for (int i=len-2;i>=0;i--)            System.out.printf("%04d",buff[i]);     }    public static void main(String[] args) {       System.out.println("Please input a integer");       Scanner in=new Scanner(System.in);       int n=in.nextInt();       Calc(n);    }}

 

 

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.