May 20, 2024

Wiki

Python

Aide

edit SideBar

Search

A little bit of mathematics in python


Perfect numbers

  1. Write a function divisors(n) that returns the list of positive divisors of an integer $n$ arranged in ascending order.
  2. An integer $n$ is said to be "perfect" if the sum of the positive divisors of $n$, distinct from $n$, is equal to $n$. For example, 6 is a perfect number.
  3. Write a function perfect(n) that returns the list of all perfect numbers between 1 and $n$.

Univariate statistical series

We consider a simple statistical series $\{ \; (x_i\,,\,n_i)\;\mathrm{with}\; 1\leq i\leq p\}$. Its average $m$ and its variance $V$ are defined by :

$ m=\frac{1}{N}\sum_{i=0}^{p} n_i\,x_i \qquad\mathrm{and}\qquad V=\frac{1}{N}\sum_{i=0}^{p}n_i(x_i-m)^2$

$N$ refers to the total number of people in the study population.

  1. Write a program that asks the user to enter two lists of the same size xi and ni. (xi being the list of observed values of the characteristic and ni those of the associated numbers)
  2. Have the average of this series calculated using a function moy(x,n).
  3. Have the variance of this series calculated using a function var(x,n).

Refined encryption

It is decided to code a secret message as follows:

  • to each letter $\alpha$ of the message, we associate its $x$ rank in the alphabet (rank between 0 and 25): a:0, b:1, c:2, ... , z:25
  • the rest of the Euclidean division of $5x+2$ by 26 is calculated $f(x)$
  • the letter $\alpha$ is then coded by the letter having the rank $f(x)$ in the alphabet.

Write a Python function that takes as a parameter a string written in lowercase and without accent, and returns the string encoded by the refined encryption described above.

Douglas Hofstadter's suite

Douglas Hofstadter left his name in the following sequence, in his bookGodel, Escher, Bach:

  1 3 7 12 18 26 35 45 56 69 83 98 114 131

To understand how it works, let's write below the sequence of the first differences (these are the quantities that separate a term from its neighbour):

  S : 1 3 7 12 18 26 35 45 56 69 83 98 114 131
  d : 2 4 5 6 8 9 10 11 13 14 15 16 17

...the numbers that do not appear in the first sequence are in the second, and vice versa. This is the very definition of this sequence: it is constructed by adding to the last written number the smallest integer not present in S or d.

Build the Hofstadter suite. We will do a function that associates $n$ with the $n$-th term of this suite.

Euler indicator

The Euler indicator function $\phi(n)$ is equal to the number of integers between 0 and $n-1$ that are coprime with $n$, agreeing that

  • $\phi(0) = $0,
  • $\phi(1) = $1.
  1. What's $\phi(12)$ worth?
  2. Make a function that returns $\phi(n)$.
  3. Experimentally find, with your function, a link between $\phi(n \times m)$ and $\phi(n)$, $\phi(m)$.
  4. What do you think is equal to $phi(p^n)$ when $p$ is prime?

Pascal's Triangle

1. Make a function that builds the Pascal's triangle up to his $n$-th line. It will return a list of lists.

2. Make a function that receives this list of lists as an argument, and which returns a triangle representation:

1      
11     
121    
1331   
14641  
15101051 
1615201561
(we can use the tabulations \t).

3. Use this function to experimentally verify that the sum of the terms of the $n$-th line is equal to $2^n$.

Page Actions

Recent Changes

Group & Page

Back Links