Module:JI ratio finder: Difference between revisions
Created lua translation of JIRAF; work-in-progress, as some limitations need tweaking |
m Comments; logic adjusted for finding the unison |
||
| Line 21: | Line 21: | ||
-- Main function | -- Main function | ||
-- Finds approximated JI ratios for a cent value for a prime and odd limit | -- Finds approximated JI ratios for a cent value for a prime and odd limit | ||
function p. | function p.find_ratios_for_cents(cents, tolerance, prime_limit, odd_limit) | ||
local cents = cents or | local cents = cents or 700 | ||
local tolerance = tolerance or | local tolerance = tolerance or 20 | ||
local prime_limit = prime_limit or | local prime_limit = prime_limit or 5 | ||
local odd_limit = odd_limit or | local odd_limit = odd_limit or 49 | ||
local num = 1 | local num = 1 | ||
| Line 38: | Line 38: | ||
local found_ratios = {} | local found_ratios = {} | ||
-- Algorithm is as follows: | |||
-- Start with a unison p/q, where p=q and check whether it's within the | |||
-- range of the target ratio. If so, record it. Increment p by 1 and repeat | |||
-- until p/q exceeds the target range. | |||
-- Once p/q exceeds the range of the target ratio, increment q and repeat | |||
-- the process described above. End this process once p or q exceed a | |||
-- specified limit (the odd limit, in this case). | |||
repeat | repeat | ||
repeat | repeat | ||
local current_ratio = rat.new(num, den) | local current_ratio = rat.new(num, den) | ||
local ratio_in_cents = rat.cents(current_ratio) | local ratio_in_cents = rat.cents(current_ratio) | ||
| Line 55: | Line 60: | ||
table.insert(found_ratios, rat.as_ratio(current_ratio)) | table.insert(found_ratios, rat.as_ratio(current_ratio)) | ||
end | end | ||
-- Increment numerator | |||
num = num + 1 | |||
until not within_max_tolerance | until not within_max_tolerance | ||