How to resolve the algorithm Fraction reduction step by step in the Ruby programming language
How to resolve the algorithm Fraction reduction step by step in the Ruby programming language
Table of Contents
Problem Statement
A method to "reduce" some reducible fractions is to cross out a digit from the numerator and the denominator. An example is: resulting in:
Naturally, this "method" of reduction must reduce to the proper value (shown as a fraction). This "method" is also known as anomalous cancellation and also accidental cancellation.
(Of course, this "method" shouldn't be taught to impressionable or gullible minds.) 😇
Find and show some fractions that can be reduced by the above "method".
Show all output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fraction reduction step by step in the Ruby programming language
This Ruby code solves a mathematical problem that involves finding the number of fractions within a range where one digit can be omitted to make them equal. The code operates as follows:
-
indexOf
: This is a helper function that returns the index of an element within an array. It iterates through the array, comparing each element to the inputneedle
object. If a match is found, the corresponding index is returned. -
getDigits
: This function takes a numbern
, the expected length of the digit arrayle
, and an array of digitsdigits
. It extracts the individual digits fromn
and stores them in thedigits
array from right to left. It also checks for leading zeros and digits already present in thedigits
array, returningfalse
if either condition is met. -
POWS
: This is an array of powers of 10 used for calculating the numeric value of the digits. -
removeDigit
: This function removes a digit at a specified index from thedigits
array and calculates the new numeric value of the resulting number. It iterates through thedigits
array, skipping the digit at the given index and multiplying the remaining digits by the appropriate power of 10. -
main
: This function serves as the entry point of the program. It iterates through four different ranges of numbers, defined by thelims
array. -
For each range, it initializes counters (
count
) and arrays (omitted
) to keep track of the number of fractions and the number of times each digit is omitted. It then enters a loop to generate fractions within the specified range. -
It uses
getDigits
to create an array of digits for each fraction. IfgetDigits
returnsfalse
, the loop continues to the next fraction. -
For each fraction, it generates a second fraction with a slightly higher value (
d
) and again usesgetDigits
to obtain its array of digits. -
It then compares the digits of the two fractions to identify any digits that can be omitted to make them equal. If such a digit is found, it removes the digit from both fractions and recalculates their numeric values.
-
If the reduced fractions are indeed equal, it increments the corresponding counter (
count
) and records the omitted digit in theomitted
array. -
After processing all fractions within a range, it provides a summary of the results, including the number of equal fractions found and the frequency of each omitted digit.
-
Finally, it prints a comprehensive summary across all ranges, showing the total number of equal fractions and the specific digits that were omitted.
In summary, this code demonstrates a creative and efficient approach to solving a mathematical problem involving fractions and digit manipulation. It combines loop-based iteration, array manipulation, and conditional checks to find and count equal fractions within specified ranges, providing detailed insights into the patterns and relationships involved.
Source code in the ruby programming language
def indexOf(haystack, needle)
idx = 0
for straw in haystack
if straw == needle then
return idx
else
idx = idx + 1
end
end
return -1
end
def getDigits(n, le, digits)
while n > 0
r = n % 10
if r == 0 or indexOf(digits, r) >= 0 then
return false
end
le = le - 1
digits[le] = r
n = (n / 10).floor
end
return true
end
POWS = [1, 10, 100, 1000, 10000]
def removeDigit(digits, le, idx)
sum = 0
pow = POWS[le - 2]
i = 0
while i < le
if i == idx then
i = i + 1
next
end
sum = sum + digits[i] * pow
pow = (pow / 10).floor
i = i + 1
end
return sum
end
def main
lims = [ [ 12, 97 ], [ 123, 986 ], [ 1234, 9875 ], [ 12345, 98764 ] ]
count = Array.new(5, 0)
omitted = Array.new(5) { Array.new(10, 0) }
i = 0
for lim in lims
n = lim[0]
while n < lim[1]
nDigits = [0] * (i + 2)
nOk = getDigits(n, i + 2, nDigits)
if not nOk then
n = n + 1
next
end
d = n + 1
while d <= lim[1] + 1
dDigits = [0] * (i + 2)
dOk = getDigits(d, i + 2, dDigits)
if not dOk then
d = d + 1
next
end
nix = 0
while nix < nDigits.length
digit = nDigits[nix]
dix = indexOf(dDigits, digit)
if dix >= 0 then
rn = removeDigit(nDigits, i + 2, nix)
rd = removeDigit(dDigits, i + 2, dix)
if (1.0 * n / d) == (1.0 * rn / rd) then
count[i] = count[i] + 1
omitted[i][digit] = omitted[i][digit] + 1
if count[i] <= 12 then
print "%d/%d = %d/%d by omitting %d's\n" % [n, d, rn, rd, digit]
end
end
end
nix = nix + 1
end
d = d + 1
end
n = n + 1
end
print "\n"
i = i + 1
end
i = 2
while i <= 5
print "There are %d %d-digit fractions of which:\n" % [count[i - 2], i]
j = 1
while j <= 9
if omitted[i - 2][j] == 0 then
j = j + 1
next
end
print "%6s have %d's omitted\n" % [omitted[i - 2][j], j]
j = j + 1
end
print "\n"
i = i + 1
end
end
main()
You may also check:How to resolve the algorithm Nim game step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Mercury programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Kotlin programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the MEL programming language