samy22
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2181 |
|
|
| 15 Jul 2017 10:28 PM |
Sort of math-y too.
Produce a function which converts a recurring decimal to a fraction in it's simplest form and outputs that fraction.
To clarify, let the input only be an integer value like 123 which produces 0.123123123123... in it's simplest fractional form.
(No need to specify a second non recurring part like 978.123123123123.. or 0.57123123123..) |
|
|
| Report Abuse |
|
|
|
| 15 Jul 2017 10:29 PM |
Yes, but, what exactly would this be needed for?
NULL > NIL |
|
|
| Report Abuse |
|
|
samy22
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2181 |
|
|
| 15 Jul 2017 10:29 PM |
| Nothing in particular, it's just for the fun/challenge of it. |
|
|
| Report Abuse |
|
|
kii55_ALT
|
  |
| Joined: 13 Nov 2016 |
| Total Posts: 67 |
|
|
| 15 Jul 2017 10:29 PM |
Ooooh i know how to awnser this!
Dont. |
|
|
| Report Abuse |
|
|
BaiYuni
|
  |
| Joined: 09 Oct 2009 |
| Total Posts: 2861 |
|
|
| 15 Jul 2017 10:38 PM |
| Is this like project Euler? |
|
|
| Report Abuse |
|
|
|
| 15 Jul 2017 10:40 PM |
I was trying, then I realized. I suck at math. Like Einstein failed math. Wait that's a myth. He actually didn't fail at math.
pushstring(LuaState, "Get out"); |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 15 Jul 2017 10:44 PM |
function a(int) length = math.floor(math.log10(int)) denominator = 0 for i = 0, length do denominator = denominator+ 9*10^i end for i = 3, denominator, 2 do while ({math.modf(denominator/i)})[2] == 0 and ({math.modf(int/i)})[2] == 0 do denominator = denominator/i int = int/i end end print(("%d/%d"):format(int, denominator)) end
a(123) a(999) |
|
|
| Report Abuse |
|
|
samy22
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2181 |
|
|
| 15 Jul 2017 11:00 PM |
Nicely done Casualist!
I'm sure you already know this but you can more easily test for divisibility using %.
denominator%i == 0 instead of ({math.modf(denominator/i)})[2] == 0
&
denominator = 0 for i = 0, length do denominator = denominator+ 9*10^i end
can be replaced with
denominator = 10^(length + 1) - 1 (by summation of power series, or just by observation.) |
|
|
| Report Abuse |
|
|
samy22
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2181 |
|
| |
|
samy22
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2181 |
|
|
| 15 Jul 2017 11:03 PM |
My soln:
function recurdec_tofraction(digitsRecurring) local d = -1 + 10^math.floor(math.log10(digitsRecurring)+1) local a, b = d, digitsRecurring repeat b, a = a%b, b until b == 0 return math.floor(digitsRecurring/a) .."/" ..math.floor(d/a) end |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 15 Jul 2017 11:06 PM |
That, and I could reduce my search space by a few orders of magnitude:
for i = 3, denominator/9, 2 do --// factorization check if int < i or denominator < i then break end end |
|
|
| Report Abuse |
|
|
|
| 15 Jul 2017 11:45 PM |
You could account for n <= 0 with:
function recurdec_tofraction(digitsRecurring) local d = digitsRecurring <= 0 and 0 or -1 + 10^math.floor(math.log10(digitsRecurring)+1) local a, b = d, digitsRecurring <= 0 and 1 or digitsRecurring repeat b, a = a%b, b until b == 0 return math.floor(digitsRecurring/a) .."/" ..math.floor(d/a) end
|
|
|
| Report Abuse |
|
|
Sulfone
|
  |
| Joined: 19 Aug 2010 |
| Total Posts: 8046 |
|
|
| 15 Jul 2017 11:56 PM |
I remember being shown a simple way of doing this. Consider the recurring decimal 0.424242... (42 here): Let 100x = 42.4242... Then x = 0.424242.... These two have the same repeated part, therefore they can be subtracted from each other. ∴ 99x=42 ∴ x = 44/99 Now there's the fraction for this recurring decimal.
From this I can say the fraction is the (x / next number after x made up of only 9s) or (x / (9 repeated string length of x)) and make:
function recurring_to_fraction(numerator) local length = string.len(tostring(numerator)) local denominator = string.rep('9', length) if denominator == tostring(numerator) then return 1 -- numerator / denominator = 1 else return numerator .. ' / ' .. denominator end end
(Also, what's the trick to making working tabs on the forums?) |
|
|
| Report Abuse |
|
|
|
| 16 Jul 2017 12:05 AM |
| Good trick, though it doesn't reduce to simplest terms. |
|
|
| Report Abuse |
|
|
Sulfone
|
  |
| Joined: 19 Aug 2010 |
| Total Posts: 8046 |
|
|
| 16 Jul 2017 12:15 AM |
for i=1, 1000 do print('KnightmareXD ' .. recurdec_tofraction(i)) print('Sulfone ' .. recurring_to_fraction(i)) end
Looks the same except for 1s. Replace my "return 1" with "return 1/1" to make them identical (but is 1/1 really simpler than 1?). |
|
|
| Report Abuse |
|
|
|
| 16 Jul 2017 12:20 AM |
| I'm referring to the cases where it doesn't reduce such as when n is 3, 6, 11, etc. |
|
|
| Report Abuse |
|
|
Sulfone
|
  |
| Joined: 19 Aug 2010 |
| Total Posts: 8046 |
|
|
| 16 Jul 2017 01:19 AM |
I completely missed that, thanks. "Simplest term" version of my previous solution (using Euclid's algorithm):
function recurring_to_fraction(numerator) local length = string.len(tostring(numerator)) local denominator = string.rep('9', length) denominator = tonumber(denominator) if denominator == numerator then return 1 -- numerator / denominator = 1 else local factor = numerator local larger = denominator while larger ~= 0 do local temp = larger larger = factor % larger factor = temp end return numerator/factor .. ' / ' .. denominator/factor end end |
|
|
| Report Abuse |
|
|