バッチファイルでProject Euler(31)

Problem 12


三角数をふつうに素因数分解していたのでは間に合いません。

@echo off

call :start_time
set /a L = 50
set /a n = 1
:loop
    set /a m = %n% * (%n% + 1) / 2
    call :num_divs %m%
    set /a n += 1
    if %ERRORLEVEL% LSS %L% goto :loop
echo %m%
call :check_time
exit /b 0

:num_divs
    setlocal
    set /a num = 1
    set /a n = %1
    set /a p = 2
    :loop_num_divs
        set /a e = 0
        :loop_num_divs2
            set /a r = %n% %% %p%
            if %r% == 0 (
                set /a e += 1
                set /a n /= %p%
                goto :loop_num_divs2
            )
        
        set /a num *= %e% + 1
        if %n% == 1 exit /b %num%
        set /a p += 1
        set /a p_sq = %p% * %p%
        if %n% GEQ %p_sq% goto :loop_num_divs
        set /a num *= 2
        exit /b %num%

:start_time
    call :get_time
    set /a __t0 = %ERRORLEVEL%
    exit /b 0

:check_time
    setlocal
    call :get_time
    set /a t = %ERRORLEVEL% - %__t0%
    if %t% LSS 10 (
        echo 0.0%t%s
    ) else (
        if %t% LSS 100 (
            echo 0.%t%s
        ) else (
            echo %t:~0,-2%.%t:~-2%s
        )
    )
    exit /b 0

:get_time
    setlocal
    set t=%TIME%
    set /a h = %t:~0,2%
    set /a m = 1%t:~3,2% %% 100
    set /a s = 1%t:~6,2% %% 100
    set /a ss = 1%t:~-2% %% 100
    set /a ret = ((%h% * 60 + %m%) * 60 + %s%) * 100 + %ss%
    exit /b %ret%