結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー 👑 obakyan
提出日時 2021-08-27 21:27:37
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 753 bytes
コンパイル時間 39 ms
コンパイル使用メモリ 6,824 KB
実行使用メモリ 37,632 KB
最終ジャッジ日時 2024-11-21 00:51:11
合計ジャッジ時間 4,343 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end
local primes = getprimes(3000000)
local l, r = io.read("*n", "*n")
--[[
(a + b) * (b - a + 1) / 2
b - a + 1 = 2
a + 1 = b
]]
local c = 0
for i = 1, #primes do
  local p = primes[i]
  if l <= p and p <= r then c = c + 1 end
  if 1 < i then
    local hp = mfl(p / 2)
    if l <= hp and hp + 1 <= r then
      c = c + 1
    end
  end
end
print(c)
0