简体   繁体   English

除法时确定数字的周期

[英]determining the period of a number when dividing

So I need to find period of a number when dividing, for example 1/7 = 0.142857142857142857... and print it like 0.(142857) so in brackets I need to print a period of that number.所以我需要在除法时找到一个数字的句点,例如 1/7 = 0.142857142857142857... 并像 0.(142857) 一样打印它,所以在括号中我需要打印一个该数字的句点。

I'm using Set for this example我在这个例子中使用 Set

class Set:
    def __init__(self):
        self.content= []

    def __str__(self):
        self.content.sort()
        return '' + str(self.content)

    def length(self):
        return len(self.content)

    def insert(self,x):
        if x not in self.content:
            self.content.append(x)

    def erase(self,x):
        for i in range(self.length()-1):
            if self.content[i]==x:
                del self.content[i]

    def find(self,x):
        if x in self.content:
            return True
        else:
            return False

    def items(self):
        return self.content

Here I want to convert that eval input to a string because I want to find point while dividing using expression.index('.') and finding that period.在这里,我想将该 eval 输入转换为字符串,因为我想在使用 expression.index('.') 进行除法并找到该句点时找到点。 After that I'm putting that period in 'k' and print the number to the point + period of that number.之后,我将该句点放在“k”中并将数字打印到该数字的点+句点。

if __name__=='__main__':
    set1=Set()
    expression=eval(input('enter an expression: '))
    expression=str(expression)
    point=expression.index('.')
    for i in range(point+1,len(expression)):
        set1.insert(expression[i])
    k=''
    for x in set1.items():
        k+=x

    print(f'{expression[:point+1]}({k})')

Problem here is that for example 7/9 = 0.77777777778 program will print 0.(78) like 78 is a period, but only 7 is a period here not 0.78787878787878...这里的问题是,例如 7/9 = 0.77777777778 程序将打印 0.(78) 就像 78 是一个句点,但这里只有 7 是一个句点而不是 0.78787878787878...

The problem is that eval() returns a floating point representation of the decimal number.问题是eval()返回十进制数的浮点表示。 This representation is inexact as you see from your question.正如您从问题中看到的那样,这种表示是不准确的。 Instead, you need to come up with an algorithm to calculate the digits yourself.相反,您需要想出一个算法来自己计算数字。 I suggest that you parse the integers from the entered fraction and perform the division yourself.我建议您从输入的分数中解析整数并自己执行除法。

There are a few ways to do this.有几种方法可以做到这一点。 I am using the string manipulation to figure out if the expression is repeating.我正在使用字符串操作来确定表达式是否重复。

  • Step 1: Find out the decimal place in the result.第 1 步:找出结果中的小数位。 Grab all the values after the decimal place to process抓取小数点后的所有值进行处理
  • Step 2: If there is no decimal place, the response is no decimals第2步:如果没有小数位,则响应no decimals
  • Step 3: Iterate through the first half of the result string to find the pattern第 3 步:遍历结果字符串的前半部分以查找模式
  • Step 3a: If the left half can be found in the right half, then keep track of the left half.步骤 3a:如果可以在右半部分找到左半部分,则跟踪左半部分。 It may be the recurring period.这可能是循环周期。
  • Step 3b: If the left half is NOT found in the right half, shift the left half by 1 to the left, and find that portion of the string towards the right.步骤 3b:如果在右半部分中没有找到左半部分,则将左半部分向左移动 1,然后找到向右的字符串部分。 If found, keep track, if not found repeat Step 3b如果找到,请跟踪,如果没有找到,请重复步骤 3b
  • Step 4: Once you have iterated through the full string and reached the 0th position, you will have the smallest sized string that's repeated.第 4 步:一旦您遍历整个字符串并到达第 0 个 position,您将获得重复的最小大小的字符串。 That's your period string那是你的句号
  • Step 5: After processing the string from mid point to 0th position, if you don't find anything, then there is no repeated period.第五步:处理中点到第0个position的字符串后,如果没有找到,则没有重复句号。 Return the response based on whether you found the repeated period string or not根据是否找到重复的句点字符串返回响应

The code is as shown below:代码如下所示:

def find_period(e):
    x = e.find('.')
    if x == -1: return 'no decimals'
    period = ''
    dec_exp = e[x+1:]
    for i in range(len(dec_exp)//2,0,-1):
        if dec_exp[i:].find(dec_exp[:i]) == 0:
            period = dec_exp[:i]
    return f'0.({period})' if period else 'no decimals'

expression = eval(input('enter an expression: '))
result     = str(expression)
print (find_period(result))

The output for some of the expressions are:一些表达式的 output 是:

enter an expression: 1/7
0.(142857)

enter an expression: 3/7
0.(428571)

enter an expression: 7/9
0.(7)

enter an expression: 3/14
no decimals

enter an expression: 22 + 5
no decimals

enter an expression: 8/2
no decimals

enter an expression: 6 * 2
no decimals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM