FANDOM


--
-- Implemented at [[Calculator:Bogrog shards]]
-- sum(i=1 to n) s * 0.7 ^ (i-1)
--
 
local p = {}
local commas = require('Module:Addcommas')
 
function p.summation(frame)
 
	local shards = frame.args[1]
	local trips = frame.args[2]
 
	-- just in case
	if type(shards) == 'string' then
		 shards = commas._strip(shards)
		 shards = tonumber(shards)
	end
 
	if type(trips) == 'string' then
		 trips = commas._strip(trips)
		 trips = tonumber(trips)
	end
 
	-- prevent errors from <1 values
	if (shards <= 0 or trips <= 0) then
		return "Please enter values greater than 0"
	end
 
	-- calling tonumber on a string returns nil
	if not shards or not trips then
		 return 'You must pass numbers as arguments'
	end
 
	-- The user will be notified if:
	-- initial is greater than 2^31-1
	-- trips start producing 0 shards
	-- Default, it assumes they don't, so blank
	local notes = ""
 
	-- You can't have more than this
	if (shards > 2147483647) then
		shards = 2147483647
		notes = "<br>Your initial shard count was truncated to 2,147,483,647"
	end
 
	-- index for summation
	local i = 1
 
	local sum = shards
	local add
 
	-- Largest initial value stops increasing after trip 64
	local max_needed = 70
 
	-- Iterate through; if shards do not increase while still less than
	-- passed number of trips, it will break early and create a note
 
	while (i <= trips and i <= 70) do
	-- flooring should be done immediately, because all values are meant to be integers
		add = math.floor(shards * math.pow (.7, i - 1))
		sum = sum + add
 
		if (add < 1 and max_needed == 70) then
			max_needed = i
		end
 
		i = i + 1
	end
 
	if (max_needed < 70) then
		notes = notes .. "<br>Starting with trip " .. max_needed .. " you will obtain 0 shards" .. "<br>So " .. max_needed - 1 .. " is the most trips you can/should make"
	end
 
	sum = math.floor(sum)
	sum = commas._add(sum)
	return "You effectively have " .. sum .. " shards" .. notes
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.