Ask Your Question
1

Lengths of preperiod and period of a rational number

asked 2020-01-01 15:34:15 +0100

geroyx gravatar image

updated 2020-01-05 17:23:00 +0100

FrédéricC gravatar image

I found something for Continued fractions, but I ask myself:

I there an easy way to get the lengths of preperiod and period of a rational number?

It seems for Rational numbers I can only use a.period(), not a.preperiod().

For, say, 1/7 = 0.142857142857... I should get something like:
Length of preperiod: 0
Length of period: 6

a = 1/7
print(float(a))
print('a.period() gives me the length of the period:', a.period())
print('a.preperiod() does not work', #a.preperiod())

Out:

0.142857142857
a.period() gives me the length of the period: 6
a.preperiod() does not work
edit retag flag offensive close merge delete

Comments

It's really not well-defined. For all you know, $0.142857142857=\frac{142857142857}{1000000000000}$, in which case the decimal expansion should be extended with $0$, so the period will be $1$ and the preperiod will be the place of the last non-zero digit.

nbruin gravatar imagenbruin ( 2020-01-02 21:29:51 +0100 )edit

@nbruin I posted an answer. ;)

geroyx gravatar imagegeroyx ( 2020-01-04 16:47:18 +0100 )edit

Ah, I see period is a method on a rational number, not on a float. That makes perfect sense then. Apologies for the noise.

nbruin gravatar imagenbruin ( 2020-01-04 18:32:38 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-01-04 16:46:47 +0100

geroyx gravatar image

updated 2020-01-05 16:23:13 +0100

Length of Preperiod and Period

a = 1
b = 2*7

u = denominator(a/b).valuation(2)
v = denominator(a/b).valuation(5)

def PreperiodLength(a,b): return max(u,v)

m = denominator(a/b) / (2^u*5^v)
y = 1/m

def PeriodLength(a,b):
    if m == 1:
        return 0
    else:
        return y.period()

#print('m = {}'.format(m))
#print('{} / {} = {}'.format(a, b, float(a/b)))
print('{} / {} = {}'.format(a, b, numerical_approx(a/b, digits=PreperiodLength(a,b) + PeriodLength(a,b))))
print('PreperiodLength: {}'.format(PreperiodLength(a,b)))
print('PeriodLength: {}'.format(PeriodLength(a,b)))

Out:

1 / 14 = 0.07142857
PreperiodLength: 1
PeriodLength: 6
edit flag offensive delete link more

Comments

Please use python3 syntax (sage 9.0) in your answers

FrédéricC gravatar imageFrédéricC ( 2020-01-04 18:22:07 +0100 )edit

@FrédéricC I fear this is over my knowledge. What do I have to correct?

geroyx gravatar imagegeroyx ( 2020-01-04 20:11:50 +0100 )edit
1

@geroyx. I edited the lines with print.

slelievre gravatar imageslelievre ( 2020-01-05 00:39:09 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-01-01 15:34:15 +0100

Seen: 636 times

Last updated: Jan 05 '20