é«éé¢æ°ï¼
Pythonã ã¨ãããªæãã«æ¸ãã¾ãã
from itertools import imap N = 100 s = sum(xrange(1, N + 1)) s2 = sum(imap(lambda n: n * n, xrange(1, N + 1))) print s * s - s2
sumã¯reduceã使ãã°ã
N = 100 s = reduce(lambda x, y: x + y, xrange(1, N + 1), 0) s2 = reduce(lambda x, y: x + y * y, xrange(1, N + 1), 0) print s * s - s2
ãããçä¼¼ã§ããªãã§ããããã
reduceã第2å¼æ°ãfirstã第3å¼æ°ãlastã¨ããé¢æ°ã¨èãã¾ãã
:reduce setlocal set f=%1 set /a first = %2 set /a last = %3 set /a init = %4 if %first% == %last% exit /b %init% call :%f% %init% %first% set /a next = %first% + 1 call :reduce %f% %next% %last% %ERRORLEVEL% exit /b %ERRORLEVEL%
ããã§ã第1å¼æ°ã¯é¢æ°ã®ã©ãã«ãæå®ãã¾ããããã§å¼æ°ã®é¢æ°ãå¼ã³åºããã¨ãã§ãã¾ããé¢æ°ãå¼æ°ã«ãã¦ããã®ã§ã¯ãªãããã¾ã§ã©ãã«ã渡ãã¦ããã ãã§é«éé¢æ°ã¨ããããã§ã¯ãªãã§ããã
@echo off set /a N = 100 set /a M = %N% + 1 call :reduce add 1 %M% 0 set /a s = %ERRORLEVEL% call :reduce add_sq 1 %M% 0 set /a s2 = %ERRORLEVEL% set /a result = %s% * %s% - %s2% echo %result% exit /b 0 :add setlocal set /a s = %1 + %2 exit /b %s% :add_sq setlocal set /a s = %1 + %2 * %2 exit /b %s% :reduce setlocal set f=%1 set /a first = %2 set /a last = %3 set /a init = %4 if %first% == %last% exit /b %init% call :%f% %init% %first% set /a next = %first% + 1 call :reduce %f% %next% %last% %ERRORLEVEL% exit /b %ERRORLEVEL%