-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactors.lua
37 lines (29 loc) · 905 Bytes
/
factors.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function get_all_factors(number)
--[[--
Gets all of the factors of a given number
@Parameter: number
The number to find the factors of
@Returns: A table of factors of the 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 Meaning of the Universe is 42. Let's find all of the factors driving the Universe.
the_universe = 42
factors_of_the_universe = get_all_factors(the_universe)
--Print out each factor
print("Count", "The Factors of Life, the Universe, and Everything")
for i, v in ipairs(factors_of_the_universe) do
print(i, v)
end