Problem 7と同じように部分的にエラトステネスのふるいを行います。
しかし、2時間もかかってしまいました。この連載ではなるべくなら1時間以内で答えを出したいところなのです。
@echo off setlocal call :get_time set /a t0 = %ERRORLEVEL% set /a N = 2000000 call :sum_primes %N% call :get_time set /a t = %ERRORLEVEL% - %t0% echo %t:~0,-2%.%t:~-2%s exit /b 0 :sum_primes set /a N = %1 call :sqrt %1 set /a M = %ERRORLEVEL% set /a L = 1000 call :make_primes ps %M% set /a s1 = 0 set /a s2 = 0 set /a k = -1 set /a p = 2 :loop_sum_primes set /a s1 += %p% set /a k += 1 set /a p = "ps_%k%" if %p% LSS %L% goto :loop_sum_primes set /a N_1 = %N% - 1 for /L %%i in (%L%, %L%, %N_1%) do call :sum_primes_core %%i %L% set /a s1 += 100000000 echo %s2%%s1:~1% exit /b 0 :sum_primes_core set /a begin = %1 set /a end = %1 + %2 call :sieve a %begin% %end% set /a s = 0 set /a k = 0 set /a L_2 = %L% / 2 :loop_sum_primes_core set /a ak = "a_%k%" set /a p = %begin% + %k% * 2 + 1 if %ak% == 1 ( set /a s += %begin% + %k% * 2 + 1 ) set /a k += 1 if %k% LSS %L_2% goto :loop_sum_primes_core set /a s1 += %s% set /a carry = %s1% / 100000000 set /a s1 %%= 100000000 set /a s2 += %carry% exit /b 0 :make_primes set /a a.size = %2 + 1 call :fill_vector a 1 set /a k = 2 :loop_make_primes set /a b = "a_%k%" if %b% == 0 set /a k += 1 & goto :loop_make_primes set /a k2 = %k% * 2 for /L %%i in (%k2%, %k%, %2) do set /a a_%%i = 0 set /a k += 1 set /a k_sq = %k% * %k% if %k_sq% LEQ %2 goto :loop_make_primes set /a k = 3 :loop_make_primes2 set /a b = "a_%k%" if %b% == 1 call :push_vector %1 %k% set /a k += 1 if %k% LSS %2 goto :loop_make_primes2 exit /b 0 :sieve set /a %1.size = L / 2 call :fill_vector %1 1 call :sqrt %3 set /a p_limit = %ERRORLEVEL% set /a k = 0 :loop_sieve set /a p = "ps_%k%" set /a m0 = ((%2 + %p% - 1) / (%p% * 2) * (%p% * 2) + %p% - %2) / 2 set /a L_1 = %L% - 1 for /L %%m in (%m0%, %p%, %L_1%) do set /a %1_%%m = 0 set /a k += 1 if %k% == %ps.size% exit /b 0 if %p% LSS %p_limit% goto :loop_sieve exit /b 0 :fill_vector set /a max_index = "%1.size" - 1 for /L %%i in (0, 1, %max_index%) do set /a %1_%%i = %2 exit /b 0 :print_vector setlocal set vector=%1 set /a size = "%vector%.size" if %size% == 0 echo %2 & exit /b 0 set /a e = "%vector%_0" set s=%e% set /a k = 1 :loop_print_vector if %k% == %size% echo %s% & exit /b 0 set /a e = "%vector%_%k%" set s=%s% %e% set /a k += 1 goto :loop_print_vector :push_vector set /a size = "%1.size" set /a %1_%size% = %2 set /a %1.size = %size% + 1 exit /b 0 :sqrt setlocal set /a n = %1 set /a m = %1 :loop_sqrt set /a m1 = (%m% + %n% / %m%) / 2 if %m1% GEQ %m% exit /b %m% set /a m = %m1% goto :loop_sqrt :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%