Attachment 'chap4-functions.rst'
DownloadChapter 4: Functions
Authors: | Vincent Delecroix |
---|---|
License: | CC BY-SA 3.0 |
The aim of this fourth chapter is to introduce you to write yourself your own function. This is useful if you want to reuse many times the same piece of code while not copy/pasting it.
The structure of a function is always the same:
sage: def f(n): ....: f = n.prime_factors() ....: return f[-1] + 1
Exercise 4.1
Write a function computing the area of a triangle from the length of the three sides:
sage: # edit here
Exercise 4.2
The duration of sunshine D(β, d) on a given place on Earth is given by the formula
where κ = (23.44)/(180)π is the inclination of the earth in radian, d ∈ [0, 365] is the number of days after the spring equinox and β ∈ [ − π ⁄ 2, π ⁄ 2] is the latitude of the place in question. Write the function D(beta, d):
sage: # edit here
Construct the list of duration of sunshine in Marseille for the 31 days of the month of July 2017:
sage: # edit here
Exercise 4.3
Let the sequence un + 1 = (1)/(1 + u2n) with u0 = 0. Write a function U(n) which returns the value of un. Compute u20:
sage: # edit here
Exercise 4.4
Write a function product_of_digits(n) which returns the product of the digits of n written in base 10:
sage: # edit here
Exercise 4.5
The number 6 is called a perfect number, because it is equal to the sum of its proper divisors: 6 = 1 + 2 + 3. Write a function is_perfect(n) which returns True or False whether the number n is perfect:
sage: # edit here
Find all four perfect numbers below 10000:
sage: # edit here
Find all four perfect numbers below 106:
sage: # edit here
https://projecteuler.net/problem=23 (abundant, perfect, non-abundant numbers)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.