Skip to main content

Repeating digits of rational numbers

Project description

This package provides a way to access repeating decimals in the decimal representation of rational numbers.

class object

>>> from dvtDecimal import *

Once package importation completed, you have to create a rational number using:

  • a fraction representation
>>> f = dvtDecimal(-604, 260)

for the fraction whose numerator is -604 and denominator is 260.

  • or a decimal representation
>>> f = dvtDecimal(2.5)
  • or repeating decimals as a string
>>> f = dvtDecimal('00765')

thus creating a number (w/o irregular part) between 0 and 1. In the example, 0.007650076500765... and so on.

object methods

Once you created the object, you can access to those variables:

>>> f.initValues
[-604, 260]
>>> f.simpValues
[-151, 65]
>>> f.intPart
-2
>>> f.irrPart
0.3
>>> f.repPart
[2, 3, 0, 7, 6, 9]
>>> f.repPartC
230769
>>> f.periodLen
6
>>> f.gcd
4
>>> f.mixedF
[-2, 21, 65]
>>> f.sign
-1

and to those methods:

>>> f.dispResults()
For fraction: -604 / 260
    integer   part : -2
    irregular part : 0.3
    periodic  part : [2, 3, 0, 7, 6, 9]
    mixed fraction : [-2, 21, 65]
    simp. fraction : [-151, 65]
               gcd : 4
    Python outputs : -2.3230769230769233
>>> f.isDecimal()
False
>>> f.dotWrite(20)
-2.32307692307692307692

Entering via repeating decimals string allows:

>>> f = dvtDecimal('0123456789')
>>> f.simpValues
[13717421, 1111111111]

dvtDecimal also supports minimal operations (+,-,*,/) in between elements of the class but also with integers:

>>> f = dvtDecimal(1, 5)
>>> g = dvtDecimal(10, 3)
>>> h = f + g
>>> h.mixedF
[3, 8, 15]
>>> i = f / g
>>> i.mixedF
[0, 3, 50]
>>> f = dvtDecimal(1, 5)
>>> g = 5
>>> h = f * g
>>> h.isDecimal()
True
>>> f = dvtDecimal(1, 5)
>>> g = dvtDecimal(7, 5)
>>> h = f - g
>>> h.simpValues
[-6, 5]

egyptian fractions

IMPORTANT: This part gives you results for the fractionnal part of the rational number you work with i.e. the last two numbers in the mixedF list, so that you only get results for <=1 fractions.

dvtDecimal provides the method egyptFractions to get all the egyptian fractions equal to your current fraction.

egyptFractions outputs a list of lists. Each of these lists are denominators (increasing) for unitary fractions whose sum equals to your fraction.

Two optional arguments:

  • eF number of fractions in the sums, default is 3
  • lim max. number of solutions in the results, default is 10

(NB A tolerance variable can be set for this method before its call, by default fixed to 1e-6. It is used for integer test. For example, f.tolerance = 1e-8 to change its default value for f object).

>>> f = dvtDecimal(18,5)
>>> f.mixedF
[3, 3, 5]
>>> f.egyptFractions()
[[2, 11, 110], [2, 12, 60], [2, 14, 35], [2, 15, 30], [2, 20, 20], [3, 4, 60], [3, 5, 15], [3, 6, 10]]
>>> f.egyptFractions(lim=5)
[[2, 11, 110], [2, 12, 60], [2, 14, 35], [2, 15, 30], [2, 20, 20]]
>>> f.egyptFractions(eF=4)
[[2, 11, 111, 12210], [2, 11, 112, 6160], [2, 11, 114, 3135], [2, 11, 115, 2530], [2, 11, 120, 1320], [2, 11, 121, 1210], [2, 11, 130, 715], [2, 11, 132, 660], [2, 11, 135, 594], [2, 11, 154, 385]]

In the above example, egyptian fractions are thus calculated with fraction 3/5.

First solution [2, 11, 111, 12210] (in the last command result) has to be interpreted as: 3/5 = 1/2 + 1/11 + 1/111 + 1/12210

dvtDecimal also provides egyptG2 variable. This gives you a list of two denominators of unitary fractions whose sum equals your fraction.

This is an implementation of the greedy algorithm. It gives you naturally matching pairs for unitary fractions input.

The result may be an empty list since all numbers cannot be written as so.

>>> f = dvtDecimal(18,5)
>>> f.intPart
3
>>> f.egyptG2
[2, 10]

So: 18/5 = 3 + 1/2 + 1/10

further

More operations!

about

dvtDecimal is rather an attempt to publish on the PyPi packages index than a fully completed python project, I do not recommend dvtDecimal usage for professionnal use. You have to consider this package as an experiment.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dvtDecimal-1.0.1.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

dvtDecimal-1.0.1-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page