How to resolve the algorithm Fraction reduction step by step in the C# programming language
How to resolve the algorithm Fraction reduction step by step in the C# 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 C# programming language
This C# program explores fraction reduction by omitting digits from the numerator and denominator. Here's a breakdown of its purpose and functionality:
-
Purpose: To find and count fractions where removing a digit from both the numerator and denominator results in an equivalent fraction.
-
Function IndexOf(int n, int[] s): This function finds the index of an integer
n
in an arrays
. It iterates through the array and returns the index ifn
is found; otherwise, it returns -1. -
Function GetDigits(int n, int le, int[] digits): This function extracts the digits of
n
and stores them in thedigits
array. It iterates through the digits ofn
from right to left, checking if any digit is 0 or already exists in thedigits
array. If so, it returnsfalse
. Otherwise, it stores the digits in the array and returnstrue
. -
Function RemoveDigit(int[] digits, int le, int idx): This function removes a digit at the specified index
idx
from thedigits
array. It calculates the sum of the remaining digits, considering their positions and powers, and returns the resulting integer. -
Main Function:
-
Initialization: The
lims
array holds pairs of lower and upper bounds for the numerator and denominator ranges. Thecount
array keeps track of the count of equivalent fractions for each range, and theomitted
array counts the frequency of omitted digits for each range. -
Looping through Ranges:
-
It iterates through the
lims
array, exploring different ranges of numerator and denominator values. -
For each range, it initializes
nDigits
anddDigits
arrays to store the digits of the numerator and denominator, respectively.
-
-
Finding Reducible Fractions:
-
For each numerator value
n
within the range, it extracts its digits usingGetDigits
. IfGetDigits
returnsfalse
, it means the numerator contains invalid digits, son
is skipped. -
For each denominator value
d
within the range, it extracts its digits usingGetDigits
. IfGetDigits
returnsfalse
, it means the denominator contains invalid digits, sod
is skipped. -
For each digit in the numerator, it checks if it exists in the denominator. If it does, it removes that digit from both the numerator and denominator using
RemoveDigit
. -
It then checks if the reduced fraction is equivalent to the original fraction by comparing their values. If equivalent, it increments the
count
and records the omitted digit in theomitted
array.
-
-
Printing Results:
-
The program prints the total count of reducible fractions for each range.
-
For each range, it prints the count of omitted digits for each digit (1-9).
-
-
-
Example Output: For example, with
lims
set to{12, 97}, {123, 986}, {1234, 9875}, {12345, 98764}
, the program might produce output like this:
12/21 = 6/10 by omitting 2's
12/91 = 6/45 by omitting 2's
123/456 = 12/45 by omitting 3's
1234/5678 = 123/567 by omitting 4's
1234/5678 = 234/567 by omitting 1's
There are 3 3-digit fractions of which:
5 have 2's omitted
1 have 3's omitted
There are 1 4-digit fractions of which:
1 have 1's omitted
1 have 4's omitted
There are 2 5-digit fractions of which:
2 have 1's omitted
1 have 4's omitted
1 have 5's omitted
- Note: The program prints up to 12 examples for each range. If there are more than 12 equivalent fractions, the remaining examples are not printed.
Source code in the csharp programming language
using System;
namespace FractionReduction {
class Program {
static int IndexOf(int n, int[] s) {
for (int i = 0; i < s.Length; i++) {
if (s[i] == n) {
return i;
}
}
return -1;
}
static bool GetDigits(int n, int le, int[] digits) {
while (n > 0) {
var r = n % 10;
if (r == 0 || IndexOf(r, digits) >= 0) {
return false;
}
le--;
digits[le] = r;
n /= 10;
}
return true;
}
static int RemoveDigit(int[] digits, int le, int idx) {
int[] pows = { 1, 10, 100, 1000, 10000 };
var sum = 0;
var pow = pows[le - 2];
for (int i = 0; i < le; i++) {
if (i == idx) continue;
sum += digits[i] * pow;
pow /= 10;
}
return sum;
}
static void Main() {
var lims = new int[,] { { 12, 97 }, { 123, 986 }, { 1234, 9875 }, { 12345, 98764 } };
var count = new int[5];
var omitted = new int[5, 10];
var upperBound = lims.GetLength(0);
for (int i = 0; i < upperBound; i++) {
var nDigits = new int[i + 2];
var dDigits = new int[i + 2];
var blank = new int[i + 2];
for (int n = lims[i, 0]; n <= lims[i, 1]; n++) {
blank.CopyTo(nDigits, 0);
var nOk = GetDigits(n, i + 2, nDigits);
if (!nOk) {
continue;
}
for (int d = n + 1; d <= lims[i, 1] + 1; d++) {
blank.CopyTo(dDigits, 0);
var dOk = GetDigits(d, i + 2, dDigits);
if (!dOk) {
continue;
}
for (int nix = 0; nix < nDigits.Length; nix++) {
var digit = nDigits[nix];
var dix = IndexOf(digit, dDigits);
if (dix >= 0) {
var rn = RemoveDigit(nDigits, i + 2, nix);
var rd = RemoveDigit(dDigits, i + 2, dix);
if ((double)n / d == (double)rn / rd) {
count[i]++;
omitted[i, digit]++;
if (count[i] <= 12) {
Console.WriteLine("{0}/{1} = {2}/{3} by omitting {4}'s", n, d, rn, rd, digit);
}
}
}
}
}
}
Console.WriteLine();
}
for (int i = 2; i <= 5; i++) {
Console.WriteLine("There are {0} {1}-digit fractions of which:", count[i - 2], i);
for (int j = 1; j <= 9; j++) {
if (omitted[i - 2, j] == 0) {
continue;
}
Console.WriteLine("{0,6} have {1}'s omitted", omitted[i - 2, j], j);
}
Console.WriteLine();
}
}
}
}
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the REXX programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the jq programming language
You may also check:How to resolve the algorithm Tropical algebra overloading step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Pythagoras tree step by step in the Ol programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Transd programming language