I'm working with fractions using Python's decimal module and I'd like to get just the repeating part of a certain fraction. For example: if I had 1/3 I'd like to get 3, if I had 1/7 I'd like to get 142857. Is there any standard function to do this?
Just divide the numerator by the denominator . If you end up with a remainder of 0 , then you have a terminating decimal. Otherwise, the remainders will begin to repeat after some point, and you have a repeating decimal.
To round up and down we use Python's round() function. The first argument we give that function is the number to round. The second argument the number of decimal places to round to. Python has no function that always rounds decimal digits up ( 9.232 into 9.24 ).
Since giving the answer could be a spoiler for project euler (which is generally not done here at stackoverflow), I'd like to give this hint: read this (section 1.2 should ring a bell).
Find the first number of the form 10**k - 1 that divides exactly by the denominator of the fraction, divide it by the denominator and multiply by the numerator and you get your repeating part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With