Skip to content

Commit 71a1fcb

Browse files
Jeffrey PrattFridaTveit
authored andcommitted
Implement new exercise rational-numbers (exercism#1274)
* Initial * Extract certain logic to methods * Implement and add tests * Use 'One' and 'Zero' in test method names instead of '1' and '0' * Change spacing * Add remaining tests * Fix indentation * Use better method for calculating rth root of x * Move reference implementation to .meta * Include exercise in settings.gradle * Add README * Add reference to config.js * Add UUID and unlocked-by * Make methods package-private; Ignore all but first test * Add 'algorithms' to topics because of gcd * Give @ignores the proper message and put in the proper order * Generate README using configlet * Add version file * Remove 'classes' from topics * Make class package private * Make class package private * Use equals method and assertEquals (and toString) for comparing Rationals * Stub out Rational with equals and toString * Stub out methods * Remove comment
1 parent a2f320a commit 71a1fcb

8 files changed

Lines changed: 514 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,18 @@
11161116
"unlocked_by": "triangle",
11171117
"uuid": "52d11278-0d65-4b5b-b387-1374fced3243"
11181118
},
1119+
{
1120+
"core": false,
1121+
"difficulty": 8,
1122+
"slug": "rational-numbers",
1123+
"topics": [
1124+
"floating_point_numbers",
1125+
"mathematics",
1126+
"algorithms"
1127+
],
1128+
"unlocked_by": "triangle",
1129+
"uuid": "50ed54ce-3047-4590-9fda-0a7e9aeeba30"
1130+
},
11191131
{
11201132
"core": false,
11211133
"difficulty": 8,
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
class Rational {
2+
private int numerator;
3+
private int denominator;
4+
5+
Rational(final int numerator, final int denominator) {
6+
if (denominator == 0) {
7+
throw new IllegalArgumentException("Denominator must not be zero.");
8+
}
9+
10+
this.numerator = numerator;
11+
this.denominator = denominator;
12+
13+
this.reduce();
14+
this.ensureSignInNumerator();
15+
}
16+
17+
private void reduce() {
18+
final int commonDivisor = gcd(this.numerator, this.denominator);
19+
20+
this.numerator /= commonDivisor;
21+
this.denominator /= commonDivisor;
22+
}
23+
24+
private int gcd(int a, int b) {
25+
while (b != 0) {
26+
final int t = b;
27+
b = a % b;
28+
a = t;
29+
}
30+
return a;
31+
}
32+
33+
private void ensureSignInNumerator() {
34+
if (this.denominator < 0) {
35+
this.denominator = -this.denominator;
36+
this.numerator = -this.numerator;
37+
}
38+
}
39+
40+
int getNumerator() {
41+
return this.numerator;
42+
}
43+
44+
int getDenominator() {
45+
return this.denominator;
46+
}
47+
48+
Rational abs() {
49+
return new Rational(abs(this.numerator), abs(this.denominator));
50+
}
51+
52+
private int abs(int x) {
53+
final int mask = x >> 31;
54+
return (mask ^ x) - mask;
55+
}
56+
57+
Rational add(Rational that) {
58+
final int commonDenominator = this.denominator * that.denominator;
59+
60+
return new Rational(
61+
this.numerator * that.denominator + that.numerator * this.denominator,
62+
commonDenominator);
63+
}
64+
65+
Rational subtract(Rational that) {
66+
final int commonDenominator = this.denominator * that.denominator;
67+
68+
return new Rational(
69+
this.numerator * that.denominator - that.numerator * this.denominator,
70+
commonDenominator);
71+
}
72+
73+
Rational multiply(Rational that) {
74+
return new Rational(this.numerator * that.numerator, this.denominator * that.denominator);
75+
}
76+
77+
Rational divide(Rational that) {
78+
return new Rational(this.numerator * that.denominator, this.denominator * that.numerator);
79+
}
80+
81+
Rational pow(int n) {
82+
return new Rational(pow(this.numerator, n), pow(this.denominator, n));
83+
}
84+
85+
double pow(double x) {
86+
return Math.pow(this.numerator, x) / Math.pow(this.denominator, x);
87+
}
88+
89+
private int pow(final int base, final int exponent) {
90+
int product = 1;
91+
92+
for (int i = 0; i < exponent; i++) {
93+
product *= base;
94+
}
95+
96+
return product;
97+
}
98+
99+
double exp(double base) {
100+
return Math.pow(10.0, Math.log10(Math.pow(base, this.numerator)) / this.denominator);
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return String.format("%d/%d", this.getNumerator(), this.getDenominator());
106+
}
107+
108+
@Override
109+
public boolean equals(Object obj) {
110+
if (obj == null || !this.getClass().isAssignableFrom(obj.getClass())) {
111+
return false;
112+
}
113+
114+
Rational other = (Rational) obj;
115+
return this.getNumerator() == other.getNumerator()
116+
&& this.getDenominator() == other.getDenominator();
117+
}
118+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Rational Numbers
2+
3+
A rational number is defined as the quotient of two integers `a` and `b`, called the numerator and denominator, respectively, where `b != 0`.
4+
5+
The absolute value `|r|` of the rational number `r = a/b` is equal to `|a|/|b|`.
6+
7+
The sum of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 + r2 = a1/b1 + a2/b2 = (a1 * b2 + a2 * b1) / (b1 * b2)`.
8+
9+
The difference of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 - r2 = a1/b1 - a2/b2 = (a1 * b2 - a2 * b1) / (b1 * b2)`.
10+
11+
The product (multiplication) of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 * r2 = (a1 * a2) / (b1 * b2)`.
12+
13+
Dividing a rational number `r1 = a1/b1` by another `r2 = a2/b2` is `r1 / r2 = (a1 * b2) / (a2 * b1)` if `a2 * b1` is not zero.
14+
15+
Exponentiation of a rational number `r = a/b` to a non-negative integer power `n` is `r^n = (a^n)/(b^n)`.
16+
17+
Exponentiation of a rational number `r = a/b` to a negative integer power `n` is `r^n = (b^m)/(a^m)`, where `m = |n|`.
18+
19+
Exponentiation of a rational number `r = a/b` to a real (floating-point) number `x` is the quotient `(a^x)/(b^x)`, which is a real number.
20+
21+
Exponentiation of a real number `x` to a rational number `r = a/b` is `x^(a/b) = root(x^a, b)`, where `root(p, q)` is the `q`th root of `p`.
22+
23+
Implement the following operations:
24+
- addition, subtraction, multiplication and division of two rational numbers,
25+
- absolute value, exponentiation of a given rational number to an integer power, exponentiation of a given rational number to a real (floating-point) power, exponentiation of a real number to a rational number.
26+
27+
Your implementation of rational numbers should always be reduced to lowest terms. For example, `4/4` should reduce to `1/1`, `30/60` should reduce to `1/2`, `12/8` should reduce to `3/2`, etc. To reduce a rational number `r = a/b`, divide `a` and `b` by the greatest common divisor (gcd) of `a` and `b`. So, for example, `gcd(12, 8) = 4`, so `r = 12/8` can be reduced to `(12/4)/(8/4) = 3/2`.
28+
29+
Assume that the programming language you are using does not have an implementation of rational numbers.
30+
31+
# Running the tests
32+
33+
You can run all the tests for an exercise by entering
34+
35+
```sh
36+
$ gradle test
37+
```
38+
39+
in your terminal.
40+
41+
## Source
42+
43+
Wikipedia [https://en.wikipedia.org/wiki/Rational_number](https://en.wikipedia.org/wiki/Rational_number)
44+
45+
## Submitting Incomplete Solutions
46+
47+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Rational {
2+
3+
Rational(int numerator, int denominator) {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
int getNumerator() {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
int getDenominator() {
12+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return String.format("%d/%d", this.getNumerator(), this.getDenominator());
18+
}
19+
20+
@Override
21+
public boolean equals(Object obj) {
22+
if (obj == null || !this.getClass().isAssignableFrom(obj.getClass())) {
23+
return false;
24+
}
25+
26+
Rational other = (Rational) obj;
27+
return this.getNumerator() == other.getNumerator()
28+
&& this.getDenominator() == other.getDenominator();
29+
}
30+
}

0 commit comments

Comments
 (0)