<> Function design
<> Experimental objectives
1. Master the design and use of functions
2. In depth understanding of various functions , Familiar with parameter transfer process
3. Master the scope of variables , Generator functions and lambda function
4. Understand the recursion and use of functions .
<> Experimental process
notes : This experiment uses Jupyter Notebook compile , And has been used pycharm Conduct feasibility verification .
<>
4. Write function , Judge whether a number is the number of daffodils . Call this function to print out 1000 Number of all daffodils within . The number of daffodils is one n digit (n≥3), The number on each bit of it n The sum of powers equals itself .
for example : 1 3 1^3 13+ 5 3 5^3 53+ 1 3 1^3 13
=153, be 153 It's daffodils . Narcissus number is only a kind of self idempotent number , Strictly speaking, three digit 3 The next idempotent becomes the daffodil number .
Reference code :
def f(n): if 100 < n < 1000 : bw = n // 100 sw = (n - bw * 100) // 10 gw = n %
10 if n == bw ** 3 + sw ** 3 + gw ** 3: print(n,end=' ') for i in range(1,1000):
f(i)
Experimental screenshot :
<>
5. Write a function to find the front of Fibonacci sequence 20 term . The second part of fiboracci sequence 1 Xiang Hedi 2 Items are 0 and 1, From the first 3 Item start , Each item is the sum of the first two items . as :0,1,1,2,3,5,8,13,21…. Try recursive function implementation .
Reference code :
def fibo(n): if n <= 1: return n else: return (fibo(n - 1) + fibo(n - 2)) n =
int(input(" Number of items in Fibonacci sequence ")) if n <= 0: print(" Enter a positive number ") else: print(" Fibonacci sequence :") for i
in range(n): print(fibo(i))
Experimental screenshot :
If there are mistakes or better methods , it is respectful to have you criticize and correct sth !
Technology