Path: blob/master/bench/tests/sunspider/math-partial-sums.lua
2727 views
--[[1The Great Computer Language Shootout2http://shootout.alioth.debian.org/3contributed by Isaac Gouy4]]5local function prequire(name) local success, result = pcall(require, name); return success and result end6local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")78function test()910local function partial(n)11local a1, a2, a3, a4, a5, a6, a7, a8, a9 = 0, 0, 0, 0, 0, 0, 0, 0, 0;12local twothirds = 2.0/3.0;13local alt = -1.0;14local k2, k3, sk, ck = 0, 0, 0, 0;1516for k = 1,n do17k2 = k*k;18k3 = k2*k;19sk = math.sin(k);20ck = math.cos(k);21alt = -alt;2223a1 = a1 + math.pow(twothirds,k-1);24a2 = a2 + math.pow(k,-0.5);25a3 = a3 + 1.0/(k*(k+1.0));26a4 = a4 + 1.0/(k3 * sk*sk);27a5 = a5 + 1.0/(k3 * ck*ck);28a6 = a6 + 1.0/k;29a7 = a7 + 1.0/k2;30a8 = a8 + alt/k;31a9 = a9 + alt/(2*k -1);32end3334return a6 + a7 + a8 + a9;35end3637local total = 0;38local i = 10243940while i <= 16384 do41total = total + partial(i);42i = i * 243end4445local expected = 60.08994194659945;4647if (total ~= expected) then48assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. total);49end5051end5253bench.runCode(test, "math-partial-sums")545556