Maxcompute factorial-Pascal

Source: Internet
Author: User

First, provide the Code:

program fac;var   i,j,n,len: integer;   rad,c,r: longint;   buff: array[1..16000] of integer;   num: string[5];begin   writeln('calculate n!');   write('n=?');   read(n);   rad:=10000;   len:=1;   buff[1]:=1;   for i:=1 to n do   begin      c:=0;      for j:= 1 to len do      begin        r := buff[j] * i +c;        buff[j] := r mod rad;        c := r div rad;      end;      if c>0 then      begin        len:=len+1;        buff[len]:=c;      end;   end;   write('n!=',buff[len]);   for i:=len-1 downto 1 do   begin       str(buff[i],num);       if length(num)<4 then          write(copy('0000',1,4-length(num)));       write(buff[i]);   end;END.

Note:
Development Environment: Free Pascal 2.4 (a DOS program, similar to Turbo Pascal)
Download Page: http://down.tech.sina.com.cn/page/46658.html
Free Pascal settings:
1. the DOS window code page is set to 437 (USA). Otherwise, garbled characters will occur.
2. Options-> compiler mode is set to Turbo Pascal compatible
Learning materials:
Pascal language training courses, download page http://www.05sun.com/downinfo/5433.html

Download, install free Pascal. It takes about two hours to learn, compile, and debug the program.
Problems:
1. Data overflow caused by the definition of variable R as integer
2. If the for keyword is deduplicated and changed to downto, everything is normal.

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.