aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-31 14:57:30 +0200
committerAbigail <abigail@abigail.be>2021-03-31 14:57:30 +0200
commit51f1e35114bff4bddd2119641fe329afcb416874 (patch)
treeb40a2124c41f86abee9b088187ae3454528d9759
parent56a9acc2b52770903eabdea426ffadd053809ba7 (diff)
downloadperlweeklychallenge-club-51f1e35114bff4bddd2119641fe329afcb416874.tar.gz
perlweeklychallenge-club-51f1e35114bff4bddd2119641fe329afcb416874.tar.bz2
perlweeklychallenge-club-51f1e35114bff4bddd2119641fe329afcb416874.zip
Notes for week 106, part 2
-rw-r--r--challenge-106/abigail/README.md40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-106/abigail/README.md b/challenge-106/abigail/README.md
index e2550248dd..2f9f8e7880 100644
--- a/challenge-106/abigail/README.md
+++ b/challenge-106/abigail/README.md
@@ -52,7 +52,47 @@ Input: $N = 5, $D = 66
Output: "0.0(75)"
~~~~
+### Notes
+
+We're creation the decimal expansion of the fraction `$N / $D`
+by performing long division.
+
+First, we calculate the part before the decimal point, by
+doing integer division of `$N / $D`.
+We're then left to do division of `$N' / $D`, where `$N'` initially
+is `$N % $D`.
+We then repeatedly find new digits by calculating the integer
+division of `(10 * $N' / $D)` (which gives us a new digit in the
+decimal expansion), and then setting `$N' = 10 * $N' % $D`.
+The fraction will have a finite decimal expansion if during the
+process `$N'` becomes `0`. Otherwise, it repeats, and it repeats
+as soon as have a `$N'` which we've already seen. By the pidgeon
+hole principle, this cannot take more then $D steps.
+To calculate the repeating part, we keep track of how far we
+were in calculating the expansion for which `$N'`.
+
+~~~~
+ 22/7 \0.318
+ 0 int (7 / 22) == 0, so 0 before decimal point
+ --
+ 7 N = N % D
+ 66 3 * D
+ --
+ 4 N = (10 * N) % D <--+
+ 22 1 * D |
+ -- | Same, so '18'
+ 18 N = (10 * N) % D | is the repeating
+ 176 8 * D | part
+ --- |
+ 4 N = (10 * N) % D <--+
+~~~~
+
+This implementation is based on the [one given on
+Wikipedia](https://en.wikipedia.org/wiki/Repeating_decimal).
+
+
### Solutions
+* [AWK](perl/ch-2.awk)
* [Perl](perl/ch-2.pl)
### Blog