Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rationals
- import java.math.BigInteger
- import java.math.BigInteger.ZERO
- import kotlin.math.sign
- class Rational(num: BigInteger, den: BigInteger) {
- val n: BigInteger
- val d: BigInteger
- init {
- require(den != ZERO) { "denominator cannot be zero" }
- val s = (num * den).signum().toBigInteger()
- val g = num.gcd(den)
- this.n = num.abs() / g * s
- this.d = den.abs() / g
- }
- constructor(nd: String) : this(
- nd.trim('[').trim(']').toBigInteger(), 1.toBigInteger()
- )
- override fun toString(): String {
- return if (d.toInt() != 1) {
- "$n/$d"
- } else this.n.toString()
- }
- override fun equals(other: Any?): Boolean {
- if (this === other) return true
- if (javaClass != other?.javaClass) return false
- other as Rational
- if (n != other.n) return false
- if (d != other.d) return false
- return true
- }
- override fun hashCode(): Int {
- var result = n.hashCode()
- result = 31 * result + d.hashCode()
- return result
- }
- }
- infix fun Number.divBy(that: Number): Rational {
- val n = this.toString().toBigInteger()
- val d = that.toString().toBigInteger()
- return Rational(n , d )
- }
- class RationalRange(val start:Rational, val end:Rational) {
- }
- operator fun Rational.rangeTo(that: Rational): RationalRange {
- return RationalRange(this.n.divBy(this.d), that.n.divBy(that.d))
- }
- infix operator fun RationalRange.contains(that: Rational): Boolean {
- val a = this.start.n
- val b = this.start.d
- val c = this.end.n
- val d = this.end.d
- val e = that.n
- val f = that.d
- val sn = a * d * f
- val en = c * b * f
- return (e * b * d) in sn..en
- }
- fun String.toRational(): Rational {
- val nd = this.split("/", ignoreCase = true, limit = 0)
- return if (nd.size > 1) {
- val n = nd.first().toBigInteger()
- val d = nd.last().toBigInteger()
- val g = n.gcd(d)
- val s = (n * d).signum()
- val a = s.toBigInteger() * n.abs()
- val b = d.abs()
- Rational(a / g, b / g)
- } else Rational(nd.toString())
- }
- operator fun Rational.plus(that: Rational): Rational {
- val a = this.d
- val b = that.d
- val g = a.gcd(b)
- val l = (a * b) / g
- return Rational(((this.n * that.d) + (that.n * this.d)) / g, l)
- }
- operator fun Rational.minus(that: Rational): Rational {
- val a = this.d
- val b = that.d
- val g = a.gcd(b)
- val l = a * b / g
- return Rational(((this.n * that.d) - (that.n * this.d)) / g, l)
- }
- operator fun Rational.times(that: Rational): Rational {
- val n = this.n.times(that.n)
- val d = this.d.times(that.d)
- val g = n.gcd(d)
- return Rational(n / g, d / g)
- }
- operator fun Rational.div(that: Rational): Rational {
- return Rational(this.n.times(that.d), this.d.times(that.n))
- }
- operator fun Rational.unaryMinus(): Rational {
- return Rational(this.n.times((-1).toBigInteger()), this.d)
- }
- infix operator fun Rational.compareTo(that: Rational): Int {
- val a = this.n
- val b = this.d
- val s1 = (a * b).signum()
- val c = that.n
- val d = that.d
- val s2 = (c * d).signum()
- val s = (s1 * s2).sign
- return if (s > 0) {
- (a * d).compareTo(c * b)
- } else s1.compareTo(s2)
- }
- fun main() {
- val half = 1 divBy 2
- val third = 1 divBy 3
- val sum: Rational = half + third
- println(5 divBy 6 == sum)
- val difference: Rational = half - third
- println(1 divBy 6 == difference)
- val product: Rational = half * third
- println(1 divBy 6 == product)
- val quotient: Rational = half / third
- println(3 divBy 2 == quotient)
- val negation: Rational = -half
- println(-1 divBy 2 == negation)
- println((2 divBy 1).toString() == "2")
- println((-2 divBy -4).toString() == "1/2")
- println("117/1098".toRational().toString() == "13/122")
- val twoThirds = 2 divBy 3
- println(half > -twoThirds)
- val gdnb1 = Rational("20325830850349869048604856908")
- val gdnb2 = Rational("-9192901948302584358938698")
- println(gdnb1 > gdnb2)
- println(half in third..twoThirds)
- println(2000000000L divBy 4000000000L == 1 divBy 2)
- println(half in third..twoThirds)
- println(2000000000L divBy 4000000000L == 1 divBy 2)
- println("912016490186296920119201192141970416029".toBigInteger() divBy
- "1824032980372593840238402384283940832058".toBigInteger() == 1 divBy 2)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement