登录后查看本帖详细内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
6.25:反素数 def isprime(number): isprime=True i=2 while i < number: if number % i ==0: isprime=False i+=1 return isprime def reverseisprime(number): remainder='' a=number while number != 0: remainder=remainder+str(number % 10) number=number // 10 if isprime(int(remainder)): return True def reverse(number): remainder='' a=number while number != 0 : remainder=remainder+str(number % 10) number=number // 10 if int(remainder)==a: return True else: return False count=0 j=2 while count < 100 : if (isprime(j) and not reverse(j)) and (reverseisprime(j)): print(format(j,'6d'),end=' ') count+=1 if count % 10 ==0: print() j+=1 6.26:梅森素数: def isPrime(number): isprime=True i=2 while i < number: if number % i ==0: isprime=False i+=1 return isprime def meisenPrime(): print('p 2^p-1') for p in range(2,32): number=2**p-1 if isPrime(number): print(p,'\t',number) meisenPrime() 6.27:双素数: def isPrime(number): isprime=True i=2 while i < number: if number % i ==0: isprime=False i+=1 return isprime def doublePrime(): for i in range(2,1000): if isPrime(i): if isPrime(i+2): print(i,i+2) doublePrime() 6.28: import random def point(): number1=random.randint(1,6) number2=random.randint(1,6) return number1+number2 number1=random.randint(1,6) number2=random.randint(1,6) if (number1+number2==2) or (number1+number2==3) or (number1+number2==12): print('you rolled '+str(number1)+'+'+str(number2)+'='+str(number1+number2)) print('you lose!') elif (number1+number2==7) or (number1+number2==11) : print('you rolled '+str(number1)+'+'+str(number2)+'='+str(number1+number2)) print('you win!') elif (number1+number2==4)or (number1+number2==5)or(number1+number2==6)or(number1+number2==8)or(number1+number2==9)or(number1+number2==10): point1=number1+number2 point2=point() if point2==7: print('point1 is'+str(point1)) print('point2 is'+str(point2)+'you lose') elif point2==point1: print('point1 is'+str(point1)) print('point2 is'+str(point2)+'you win') while point2!=7 and point2!=point1: point2=point() if point2==7: print('point1 is'+str(point1)) print('point2 is'+str(point2)+'you lose') elif point2==point1: print('point1 is'+str(point1)) print('point2 is'+str(point2)+'you win') 6.29: def reverse(number): sum='' while number != 0: remainder=number%10 number=number // 10 sum=sum+str(remainder) return int(sum) def isValid(number): isValid=False if 1000000000000<= number <=9999999999999999: reverseNumber=reverse(number) if reverseNumber % 10 ==4: isValid=True elif reverseNumber % 10 ==5: isValid=True elif reverseNumber % 10 == 3 and reverseNumber// 10 % 10 ==7 : isValid=True elif reverseNumber % 10 ==6: isValid=True return isValid def getSize(number): count=0 while number != 0: number=number//10 count+=1 return count def sumOfPlace(number): sumOfDouble=0 sumOfsingle=0 i=str(number) for j in range(getSize(number)//2): sumOfDouble+=( (int(i[j*2])*2) //10 ) + ( (int(i[j*2])*2) * 10 ) for k in range(getSize(number)//2): sumOfsingle+=(int(i[k*2+1])) sum=sumOfDouble+sumOfsingle return sum def main(number): if isValid(number): if sumOfPlace(number) % 10 ==0: return True else: return False else: print(str(number)+'is not valid') print(main(eval(input('enter thenumber:'))))
|