Calculate the factorial of n. the maximum number is 9999!

Source: Internet
Author: User

C language core (Source Network ):

# Define N 8 // calculate the factorial of n. the maximum number is 9999 <br/> long a [8916] = {}, N, I, C, Len; <br/> int main () <br/> {<br/> N = N; <br/> for (LEN = 1; n> 1; n --) <br/> {<br/> for (C = 0, I = 0; I <Len; I ++) <br/>{< br/> long P = A [I] * n + C; <br/> A [I] = P % 10000; <br/> C = P/10000; <br/>}< br/> A [I] = C; <br/> If (C> 0) <br/> Len ++; <br/>}< br/> printf ("% d", a [len-1]); <br/> for (Len --; len> = 0; Len --) <br/> printf ("% 04d", a [Len]); <br/> system ("pause "); <br/> return 0; <br/>}< br/>I used the Assembly to rewrite it:; $ $ <Br/>; -=-factorial by G-spider @ 2010 <br/>;-=-ml/C/coff factorial. ASM <br/>;-=-link/subsystem: Console factorial. OBJ <br/>;-=-invoke MessageBox, 9999, <br/>;-=-_ factorial function can be up! <Br/>;-=-exit by Q or q <br/>; $ $ <br/>. 386 <br/>. model flat, stdcall </P> <p> include user32.inc <br/> include kernel32.inc <br/> include msvcrt. INC </P> <p> includelib user32.lib <br/> includelib kernel32.lib <br/> includelib msvcrt. lib <br/>. data <br/> fmt0 dB '% d! = % D', 0 <br/> fmt1 dB '% 04d', 0 <br/> fmt2 dB 0dh, 0ah, 0 <br/> Inform dB 'Please input num (0 ~ 999): ', 0dh, 0ah, 0 <br/> outform dB 'Please re-input num 0 ~ 999 or Q-Exit: ', 0dh, 0ah, 0 <br/> szpause dB 'pause', 0 <br/> dwstrlen dd 8916 </P> <p>. data? <Br/> lpoutputbuf dd? <Br/> Len dd? <Br/>. code <br/>; * ************************** </P> <p> _ factorial proc LPA: DWORD, N: DWORD <br/> Local @ C, @ Len </P> <p> mov @ Len, 1 <br/> mov ESI, LPA <br/> cmp n, 0 <br/> je exit <br/> loop1: <br/> mov @ C, 0 <br/> xor ebx, EBX <br/>: <br/>; P = A [I] * n + C; <br/> mov eax, [ESI + EBX * 4] <br/> imul eax, n <br/> Add eax, @ C </P> <p> CDQ <br/> mov ECx, 10000; base: 10000 <br/> idiv ECx <br/> mov [ESI + EBX * 4], EDX <br/> mov @ C, eax </P> <p> Inc EBX <br/> cmp ebx, @ Len <br/> JB @ B </P> <p> mov [ESI + EBX * 4], eax <br/> CMP eax, 0 <br/> jbe a00 <br/> Inc @ Len <br/> a00: <br/> dec n <br/> cmp n, 1 <br/> ja loop1 <br/> Exit: <br/> mov eax, @ Len <br/> RET <br/> _ factorial endp <br/>; **************************** <br/> _ INPUT proc <br/> Local @ num <br/> mov @ num, 0 <br/>@:< br/> invoke crt_getchar <br/> mov EBX, eax </P> <p>. if EBX = 'q' | EBX = 'q' <br/> invoke exitprocess, 0 </P> <p>. elseif EBX> = '0' & EBX <= '9' <br/> mov ECx, @ num <br/> imul ECx, 10 <br/> sub EBX, '0' <br/> Add ECx, EBX <br/> mov @ num, ECx <br/>. elseif EBX = 0ah <br/> mov eax, @ num <br/>. if eax <0 | eax> 999;> 9999 <br/> invoke crt_printf, offset outform <br/> mov @ num, 0 <br/> JMP @ B <br/>. endif <br/> RET <br/>. endif <br/> JMP @ B <br/> _ INPUT endp </P> <p>; * *************************** <br/> _ output proc lpbuf: DWORD, @ num: DWORD <br/> invoke _ factorial, lpbuf, @ num <br/> dec eax <br/> push ESI <br/> mov ESI, lpbuf </P> <p> invoke crt_printf, offset fmt0, @ num, dword ptr [ESI + eax * 4] <br/> pop ESI <br/> pop eax <br/> test eax, eax <br/> JZ exit <br/> dec eax <br/>: <br/> push eax <br/> push ESI <br/> invoke crt_printf, offset fmt1, dword ptr [ESI + eax * 4] <br/> pop ESI <br/> pop eax <br/> dec eax <br/> JNS @ B <br/> Exit: <br/> invoke crt_printf, offset fmt2 <br/> XOR eax, eax <br/> RET <br/> _ output endp <br/>; * **************************** </P> <p> start: <br/> invoke crt_printf, offset inform <br/> mov eax, dwstrlen <br/> SHL eax, 2 <br/> mov dwstrlen, eax <br/> invoke crt_malloc, eax <br/> mov lpoutputbuf, eax <br/>: <br/> invoke rtlzeromemory, lpoutputbuf, dwstrlen <br/> mov ESI, lpoutputbuf <br/> mov dword ptr [esi], 1 </P> <p> invoke _ INPUT <br/> invoke _ output, lpoutputbuf, eax <br/> JMP @ B </P> <p> end start <br/>

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.