批处理bat递归计算N!的实现代码有哪些?

批处理bat递归计算N!的实现代码有哪些?

核心代码:

@echo off
:: 代码来自网友:liangbch
setlocal enabledelayedexpansion

echo To calculate n^^!
set/p nn=n=?

set/a "buff_1=1,rad=10000,len=1" 

for /l %%i in (1,1,%nn% ) do (
  set/a cc=0
  for /l %%j in (1,1,!len!) do (
   set/a "tt=buff_%%j* %%i+!cc!"
   set/a "buff_%%j=!tt! %% !rad!,cc=!tt!/!rad!"
  )
  if !cc! gtr 0 (
   set/a len+=1
   set/a buff_!len!=!cc!
  ) 
)

set/p xx=n^^!=<nul
set/a tt=buff_%len%
set/p xx=!tt!<nul
set/a len-=1

for /l %%i in (%len%,-1,1 ) do (
 set/a tt=buff_%%i
 if !tt! lss 1000 set tt=00000!tt!
 set/p xx=!tt:~-4!<nul
)

echo.
pause

双击输入数字即可。