#!/usr/bin/python#-*-coding:utf-8-*- fromMathImportsqrtImportRandomdefDaoxu (n): D=N S=0 whiled!=0:d,f=divmod (d,10) s=f+s*10returnsdefz85 ():#arbitrarily take a decimal number such as 123, then he and his palindrome number 321 Add, get a new integer after repeating the above steps, and finally #can get a palindrome number 444s1=179233126S2=0 whiles2!=s1:s1=s1+S2 s2=Daoxu (S1)PrintS2returndefz81 ():" "The Angle Valley conjecture is any one number if it is even divided by 2 if it is odd multiplied by 3 plus 1, which will eventually lead to 1,4,2, loop" "x=123347 whileX!=1: x= X%2==0 andX/2or(x*3+1)/2PrintxdefFANGF (x): T=Int (sqrt (x))ifT*t==x:return[T] t=int (sqrt (x-1)) +1m=Range (t) forIinchm: forJinchm: forKinchRange (j,t): forPinchRange (k,t):ifI *i + J * J + k * k + p * p = =x:ifI==0 andJ==0:return[K,p]ifI==0 andJ!=0:return[J,k,p]return[I,j,k,p]defz82 ():#all natural numbers can be represented by a square of four natural numbers to verify the theorem forIinchRange (2,100): PrintI,FANGF (i)defz83 ():#any one four-digit number, such as 1324, can get the number 1234 and 4321, then 4321-1234 to get the new number, repeat the above steps, #finally get 6174. and 7641-1467=6174 defSix (x): T=1e=list (str (x)) E.sort () e="'. Join (e) n=Int (e)ifn<1000:t=10returnDaoxu (n) *t-n i=1999PrintI whilei!=6174: I=Six (i)PrintIdefz84 ():" "prove that any one number a cubic is equal to a series of consecutive odd sum, where the first item is (A * (A-1) +1), the tolerance is 2, a total of their sum is ((A * (A-1) +1) *a+ (2*a-2) *a/2= (a^3-a^2+a) + (a^2-a) =a^3 " " Print "any cubic of a number a is equal to a series of consecutive odd and" returndefZ80 ():" "it is easy to prove that the theorem is greater than 1000 odd X has x*x-1 is a multiple of 8 [1,3,5,7]=[1,1,1,1]" "T=[i*i%8 forIinchRange (1,8,2)] PrintTPrint "odd x greater than 1000 with x*x-1 is a multiple of 8" returndefz78 ():#calculation of pi using regular polygon approximation methodx1=100000B,i=0.5,6 whilei<x1:b=sqrt (2-2*sqrt (1-B*B))/2I*=2Printb*Idefz79 ():" "random method to calculate PI: This program does not use the calculation of Pi value algorithm, it is only a probabilistic simulation, that is, in the side length of 100 square within the random generation of multiple points, the point is divided into arcs for the boundary statistics, because of the number of points, until almost full area. At this point, the number of points can be regarded as the area of its area. Can be deduced as follows: (1) The number of points in the Blue area: Total number of ≈ Blue area: Total area (2) number of points in blue area: Total number ≈ Circle Area/4: Total area (3) Number of points in blue area: Total number of points ≈πx200x200/4:200x200 (4) π≈4x number of dots in blue area/total number of course , the position of the point is repeated, so the result is different from the π value, but when the point is long enough, a very close result can be seen. " "x, y=0,100000, 0 g=Random.random whilex<y:a=g () b=g ()ifA*a+b*b<1: Z+=1x+=1Print4.0*z/yif __name__=='__main__': S="" forIinchRange (78,86): S+='Z'+str (i) +'() \ n' exec(s)
Python Exercise 4