Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created April 22, 2012 04:21
Show Gist options
  • Select an option

  • Save sirkitree/2450960 to your computer and use it in GitHub Desktop.

Select an option

Save sirkitree/2450960 to your computer and use it in GitHub Desktop.
Project Euler #3
--[[
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
--]]
function get_all_factors(number)
local factors = {}
for possible_factor=1, math.sqrt(number), 1 do
local remainder = number%possible_factor
if remainder == 0 then
local factor, factor_pair = possible_factor, number/possible_factor
table.insert(factors, factor)
if factor ~= factor_pair then
table.insert(factors, factor_pair)
end
end
end
table.sort(factors)
return factors
end
the_universe = 600851475143
factors_of_the_universe = get_all_factors(the_universe)
print("Count", "The Factors of Life, the Universe, and Everything")
table.foreach(factors_of_the_universe, print)
--[[
Count The Factors of Life, the Universe, and Everything
1 1
2 71
3 839
4 1471
5 6857
6 59569
7 104441
8 486847
9 1234169
10 5753023
11 10086647
12 87625999
13 408464633
14 716151937
15 8462696833
16 600851475143
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment