编写首先添加输入值的python程序,然后检查新值是否为素数

时间:2015-02-26 10:50:11

标签: python

我正在尝试用Python编写一个程序:

  1. 添加输入。假设输入为123,它将添加它并给出输出(1 + 2 + 3)6
  2. 检查输出(6)是否为素数
  3. 这就是我一直在做的事情,但它似乎不适用于需要首先执行添加的10以上的数字。

    def prime_digit_sum(number):
        n = (number)
        temp = n = int(n) 
        remainder = sum = 0 
        while (  temp > 0) :
                remainder = temp % 10
                temp = temp/10
                sum= sum +remainder
    
        if number == 2:
            return 1
        elif number == 3:
            return 1
        if number <= 1 or number%2==0:  
            return 0  
        check=3  
        maxneeded=number  
        while check<maxneeded+1:  
            maxneeded=number/check  
            if number%check==0:  
                return 0  
            check+=2  
        return 1  
    
    while(True):
        text = input("please enter an integer: ")
        if (text == 'q'):
            break
        number = int(text)
    
        result = prime_digit_sum(number)
        if (result == None):
            continue
        if result:
            print("the sum of digits of", number, "is a prime number")
        else:
            print("the sum of digits of", number, "is not a prime number")
    
        print()
    
    print("exiting")
    

0 个答案:

没有答案