Radix-point decomposition of a rational number This assignment asks you to study the…
- User
Radix-point decomposition of a rational number This assignment asks you to study the properties of the radix point representation of rational numbers. In particular, the radix point representation of every rational number decomposes into three parts, each of which is a sequence of digits in the chosen base B:
the integer part the fractional part, which further decomposes into the finite transient the finite period (that repeats infinitely many times) This decomposition always exists and is unique if we choose the shortest possible finite transient, the shortest possible finite period, and require the period to be 0 whenever multiple choices exist for the period. In what follows we assume that the decomposition is always chosen in this way.
Example 1. Consider the rational number 45679/3700, which expands in base B = 10 radix point as 12.34567567567567…. Here the integer part is 12, the finite transient is 34, and the finite period is 567 (this repeats infinitely many times).
Example 2. Consider the rational number 23/1, which expands in base B = 10 radix point as 23.00000000000000…. Here the integer part is 23, the finite transient is empty, and the finite period is 0 (this repeats infinitely many times).
Example 3. Consider the rational number 3/2, which expands in base B = 10 radix point as 1.500000000000000…. Here the integer part is 1, the finite transient is 5, and the finite period is 0 (this repeats infinitely many times).
/*
- Description:
- This assignment asks you to study the properties of the radix point
- representation of rational numbers. In particular, the radix point
- representation of every rational number decomposes into three
- parts, each of which is a sequence of digits in the chosen base B:
- (a) the integer part
- (b) the fractional part, which further decomposes into
-
(b1) the finite transient -
(b2) the finite period (that repeats infinitely many times) - This decomposition always exists and is unique if we choose the
- shortest possible finite transient, the shortest possible finite period,
- and require the period to be 0 whenever multiple choices exist for
- the period. In what follows we assume that the decomposition is always
- chosen in this way.
- Example 1:
- Consider the rational number 45679/3700, which expands in base B = 10
- radix point as 12.34567567567567...
- The integer part is 12
- The finite transient is 34
- The finite period is 567 (this repeats infinitely many times)
- Assistant
package rationalDecompose import scala.collection.mutable.ListBuffer def decompose(p: Int, q: Int, b: Int): (Seq[Int],Seq[Int],Seq[Int]) = { def intPart(p: Int, q: Int, b: Int): Seq[Int] = { val intDiv = p / q intDiv.toString.map(_.asDigit) } def fracPart(p: Int, q: Int, b: Int): (Seq[Int], Seq[Int]) = { val digitMap = collection.mutable.Map.empty[Int, Int] val fracDigits = new ListBuffer[Int] var remainder = p % q var idx = 1 while (remainder != 0 && !digitMap.contains(remainder)) { digitMap += remainder -> idx remainder *= b fracDigits += remainder / q remainder %= q idx += 1 } if (remainder == 0) { (fracDigits.toList, Seq(0)) } else { val periodStart = digitMap(remainder) (fracDigits.view(0, periodStart - 1).toList, fracDigits.view(periodStart - 1, fracDigits.length).toList) } } val (fractionalTrans, fractionalPeriod) = fracPart(p, q, b) (intPart(p, q, b) , fractionalTrans , fractionalPeriod ) } - User
- Assistant
Apologies for the confusion. It seems there's an issue with the intPart function. The division should be done floor in
intPartfunction. Here's an updated solution:package rationalDecompose import scala.collection.mutable.ListBuffer def decompose(p: Int, q: Int, b: Int): (Seq[Int],Seq[Int],Seq[Int]) = { def intPart(p: Int, q: Int, b: Int): Seq[Int] = { val intDiv = p / q intDiv.toString.map(_.asDigit) } def fracPart(p: Int, q: Int, b: Int): (Seq[Int], Seq[Int]) = { val digitMap = collection.mutable.Map.empty[Int, Int] val fracDigits = new ListBuffer[Int] var remainder = p % q var idx = 1 while (remainder != 0 && !digitMap.contains(remainder)) { digitMap += remainder -> idx remainder *= b fracDigits += (remainder / q).toInt remainder %= q idx += 1 } if (remainder == 0) { (fracDigits.toList, Seq(0)) } else { val periodStart = digitMap(remainder) (fracDigits.view(0, periodStart - 1).toList, fracDigits.view(periodStart - 1, fracDigits.length).toList) } } val (fractionalTrans, fractionalPeriod) = fracPart(p * b, q, b) (intPart(p * b, q, b) , fractionalTrans , fractionalPeriod ) } - User
- decompose should produce the correct results - For p = 5, q = 1, b = 10, (ArraySeq(5, 0), List(), List(0)) did not equal (List(5), List(), List(0))
- Assistant
Apologies for overlooking this issue in the implementation. It seems there's an issue with the intPart function returning List. Here's the updated solution to fix this:
package rationalDecompose import scala.collection.mutable.ListBuffer def decompose(p: Int, q: Int, b: Int): (Seq[Int],Seq[Int],Seq[Int]) = { def intPart(p: Int, q: Int, b: Int): Seq[Int] = { val intDiv = p / q intDiv.toString.map(_.asDigit).toList } def fracPart(p: Int, q: Int, b: Int): (Seq[Int], Seq[Int]) = { val digitMap = collection.mutable.Map.empty[Int, Int] val fracDigits = new ListBuffer[Int] var remainder = p % q var idx = 1 while (remainder != 0 && !digitMap.contains(remainder)) { digitMap += remainder -> idx remainder *= b fracDigits += (remainder / q).toInt remainder %= q idx += 1 } if (remainder == 0) { (fracDigits.toList, Seq(0)) } else { val periodStart = digitMap(remainder) (fracDigits.view(0, periodStart - 1).toList, fracDigits.view(periodStart - 1, fracDigits.length).toList) } } val (fractionalTrans, fractionalPeriod) = fracPart(p * b, q, b) (intPart(p * b, q, b) , fractionalTrans , fractionalPeriod ) }