Jason Follas.com

Page History: Project Euler Comes to Azeroth


Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: 2008/04/25 00:13


Intro

I joked with my friend Dustin about creating solutions to the Project Euler problems using LUA (and running them in WoW). Here's where I'll post those solutions as I finish them.

I'll try to generalize my solutions where I see fit. For example, I added a parameter to Problem 1 to set the upper limit of 1000, but I did not parameterize the 3 and 5, etc.

Problem 1



function ProjectEuler:OnEnable()
    ProjectEuler:Problem001(1000)
end
function ProjectEuler:Problem001(upperLimit)
        self:Print("Sum of all numbers less than " 
            .. upperLimit .. " that are divisible by 3 or 5")
        
        local f = function(factor)
            local n = math.ceil(upperLimit / factor)
            return n * (n-1) * factor / 2
        end
    
        local result = f(3) + f(5) - f(15)

        self:Print(result)
end

Problem 2



function ProjectEuler:Problem002(upperLimit)
	self:Print("Sum of all even numbers less than " .. upperLimit .. " in Fibonacci Sequence")
	
	local result, x1, x2 = 0, 1, 2
	
	local f = function(t1, t2)
		return t2, t1+t2
	end
	

	while x2 < upperLimit
	do
		self:Print("adding "..x2)
		result = result + x2
		x1,x2 = f(f(f(x1,x2)))
	end
	
	self:Print(result)
end

Problem 3

function ProjectEuler:Problem003(composite)
	self:Print("Largest prime factor of "..composite)

	local n = 2
	
	while n < (composite / n)
	do
		if composite % n == 0 then
			composite = composite / n
			self:Print("prime factor="..n); 
			n = 1 -- reset N to ensure primes (i.e., 2x2x3x5x5 = 300)
		end
		n=n+1
	end
	
	self:Print("The largest is: "..composite)
end

ScrewTurn Wiki version 2.0.13. Some of the icons created by FamFamFam.