# Thread: help to write program

1. ## help to write program

I'm new to c programing and I want to write following program
Teacher ask student to solve 26/65 he simply cancelled 6 from numerator and denominator and answer found correct.
Write program to determine all fractions with 2 digit numerators and denominators for which this cancellation technique works correctly
Thank you

2. Teacher wants students to do the work on their own without asking someone on the Net to do this work for them.

What do you mean by "cancelling" 6 from the numerator and denominator in the example given?

3. Cancelling 6 means canceling same digit from numerator n denominator
I'm new I don't know to write code for this program

4. Do you mean subtracting 6 from each, "20/59" or removing "6" from each, "2/5"? If the latter, it does not make sense to me mathematically!

5. Removing 6 from right digit of numerator n left digit of denominator

6. Originally Posted by san12345
I'm new I don't know to write code for this program
Writing code is only one part of programming. Another is figuring out how to break down a problem and solve it. This is a skill you must develop through practice.

Also, as rstanley mentions, make sure you completely understand the problem before trying to devise a solution.

7. Please explain the logic of this math!

8. It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic:

26/65 = 0.4
2/5 = 0.4

@ OP: You need to sit down with a pencil and paper, and start trying to figure out this algorithm by hand. If you cannot solve a problem yourself step by step, you will not be able to program a computer to solve it for you.

9. @matticus, I agree with you. IMHO, yet another example of a bad instructor! ;^)

10. Originally Posted by Matticus
It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic:
Originally Posted by rstanley
@matticus, I agree with you. IMHO, yet another example of a bad instructor! ;^)
Err... not necessarily. The scenario is that a hypothetical teacher gives a student a maths problem, but the student incorrectly solved it by applying a nonsensical rule ("cancelling" digits that happen to be the same in the numerator and denominator), yet the answer turned out to be correct by coincidence. The required program is supposed to find all such coincidences for 2 digit numerators and denominators. The instructor himself/herself is not saying that these coincidences form an "obscure mathematical rule", but rather the scenario is merely how the programming problem is presented for context.

11. Originally Posted by laserlight
The required program is supposed to find all such coincidences for 2 digit numerators and denominators.
Yes, clearly. That is why I suggested they start developing an algorithm to write this code, rather than try to "correct" their requirements.

I said the problem "seems to rely more on coincidence", which is also what you are saying.

12. Originally Posted by san12345
Teacher ask student to solve 26/65 he simply cancelled 6 from numerator and denominator and answer found correct.
That's just because of a chance occurrence. It was not a 6 that was cancelled; a 13 was cancelled from both numerator and denominator:
Code:
```26   2⋅13   2
── = ──── = ─
65   5⋅13   5```
A computer program typically finds the greatest common divisor of both numerator and denominator -- here, gcd(26,65)=13 -- and divides both by it, to find the reduced form.

The computer program should obviously look at all pairs of two-digit numerators and denominators that have at least one common digit, and check if removing one of those common digits from both numerator and denominator actually yields the same fraction as the original number.

So, no need to implement gcd() at all. Just use a single loop for each digit (so four nested loops in total), and construct the numbers using A = N1 + 10⋅N2 and B = N3 + 10⋅N4. It is then very easy to check if the two have digits in common.

I'm pretty sure the teacher thought that floating-point results suffice for the comparison, but I'm not convinced. Instead, you should check the two ratios using integer variables and cross multiplication,
Code:
```A   C
─ = ─  if and only if A⋅D = B⋅C and B≠0, D≠0.
B   D```
where C/D is the original ratio, and A/B is the ratio with one common digit removed from both A and B.

As long as your (unsigned) integer type is large enough to hold the products A⋅D and B⋅C, and int and unsigned int certainly are for two-digit numbers, the above check is exact and robust.

(Lots of edits during first 20 minutes this post was up.)

13. Originally Posted by Matticus
It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic
Well, the rule is pretty obscure for sure. If we define

Let
N = sum(Ni ⋅ 10i, i = 0..LN-1)
where
1 ≤ Ni ≤ 9, i = LN-1
0 ≤ Ni ≤ 9, 0 ≤ i < LN-1
and
D = sum(Di ⋅ 10i, i = 0..LD-1)
where
1 ≤ Di ≤ 9, i = LD-1
0 ≤ Di ≤ 9, 0 ≤ i < LD-1
so that N is an LN-digit positive decimal number,
10LN-1N < 10LN
and D is an LD-digit positive decimal number,
10LD-1D < 10LD

the rule is

There is a pair (n, d),
0 ≤ n < LN
0 ≤ d < LD
Nn = Dd
that fulfills the following rule:
U = sum(Ni ⋅ 10i, i = 0..n-1) + sum(Ni ⋅ 10i-1, i = n+1..LN-1)
V = sum(Di ⋅ 10i, i = 0..d-1) + sum(Di ⋅ 10i-1, i = d+1..LD-1)
N V = D U ≠ 0

I don't think there is a simpler way to state the rule (anything like summing the digits, or such tricks); I think this was what Matticus was saying, too.

The OP's problem to be solved is to find all that fulfill the above, when LN = LD = 2. My awk scriptlet says there are 242 of those if you count 89/89 = 8/8 and 89/89 = 9/9 as two separate cases, or 170 if you count 89/89 = 8/8 = 9/9 only once.

Popular pages Recent additions